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: Created Feb. 14, 2019, 5:36 a.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 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;
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