diff --git a/package.json b/package.json index 8d17b8c..dde0ba5 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "types": "./dist/index.d.ts", "scripts": { "prebuild": "tslint -c tslint.json -p tsconfig.json --fix", - "build": "rm -rf dist && tsc", + "build": "tsc", "prepublish": "npm test", "test": "npm run build && jest --coverage" }, diff --git a/src/cache.test.ts b/src/cache.test.ts index fdbc2ac..c701c74 100644 --- a/src/cache.test.ts +++ b/src/cache.test.ts @@ -4,6 +4,7 @@ import MemoryStore from "../src/memoryStore"; const cache = new Cache({ namespace: "test", policy: { + stdTTL: 0, maxEntries: 1 }, backend: MemoryStore diff --git a/src/cache.ts b/src/cache.ts index 3f7153a..41831e6 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -3,6 +3,7 @@ export interface ICacheOptions { backend: any; namespace: string; policy: ICachePolicy; + prunecallback? : ((keys : string[]) => void); } export interface ICachePolicy { @@ -14,11 +15,13 @@ export default class Cache { protected backend: any; protected namespace: string; protected policy: ICachePolicy; + protected prunecallback? : ((keys : string[]) => void); constructor(options: ICacheOptions) { this.namespace = options.namespace; this.backend = options.backend; this.policy = options.policy; + this.prunecallback = options.prunecallback; let ttl = this.policy.stdTTL; if (!ttl || typeof(ttl) !== 'number') { ttl = 0; @@ -53,6 +56,10 @@ export default class Cache { await Promise.all(removePromises); + if(this.prunecallback !== undefined && victimList.length > 0){ + this.prunecallback(victimList); + } + const survivorList = lru.slice(victimCount); return this.setLRU(survivorList); }