Skip to content

Commit

Permalink
RrObjectCache removed
Browse files Browse the repository at this point in the history
  • Loading branch information
jchook committed Nov 3, 2023
1 parent 2d7ae22 commit 80c37d1
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 140 deletions.
69 changes: 4 additions & 65 deletions jest/__snapshots__/bundles-snapshot.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
};
Expand All @@ -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]);
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/cache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] |
Expand Down
12 changes: 4 additions & 8 deletions src/cache/RrMapCache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import validateCacheSize from './util/validateCacheSize';
import isStringOrNumber from './util/isStringOrNumber';

export default class RrMapCache {
constructor({cacheSize} = {}) {
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
44 changes: 0 additions & 44 deletions src/cache/RrObjectCache.js

This file was deleted.

12 changes: 0 additions & 12 deletions src/cache/__tests__/RrObjectCache.spec.js

This file was deleted.

9 changes: 0 additions & 9 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit 80c37d1

Please sign in to comment.