All files LruTtlMap.ts

100% Statements 25/25
90% Branches 9/10
100% Functions 6/6
100% Lines 25/25

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  1x 1x 1x     9x 9x     1x 1x     2x 2x     31x 6x 25x 25x 25x 2x 25x     31x 8x 23x 23x 23x     33x 33x 33x
import {LruMap} from './LruMap';
 
export class LruTtlMap<K, V> extends LruMap<K, V> {
  private readonly expiry = new Map<K, number>();
 
  public clear(): void {
    this.expiry.clear();
    super.clear();
  }
 
  public delete(key: K): boolean {
    this.expiry.delete(key);
    return super.delete(key);
  }
 
  public has(key: K, now: number = 0): boolean {
    if (!super.has(key)) return false;
    const expiry = this.expiry.get(key) || 0;
    const expired = now > expiry;
    if (expired) this.delete(key);
    return !expired;
  }
 
  public get(key: K, now?: number): V | undefined {
    if (!this.has(key, now)) return undefined;
    const value = super.get(key)!;
    super.set(key, value);
    return value;
  }
 
  public set(key: K, value: V, expiry: number = Infinity): this {
    super.set(key, value);
    this.expiry.set(key, expiry);
    return this;
  }
}