Index: lib/caching.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/lib/caching.js |
@@ -0,0 +1,81 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-present eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+"use strict"; |
+ |
+/** |
+ * A <code>Cache</code> object represents a cache of arbitrary data. |
+ */ |
+class Cache |
+{ |
+ /** |
+ * Creates a cache. |
+ * @param {number} capacity The maximum number of entries that can exist in |
+ * the cache. |
+ */ |
+ constructor(capacity) |
+ { |
+ if (isNaN(capacity)) |
+ throw new Error("capacity must be a number."); |
+ |
+ this._capacity = capacity; |
+ this._storage = new Map(); |
+ } |
+ |
+ /** |
+ * Reads an entry from the cache. |
+ * @param {?*} key The key for the entry. |
+ * @returns {?*} The value of the entry, or <code>undefined</code> if the |
+ * entry doesn't exist in the cache. |
+ */ |
+ 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.
|
+ { |
+ return this._storage.get(key); |
+ } |
+ |
+ /** |
+ * Saves an entry into the cache. If the cache has reached the specified |
+ * maximum number of entries, all the old entries are cleared first. |
+ * @param {?*} key The key for the entry. |
+ * @param {?*} value The value of the entry. |
+ */ |
+ save(key, value) |
+ { |
+ // To prevent logical errors, neither key nor value is allowed to be |
+ // undefined. |
+ if (typeof key == "undefined") |
+ throw new Error("key must not be undefined."); |
+ |
+ if (typeof value == "undefined") |
+ throw new Error("value must not be undefined."); |
+ |
+ if (this._storage.size >= this._capacity) |
+ this._storage.clear(); |
+ |
+ this._storage.set(key, value); |
+ } |
+ |
+ /** |
+ * Clears the cache. |
+ */ |
+ clear() |
+ { |
+ this._storage.clear(); |
+ } |
+} |
+ |
+exports.Cache = Cache; |