diff --git a/jest/__snapshots__/bundles-snapshot.test.js.snap b/jest/__snapshots__/bundles-snapshot.test.js.snap index b8248e1a..82cd60af 100644 --- a/jest/__snapshots__/bundles-snapshot.test.js.snap +++ b/jest/__snapshots__/bundles-snapshot.test.js.snap @@ -370,63 +370,6 @@ exports[`Dist bundle is unchanged 1`] = ` return LruMapCache; }(); - var RrObjectCache = /*#__PURE__*/function () { - function RrObjectCache(_temp) { - var _ref = _temp === void 0 ? {} : _temp, - cacheSize = _ref.cacheSize; - - validateCacheSize(cacheSize); - this.clear(); - this._cacheSize = cacheSize; - } - - var _proto = RrObjectCache.prototype; - - _proto.set = function set(key, selectorFn) { - if (this._cacheLength >= this._cacheSize) { - this._randomReplace(key, selectorFn); - } else { - this._cache[key] = selectorFn; - this._cacheKeys[this._cacheLength] = key; - this._cacheLength++; - } - }; - - _proto.get = function get(key) { - return this._cache[key]; - }; - - _proto.remove = function remove(key) { - var index = this._cacheKeys.indexOf(key); // O(1) - - - if (index > -1) { - delete this._cache[key]; - this._cacheLength--; - this._cacheKeys[index] = this._cacheKeys[this._cacheLength]; - } - }; - - _proto.clear = function clear() { - this._cache = {}; - this._cacheKeys = []; - this._cacheLength = 0; - }; - - _proto._randomReplace = function _randomReplace(newKey, newValue) { - var index = Math.floor(Math.random() * this._cacheLength); - delete this._cache[this._cacheKeys[index]]; - this._cacheKeys[index] = newKey; - this._cache[newKey] = newValue; - }; - - _proto.isValidCacheKey = function isValidCacheKey(cacheKey) { - return isStringOrNumber(cacheKey); - }; - - return RrObjectCache; - }(); - var RrMapCache = /*#__PURE__*/function () { function RrMapCache(_temp) { var _ref = _temp === void 0 ? {} : _temp, @@ -440,13 +383,12 @@ exports[`Dist bundle is unchanged 1`] = ` var _proto = RrMapCache.prototype; _proto.set = function set(key, selectorFn) { - if (this._cacheLength >= this._cacheSize) { + if (this._cache.size >= this._cacheSize) { this._randomReplace(key, selectorFn); } else { this._cache.set(key, selectorFn); - this._cacheKeys[this._cacheLength] = key; - this._cacheLength++; + this._cacheKeys[this._cache.size] = key; } }; @@ -460,19 +402,17 @@ exports[`Dist bundle is unchanged 1`] = ` if (index > -1) { delete this._cache[\\"delete\\"](key); - this._cacheLength--; - this._cacheKeys[index] = this._cacheKeys[this._cacheLength]; + this._cacheKeys[index] = this._cacheKeys[this._cache.size]; } }; _proto.clear = function clear() { this._cache = new Map(); this._cacheKeys = []; - this._cacheLength = 0; }; _proto._randomReplace = function _randomReplace(newKey, newValue) { - var index = Math.floor(Math.random() * this._cacheLength); + var index = Math.floor(Math.random() * this._cache.size); this._cache[\\"delete\\"](this._cacheKeys[index]); @@ -491,7 +431,6 @@ exports[`Dist bundle is unchanged 1`] = ` exports.LruMapCache = LruMapCache; exports.LruObjectCache = LruObjectCache; exports.RrMapCache = RrMapCache; - exports.RrObjectCache = RrObjectCache; exports.createCachedSelector = createCachedSelector; exports.createStructuredCachedSelector = createStructuredCachedSelector; exports[\\"default\\"] = createCachedSelector; diff --git a/src/cache/README.md b/src/cache/README.md index 014f8660..91ce7264 100644 --- a/src/cache/README.md +++ b/src/cache/README.md @@ -11,7 +11,6 @@ | [`FlatObjectCache`](./FlatObjectCache.js) | `number` `string` | flat unlimited | JS object | | [`FifoObjectCache`](./FifoObjectCache.js) | `number` `string` | [first in first out][docs-fifo-cache] | JS object | | [`LruObjectCache`](./LruObjectCache.js) | `number` `string` | [least recently used][docs-lru-cache] | JS object | -| [`RrObjectCache`](./RrObjectCache.js) | `number` `string` | [random replacement][docs-rr-cache] | JS object | | [`FlatMapCache`](./FlatMapCache.js) | any | flat unlimited | [Map object][docs-mozilla-map] | | [`FifoMapCache`](./FifoMapCache.js) | any | [first in first out][docs-fifo-cache] | [Map object][docs-mozilla-map] | | [`LruMapCache`](./LruMapCache.js) | any | [least recently used][docs-lru-cache] | [Map object][docs-mozilla-map] | diff --git a/src/cache/RrMapCache.js b/src/cache/RrMapCache.js index cde4ad65..26fd685f 100644 --- a/src/cache/RrMapCache.js +++ b/src/cache/RrMapCache.js @@ -1,5 +1,4 @@ import validateCacheSize from './util/validateCacheSize'; -import isStringOrNumber from './util/isStringOrNumber'; export default class RrMapCache { constructor({cacheSize} = {}) { @@ -8,12 +7,11 @@ export default class RrMapCache { this._cacheSize = cacheSize; } set(key, selectorFn) { - if (this._cacheLength >= this._cacheSize) { + if (this._cache.size >= this._cacheSize) { this._randomReplace(key, selectorFn); } else { this._cache.set(key, selectorFn); - this._cacheKeys[this._cacheLength] = key; - this._cacheLength++; + this._cacheKeys[this._cache.size] = key; } } get(key) { @@ -23,17 +21,15 @@ export default class RrMapCache { const index = this._cacheKeys.indexOf(key); // O(1) if (index > -1) { delete this._cache.delete(key); - this._cacheLength--; - this._cacheKeys[index] = this._cacheKeys[this._cacheLength]; + this._cacheKeys[index] = this._cacheKeys[this._cache.size]; } } clear() { this._cache = new Map(); this._cacheKeys = []; - this._cacheLength = 0; } _randomReplace(newKey, newValue) { - const index = Math.floor(Math.random() * this._cacheLength); + const index = Math.floor(Math.random() * this._cache.size); this._cache.delete(this._cacheKeys[index]); this._cacheKeys[index] = newKey; this._cache.set(newKey, newValue); diff --git a/src/cache/RrObjectCache.js b/src/cache/RrObjectCache.js deleted file mode 100644 index 6440ed48..00000000 --- a/src/cache/RrObjectCache.js +++ /dev/null @@ -1,44 +0,0 @@ -import validateCacheSize from './util/validateCacheSize'; -import isStringOrNumber from './util/isStringOrNumber'; - -export default class RrObjectCache { - constructor({cacheSize} = {}) { - validateCacheSize(cacheSize); - this.clear(); - this._cacheSize = cacheSize; - } - set(key, selectorFn) { - if (this._cacheLength >= this._cacheSize) { - this._randomReplace(key, selectorFn); - } else { - this._cache[key] = selectorFn; - this._cacheKeys[this._cacheLength] = key; - this._cacheLength++; - } - } - get(key) { - return this._cache[key]; - } - remove(key) { - const index = this._cacheKeys.indexOf(key); // O(1) - if (index > -1) { - delete this._cache[key]; - this._cacheLength--; - this._cacheKeys[index] = this._cacheKeys[this._cacheLength]; - } - } - clear() { - this._cache = {}; - this._cacheKeys = []; - this._cacheLength = 0; - } - _randomReplace(newKey, newValue) { - const index = Math.floor(Math.random() * this._cacheLength); - delete this._cache[this._cacheKeys[index]]; - this._cacheKeys[index] = newKey; - this._cache[newKey] = newValue; - } - isValidCacheKey(cacheKey) { - return isStringOrNumber(cacheKey); - } -} diff --git a/src/cache/__tests__/RrObjectCache.spec.js b/src/cache/__tests__/RrObjectCache.spec.js deleted file mode 100644 index 4d69bf55..00000000 --- a/src/cache/__tests__/RrObjectCache.spec.js +++ /dev/null @@ -1,12 +0,0 @@ -import {RrObjectCache as CacheObject} from '../../../src/index'; -import testBasicBehavior from '../__util__/testBasicBehavior'; -import testRrBehavior from '../__util__/testRrBehavior'; -import testCacheSizeOptionValidation from '../__util__/testCacheSizeOptionValidation'; -import testObjectCacheKeyBehavior from '../__util__/testObjectCacheKeyBehavior'; - -describe('RrObjectCache', () => { - testBasicBehavior(CacheObject, {cacheSize: 10}); - testRrBehavior(CacheObject); - testCacheSizeOptionValidation(CacheObject); - testObjectCacheKeyBehavior(CacheObject, {cacheSize: 10}); -}); diff --git a/src/index.d.ts b/src/index.d.ts index c417aeb6..749886cf 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -4327,15 +4327,6 @@ export class LruObjectCache implements ICacheObject { isValidCacheKey(key: ObjectCacheKey): boolean; } -export class RrObjectCache implements ICacheObject { - constructor(options: {cacheSize: number}); - set(key: ObjectCacheKey, selectorFn: any): void; - get(key: ObjectCacheKey): any; - remove(key: ObjectCacheKey): void; - clear(): void; - isValidCacheKey(key: ObjectCacheKey): boolean; -} - export class FlatMapCache implements ICacheObject { set(key: any, selectorFn: any): void; get(key: any): any; diff --git a/src/index.js b/src/index.js index c0f19317..4830a675 100644 --- a/src/index.js +++ b/src/index.js @@ -11,5 +11,4 @@ export {default as LruObjectCache} from './cache/LruObjectCache'; export {default as FlatMapCache} from './cache/FlatMapCache'; export {default as FifoMapCache} from './cache/FifoMapCache'; export {default as LruMapCache} from './cache/LruMapCache'; -export {default as RrObjectCache} from './cache/RrObjectCache'; export {default as RrMapCache} from './cache/RrMapCache';