-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Replace LRU Dependency With Custom Implementation (#1659)
* build: remove `lru-cache` * feat: Add Custom LRU * fix: remove log * refactor: Use `Map` instead of Linked List * chore: Move `LRU` to a `util.ts` * chore: Rename for clarity * test: Add `LRUCache` tests * chore: use utility LRUCache * fix: use manual Promise `setTimeout` 'timers/promises' is not available in Node 14 * chore: lint * docs: LRUCache
- Loading branch information
Showing
4 changed files
with
181 additions
and
6 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,110 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
export interface LRUCacheOptions { | ||
/** | ||
* The maximum number of items to cache. | ||
*/ | ||
capacity: number; | ||
/** | ||
* An optional max age for items in milliseconds. | ||
*/ | ||
maxAge?: number; | ||
} | ||
|
||
/** | ||
* A simple LRU cache utility. | ||
* Not meant for external usage. | ||
* | ||
* @experimental | ||
* @internal | ||
*/ | ||
export class LRUCache<T> { | ||
readonly capacity: number; | ||
|
||
/** | ||
* Maps are in order. Thus, the older item is the first item. | ||
* | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map} | ||
*/ | ||
#cache = new Map<string, {lastAccessed: number; value: T}>(); | ||
maxAge?: number; | ||
|
||
constructor(options: LRUCacheOptions) { | ||
this.capacity = options.capacity; | ||
this.maxAge = options.maxAge; | ||
} | ||
|
||
/** | ||
* Moves the key to the end of the cache. | ||
* | ||
* @param key the key to move | ||
* @param value the value of the key | ||
*/ | ||
#moveToEnd(key: string, value: T) { | ||
this.#cache.delete(key); | ||
this.#cache.set(key, { | ||
value, | ||
lastAccessed: Date.now(), | ||
}); | ||
} | ||
|
||
/** | ||
* Add an item to the cache. | ||
* | ||
* @param key the key to upsert | ||
* @param value the value of the key | ||
*/ | ||
set(key: string, value: T) { | ||
this.#moveToEnd(key, value); | ||
this.#evict(); | ||
} | ||
|
||
/** | ||
* Get an item from the cache. | ||
* | ||
* @param key the key to retrieve | ||
*/ | ||
get(key: string): T | undefined { | ||
const item = this.#cache.get(key); | ||
if (!item) return; | ||
|
||
this.#moveToEnd(key, item.value); | ||
this.#evict(); | ||
|
||
return item.value; | ||
} | ||
|
||
/** | ||
* Maintain the cache based on capacity and TTL. | ||
*/ | ||
#evict() { | ||
const cutoffDate = this.maxAge ? Date.now() - this.maxAge : 0; | ||
|
||
/** | ||
* Because we know Maps are in order, this item is both the | ||
* last item in the list (capacity) and oldest (maxAge). | ||
*/ | ||
let oldestItem = this.#cache.entries().next(); | ||
|
||
while ( | ||
!oldestItem.done && | ||
(this.#cache.size > this.capacity || // too many | ||
oldestItem.value[1].lastAccessed < cutoffDate) // too old | ||
) { | ||
this.#cache.delete(oldestItem.value[0]); | ||
oldestItem = this.#cache.entries().next(); | ||
} | ||
} | ||
} |
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,67 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {strict as assert} from 'assert'; | ||
|
||
import {LRUCache} from '../src/util'; | ||
|
||
describe('util', () => { | ||
describe('LRUCache', () => { | ||
it('should set and get a cached item', () => { | ||
const expected = 'value'; | ||
const lru = new LRUCache({capacity: 5}); | ||
lru.set('sample', expected); | ||
|
||
assert.equal(lru.get('sample'), expected); | ||
}); | ||
|
||
it('should evict oldest items when over capacity', () => { | ||
const capacity = 5; | ||
const overCapacity = 2; | ||
|
||
const lru = new LRUCache({capacity}); | ||
|
||
for (let i = 0; i < capacity + overCapacity; i++) { | ||
lru.set(`${i}`, i); | ||
} | ||
|
||
// the first few shouldn't be there | ||
for (let i = 0; i < overCapacity; i++) { | ||
assert.equal(lru.get(`${i}`), undefined); | ||
} | ||
|
||
// the rest should be there | ||
for (let i = overCapacity; i < capacity + overCapacity; i++) { | ||
assert.equal(lru.get(`${i}`), i); | ||
} | ||
}); | ||
|
||
it('should evict items older than a supplied `maxAge`', async () => { | ||
const maxAge = 50; | ||
|
||
const lru = new LRUCache({capacity: 5, maxAge}); | ||
|
||
lru.set('first', 1); | ||
lru.set('second', 2); | ||
|
||
await new Promise(res => setTimeout(res, maxAge + 1)); | ||
|
||
lru.set('third', 3); | ||
|
||
assert.equal(lru.get('first'), undefined); | ||
assert.equal(lru.get('second'), undefined); | ||
assert.equal(lru.get('third'), 3); | ||
}); | ||
}); | ||
}); |