Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 11 matching lines...) Expand all Loading... | |
22 */ | 22 */ |
23 class Cache | 23 class Cache |
24 { | 24 { |
25 /** | 25 /** |
26 * Creates a cache. | 26 * Creates a cache. |
27 * @param {number} capacity The maximum number of entries that can exist in | 27 * @param {number} capacity The maximum number of entries that can exist in |
28 * the cache. | 28 * the cache. |
29 */ | 29 */ |
30 constructor(capacity) | 30 constructor(capacity) |
31 { | 31 { |
32 if (isNaN(capacity)) | 32 // Note: This check works for non-number values. |
33 throw new Error("capacity must be a number."); | 33 if (!(capacity >= 1)) |
34 throw new Error("capacity must be a positive number."); | |
34 | 35 |
35 this._capacity = capacity; | 36 this._capacity = capacity; |
36 this._storage = new Map(); | 37 this._storage = new Map(); |
37 } | 38 } |
38 | 39 |
39 /** | 40 /** |
40 * Reads an entry from the cache. | 41 * Reads an entry from the cache. |
41 * @param {?*} key The key for the entry. | 42 * @param {?*} key The key for the entry. |
42 * @returns {?*} The value of the entry, or <code>undefined</code> if the | 43 * @returns {?*} The value of the entry, or <code>undefined</code> if the |
43 * entry doesn't exist in the cache. | 44 * entry doesn't exist in the cache. |
44 */ | 45 */ |
45 read(key) | 46 get(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 { |
47 return this._storage.get(key); | 48 return this._storage.get(key); |
48 } | 49 } |
49 | 50 |
50 /** | 51 /** |
51 * Saves an entry into the cache. If the cache has reached the specified | 52 * Writes an entry to the cache. If the cache has reached the specified |
52 * maximum number of entries, all the old entries are cleared first. | 53 * maximum number of entries, all the old entries are cleared first. |
53 * @param {?*} key The key for the entry. | 54 * @param {?*} key The key for the entry. |
54 * @param {?*} value The value of the entry. | 55 * @param {?*} value The value of the entry. |
55 */ | 56 */ |
56 save(key, value) | 57 set(key, value) |
57 { | 58 { |
58 // To prevent logical errors, neither key nor value is allowed to be | 59 // To prevent logical errors, neither key nor value is allowed to be |
59 // undefined. | 60 // undefined. |
60 if (typeof key == "undefined") | 61 if (typeof key == "undefined") |
61 throw new Error("key must not be undefined."); | 62 throw new Error("key must not be undefined."); |
62 | 63 |
63 if (typeof value == "undefined") | 64 if (typeof value == "undefined") |
64 throw new Error("value must not be undefined."); | 65 throw new Error("value must not be undefined."); |
65 | 66 |
66 if (this._storage.size >= this._capacity) | 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.
| |
67 this._storage.clear(); | 68 this._storage.clear(); |
68 | 69 |
69 this._storage.set(key, value); | 70 this._storage.set(key, value); |
70 } | 71 } |
71 | 72 |
72 /** | 73 /** |
73 * Clears the cache. | 74 * Clears the cache. |
74 */ | 75 */ |
75 clear() | 76 clear() |
76 { | 77 { |
77 this._storage.clear(); | 78 this._storage.clear(); |
78 } | 79 } |
79 } | 80 } |
80 | 81 |
81 exports.Cache = Cache; | 82 exports.Cache = Cache; |
LEFT | RIGHT |