Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | 35x 35x 35x 26x 26x 15x 1x 1x 14x 14x 14x 22x 22x 2x 2x 22x 3x 3x 1x 1x 1x 1x 6x | // src/cache/lru-cache.ts
// LRU cache implementation
import { Cache } from './cache';
export class LRUCache<TKey extends string, TValue> implements Cache<TKey, TValue> {
private cache: Map<TKey, { value: TValue; timestamp: number }> = new Map();
private maxSize: number;
private ttl: number;
constructor(maxSize: number, ttl: number) {
this.maxSize = maxSize;
this.ttl = ttl;
}
get(key: TKey): TValue | undefined {
const entry = this.cache.get(key);
if (!entry) return undefined;
// Check TTL
if (Date.now() - entry.timestamp > this.ttl) {
this.cache.delete(key);
return undefined;
}
// Move to end (most recently used)
this.cache.delete(key);
this.cache.set(key, entry);
return entry.value;
}
set(key: TKey, value: TValue): void {
// If key exists, delete it first to update position
Iif (this.cache.has(key)) {
this.cache.delete(key);
}
// If at capacity, delete oldest
else if (this.cache.size >= this.maxSize) {
const oldestKey = this.cache.keys().next().value;
Eif (oldestKey) this.cache.delete(oldestKey);
}
this.cache.set(key, { value, timestamp: Date.now() });
}
has(key: TKey): boolean {
const entry = this.cache.get(key);
if (!entry) return false;
// Check TTL
Iif (Date.now() - entry.timestamp > this.ttl) {
this.cache.delete(key);
return false;
}
return true;
}
delete(key: TKey): void {
this.cache.delete(key);
}
clear(): void {
this.cache.clear();
}
get size(): number {
return this.cache.size;
}
}
|