Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/caching.js

Issue 30007559: Issue 7285 - Abstract caching logic (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add tests and change API and behavior slightly Created Feb. 16, 2019, 12:14 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | lib/elemHide.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // Note: This check works for non-number values.
33 if (!(capacity >= 1))
34 throw new Error("capacity must be a positive number.");
35
36 this._capacity = capacity;
37 this._storage = new Map();
38 }
39
40 /**
41 * Reads an entry from the cache.
42 * @param {?*} key The key for the entry.
43 * @returns {?*} The value of the entry, or <code>undefined</code> if the
44 * entry doesn't exist in the cache.
45 */
46 get(key)
47 {
48 return this._storage.get(key);
49 }
50
51 /**
52 * Writes an entry to the cache. If the cache has reached the specified
53 * maximum number of entries, all the old entries are cleared first.
54 * @param {?*} key The key for the entry.
55 * @param {?*} value The value of the entry.
56 */
57 set(key, value)
58 {
59 // To prevent logical errors, neither key nor value is allowed to be
60 // undefined.
61 if (typeof key == "undefined")
62 throw new Error("key must not be undefined.");
63
64 if (typeof value == "undefined")
65 throw new Error("value must not be undefined.");
66
67 if (this._storage.size == this._capacity && !this._storage.has(key))
Manish Jethani 2019/02/16 12:17:17 This is slightly less efficient but more correct (
hub 2019/02/18 13:36:51 Acknowledged.
68 this._storage.clear();
69
70 this._storage.set(key, value);
71 }
72
73 /**
74 * Clears the cache.
75 */
76 clear()
77 {
78 this._storage.clear();
79 }
80 }
81
82 exports.Cache = Cache;
OLDNEW
« no previous file with comments | « no previous file | lib/elemHide.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld