simple-node-memory-cache
Version:
In-memory object cache written in typescript for Node that supports multiple eviction strategies.
24 lines (19 loc) • 673 B
text/typescript
import { SimpleCache } from "../cacheBase/Cache";
import { ICacheImp } from "../interfaces/ICacheImp";
export class SimpleLIFO<T, U> extends SimpleCache<T, U> implements ICacheImp<T, U> {
private keyArray: T[] = [];
public get(key: T): [U, number, number] | undefined {
return this.cache.get(key);
}
public set(key: T, value: U): Map<T, [U, number, number]> {
if(this.keyArray.length >= this._maxObjectsInCache && !this.cache.has(key)) {
this.cache.delete(this.keyArray.pop());
}
this.keyArray.push(key);
return this.cache.set(key, [value, Date.now(), 0]);
}
public clear(): void {
super.clear();
this.keyArray = [];
}
}