hypertune
Version:
[Hypertune](https://www.hypertune.com/) is the most flexible platform for feature flags, A/B testing, analytics and app configuration. Built with full end-to-end type-safety, Git-style version control and local, synchronous, in-memory flag evaluation. Opt
40 lines (30 loc) • 833 B
text/typescript
// LRUCache relies on the fact that Map stores keys in their insertion order.
export default class LRUCache<T> {
private readonly maxSize: number;
private cache: Map<string, T>;
constructor(maxSize: number) {
this.maxSize = maxSize;
this.cache = new Map<string, T>();
}
get(key: string): T | null {
if (!this.cache.has(key)) {
return null;
}
const value = this.cache.get(key) as T;
// Refresh key.
this.cache.delete(key);
this.cache.set(key, value);
return value;
}
set(key: string, value: T): void {
this.cache.delete(key);
while (this.cache.size >= this.maxSize) {
// Evict last used key.
this.cache.delete(this.cache.keys().next().value!);
}
this.cache.set(key, value);
}
purge(): void {
this.cache = new Map<string, T>();
}
}