| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-present eyeo GmbH | |
| 4 * | |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | |
| 6 * it under the terms of the GNU General Public License version 3 as | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 14 * You should have received a copy of the GNU General Public License | |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
| 16 */ | |
| 17 | |
| 18 "use strict"; | |
| 19 | |
| 20 /** | |
| 21 * A <code>Cache</code> object represents a cache of arbitrary data. | |
| 22 */ | |
| 23 class Cache | |
| 24 { | |
| 25 /** | |
| 26 * Creates a cache. | |
| 27 * @param {number} capacity The maximum number of entries that can exist in | |
| 28 * the cache. | |
| 29 */ | |
| 30 constructor(capacity) | |
| 31 { | |
| 32 if (isNaN(capacity)) | |
| 33 throw new Error("capacity must be a number."); | |
| 34 | |
| 35 this._capacity = capacity; | |
| 36 this._storage = new Map(); | |
| 37 } | |
| 38 | |
| 39 /** | |
| 40 * Reads an entry from the cache. | |
| 41 * @param {?*} key The key for the entry. | |
| 42 * @returns {?*} The value of the entry, or <code>undefined</code> if the | |
| 43 * entry doesn't exist in the cache. | |
| 44 */ | |
| 45 read(key) | |
|
hub
2019/02/14 20:43:00
I think I would still have names these methods `ge
Manish Jethani
2019/02/16 12:17:17
Done.
| |
| 46 { | |
| 47 return this._storage.get(key); | |
| 48 } | |
| 49 | |
| 50 /** | |
| 51 * Saves an entry into the cache. If the cache has reached the specified | |
| 52 * maximum number of entries, all the old entries are cleared first. | |
| 53 * @param {?*} key The key for the entry. | |
| 54 * @param {?*} value The value of the entry. | |
| 55 */ | |
| 56 save(key, value) | |
| 57 { | |
| 58 // To prevent logical errors, neither key nor value is allowed to be | |
| 59 // undefined. | |
| 60 if (typeof key == "undefined") | |
| 61 throw new Error("key must not be undefined."); | |
| 62 | |
| 63 if (typeof value == "undefined") | |
| 64 throw new Error("value must not be undefined."); | |
| 65 | |
| 66 if (this._storage.size >= this._capacity) | |
| 67 this._storage.clear(); | |
| 68 | |
| 69 this._storage.set(key, value); | |
| 70 } | |
| 71 | |
| 72 /** | |
| 73 * Clears the cache. | |
| 74 */ | |
| 75 clear() | |
| 76 { | |
| 77 this._storage.clear(); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 exports.Cache = Cache; | |
| OLD | NEW |