-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): Remove
lru_map
dependency (#9300)
- Loading branch information
Showing
13 changed files
with
113 additions
and
44 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
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
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
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,60 @@ | ||
/** A simple Least Recently Used map */ | ||
export class LRUMap<K, V> { | ||
private readonly _cache: Map<K, V>; | ||
|
||
public constructor(private readonly _maxSize: number) { | ||
this._cache = new Map<K, V>(); | ||
} | ||
|
||
/** Get the current size of the cache */ | ||
public get size(): number { | ||
return this._cache.size; | ||
} | ||
|
||
/** Get an entry or undefined if it was not in the cache. Re-inserts to update the recently used order */ | ||
public get(key: K): V | undefined { | ||
const value = this._cache.get(key); | ||
if (value === undefined) { | ||
return undefined; | ||
} | ||
// Remove and re-insert to update the order | ||
this._cache.delete(key); | ||
this._cache.set(key, value); | ||
return value; | ||
} | ||
|
||
/** Insert an entry and evict an older entry if we've reached maxSize */ | ||
public set(key: K, value: V): void { | ||
if (this._cache.size >= this._maxSize) { | ||
// keys() returns an iterator in insertion order so keys().next() gives us the oldest key | ||
this._cache.delete(this._cache.keys().next().value); | ||
} | ||
this._cache.set(key, value); | ||
} | ||
|
||
/** Remove an entry and return the entry if it was in the cache */ | ||
public remove(key: K): V | undefined { | ||
const value = this._cache.get(key); | ||
if (value) { | ||
this._cache.delete(key); | ||
} | ||
return value; | ||
} | ||
|
||
/** Clear all entries */ | ||
public clear(): void { | ||
this._cache.clear(); | ||
} | ||
|
||
/** Get all the keys */ | ||
public keys(): Array<K> { | ||
return Array.from(this._cache.keys()); | ||
} | ||
|
||
/** Get all the values */ | ||
public values(): Array<V> { | ||
const values: V[] = []; | ||
this._cache.forEach(value => values.push(value)); | ||
return values; | ||
} | ||
} |
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 { LRUMap } from '../src/lru'; | ||
|
||
describe('LRUMap', () => { | ||
test('evicts older entries when reaching max size', () => { | ||
const map = new LRUMap<string, string>(3); | ||
map.set('a', '1'); | ||
map.set('b', '2'); | ||
map.set('c', '3'); | ||
map.set('d', '4'); | ||
map.set('e', '5'); | ||
|
||
expect(map.keys()).toEqual(['c', 'd', 'e']); | ||
}); | ||
|
||
test('updates last used when calling get', () => { | ||
const map = new LRUMap<string, string>(3); | ||
map.set('a', '1'); | ||
map.set('b', '2'); | ||
map.set('c', '3'); | ||
|
||
map.get('a'); | ||
|
||
map.set('d', '4'); | ||
map.set('e', '5'); | ||
|
||
expect(map.keys()).toEqual(['a', 'd', 'e']); | ||
}); | ||
|
||
test('removes and returns entry', () => { | ||
const map = new LRUMap<string, string>(3); | ||
map.set('a', '1'); | ||
map.set('b', '2'); | ||
map.set('c', '3'); | ||
map.set('d', '4'); | ||
map.set('e', '5'); | ||
|
||
expect(map.remove('c')).toEqual('3'); | ||
|
||
expect(map.keys()).toEqual(['d', 'e']); | ||
}); | ||
}); |
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