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 | 1x 1x 1x 17x 17x 2x 2x 1109x 1109x 38x 7x 31x 31x 31x 3x 31x 34x 9x 25x 25x 25x 1239x 1239x 1239x | import {LruMap} from './LruMap';
/**
* An {@link LruMap} where each entry additionally carries an absolute expiry
* deadline, in the same units as the `now` timestamps supplied to reads
* (milliseconds since the Unix epoch, by default).
*/
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);
}
/**
* @param now Current time, defaults to `Date.now()`. Entries with a deadline
* strictly below it are treated as missing and are removed.
*/
public has(key: K, now: number = Date.now()): 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;
}
/**
* @param now Current time, defaults to `Date.now()`. Entries with a deadline
* strictly below it are treated as missing and are removed.
*/
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;
}
/**
* @param expiry Absolute deadline after which the entry expires, defaults to
* `Infinity` (never expires). For a relative TTL use `Date.now() + ttl`.
*/
public set(key: K, value: V, expiry: number = Infinity): this {
this.expiry.set(key, expiry);
super.set(key, value);
return this;
}
}
|