Skip to content

Commit

Permalink
Merge pull request #199 from TanmayDhobale/patch-2
Browse files Browse the repository at this point in the history
Feature: Dynamic Cache Entry Expiry
  • Loading branch information
hkirat authored Mar 11, 2024
2 parents 97482fd + 924c392 commit c029acf
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/db/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class Cache {
}
>();
}

static getInstance() {
if (!this.instance) {
this.instance = new Cache();
Expand All @@ -25,26 +26,27 @@ export class Cache {
return this.instance;
}

set(type: string, args: string[], value: any) {
set(type: string, args: string[], value: any, expirySeconds: number = parseInt(process.env.CACHE_EXPIRE_S || '100', 10)) {
this.inMemoryDb.set(`${type} ${JSON.stringify(args)}`, {
value,
expiry:
new Date().getTime() +
parseInt(process.env.CACHE_EXPIRE_S || '100', 10) * 1000,
expiry: new Date().getTime() + expirySeconds * 1000,
});
}

get(type: string, args: string[]) {
const value = this.inMemoryDb.get(`${type} ${JSON.stringify(args)}`);
if (!value) {
const key = `${type} ${JSON.stringify(args)}`;
const entry = this.inMemoryDb.get(key);
if (!entry) {
return null;
}
if (new Date().getTime() > value.expiry) {
this.inMemoryDb.delete(`${type} ${JSON.stringify(args)}`);
if (new Date().getTime() > entry.expiry) {
this.inMemoryDb.delete(key);
return null;
}
return value.value;
return entry.value;
}

evict() {}
evict() {

}
}

0 comments on commit c029acf

Please sign in to comment.