mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-12-22 02:11:29 +00:00
28 lines
580 B
TypeScript
28 lines
580 B
TypeScript
export class ObjectCache<T> extends Map<string, T> {
|
|
constructor(private limit: number) {
|
|
super();
|
|
}
|
|
|
|
public get(key: string): T | undefined {
|
|
return this.touch(key);
|
|
}
|
|
|
|
public set(key: string, value: T): this {
|
|
if (this.size === this.limit) {
|
|
this.delete(this.keys().next().value);
|
|
}
|
|
|
|
return this.touch(key, value), this;
|
|
}
|
|
|
|
private touch(key: string, value = super.get(key)) {
|
|
// Map keeps (re) insertion order according to ES6 spec
|
|
if (value) {
|
|
this.delete(key);
|
|
super.set(key, value);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
}
|