-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
291 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import validateCacheSize from './util/validateCacheSize'; | ||
import isStringOrNumber from './util/isStringOrNumber'; | ||
|
||
export default class RrMapCache { | ||
constructor({cacheSize} = {}) { | ||
validateCacheSize(cacheSize); | ||
this.clear(); | ||
this._cacheSize = cacheSize; | ||
} | ||
set(key, selectorFn) { | ||
if (this._cacheLength >= this._cacheSize) { | ||
this._randomReplace(key, selectorFn); | ||
} else { | ||
this._cache.set(key, selectorFn); | ||
this._cacheKeys[this._cacheLength] = key; | ||
this._cacheLength++; | ||
} | ||
} | ||
get(key) { | ||
return this._cache.get(key); | ||
} | ||
remove(key) { | ||
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]; | ||
} | ||
} | ||
clear() { | ||
this._cache = new Map(); | ||
this._cacheKeys = []; | ||
this._cacheLength = 0; | ||
} | ||
_randomReplace(newKey, newValue) { | ||
const index = Math.floor(Math.random() * this._cacheLength); | ||
this._cache.delete(this._cacheKeys[index]); | ||
this._cacheKeys[index] = newKey; | ||
this._cache.set(newKey, newValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {RrMapCache as CacheObject} from '../../../src/index'; | ||
import testBasicBehavior from '../__util__/testBasicBehavior'; | ||
import testRrBehavior from '../__util__/testRrBehavior'; | ||
import testCacheSizeOptionValidation from '../__util__/testCacheSizeOptionValidation'; | ||
import testMapCacheKeyBehavior from '../__util__/testMapCacheKeyBehavior'; | ||
|
||
describe('RrMapCache', () => { | ||
testBasicBehavior(CacheObject, {cacheSize: 10}); | ||
testRrBehavior(CacheObject); | ||
testCacheSizeOptionValidation(CacheObject); | ||
testMapCacheKeyBehavior(CacheObject, {cacheSize: 10}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
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}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import fillCacheWith from './fillCacheWith'; | ||
|
||
function testRrBehavior(CacheObject) { | ||
describe('RR cache behavior', () => { | ||
it('limits cache size by removing a random item', () => { | ||
const cache = new CacheObject({cacheSize: 5}); | ||
const entries = [1, 2, 3, 4, 5, 6]; | ||
const get = cache.get.bind(cache); | ||
fillCacheWith(cache, entries); | ||
expect(entries.every(get)).toBe(false); | ||
}); | ||
|
||
it('shrinks when removing existing items manually', () => { | ||
const cache = new CacheObject({cacheSize: 5}); | ||
const entries = [1, 2, 3, 4, 5]; | ||
const get = cache.get.bind(cache); | ||
fillCacheWith(cache, entries); | ||
expect(entries.every(get)).toBe(true); | ||
cache.remove('non-existant'); | ||
cache.remove(5); | ||
expect(cache.get(5)).toBeUndefined(); | ||
cache.set(5, 5); | ||
expect(entries.every(get)).toBe(true); | ||
cache.set(6, 6); | ||
expect(entries.every(get)).toBe(false); | ||
}); | ||
}); | ||
} | ||
|
||
export default testRrBehavior; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters