UNPKG

@linkedmink/multilevel-aging-cache

Version:

Package provides an interface to cache and persist data to Redis, MongoDB, memory

52 lines (44 loc) 1.65 kB
import { IAgingCacheDeleteStrategy } from './IAgingCacheWriteStrategy'; import { IAgingCacheWrite } from './IAgingCache'; import { AgingCacheWriteStrategy } from './AgingCacheWriteStrategy'; /** * Strategy to overwrite only if we're forced to */ export class RefreshAlwaysDeleteStrategy<TKey, TValue> extends AgingCacheWriteStrategy<TKey, TValue> implements IAgingCacheDeleteStrategy<TKey, TValue> { delete(key: TKey, force: boolean): Promise<IAgingCacheWrite<TValue>> { if (force) { return this.executeDelete(key); } return this.deleteConditionally(key); } evict(key: TKey, evictAtLevel?: number, force?: boolean): Promise<IAgingCacheWrite<TValue>> { if (force) { return this.executeDelete(key, evictAtLevel); } return this.deleteConditionally(key, evictAtLevel); } deleteConditionally(key: TKey, evictAtLevel?: number): Promise<IAgingCacheWrite<TValue>> { return this.hierarchy.getValueAtTopLevel(key).then(highestAgedValue => { if (!highestAgedValue) { return this.executeDelete(key, evictAtLevel); } return this.hierarchy.getValueAtBottomLevel(key).then(lowestAgedValue => { if ( lowestAgedValue && this.evictQueue.compare(lowestAgedValue.age, highestAgedValue.age) === 0 ) { return this.executeDelete(key, evictAtLevel); } this.logger.debug( `Delete deferred: key=${key},ageToSet=${ lowestAgedValue ? lowestAgedValue.age : 'null' },ageFound=${highestAgedValue.age}` ); return this.setFromHighestLevel(key, highestAgedValue); }); }); } }