-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed the unnecessary else after return as per the linting suggestion.
- Loading branch information
1 parent
14569ce
commit 924c392
Showing
1 changed file
with
34 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,52 @@ | ||
export class Cache { | ||
private inMemoryDb: Map<string, { value: any; expiry: number; }>; | ||
private inMemoryDb: Map< | ||
string, | ||
{ | ||
value: any; | ||
expiry: number; | ||
} | ||
>; | ||
private static instance: Cache; | ||
|
||
private constructor() { | ||
this.inMemoryDb = new Map(); | ||
this.inMemoryDb = new Map< | ||
string, | ||
{ | ||
value: any; | ||
expiry: number; | ||
} | ||
>(); | ||
} | ||
|
||
static getInstance(): Cache { | ||
if (!Cache.instance) { | ||
Cache.instance = new Cache(); | ||
static getInstance() { | ||
if (!this.instance) { | ||
this.instance = new Cache(); | ||
} | ||
return Cache.instance; | ||
|
||
return this.instance; | ||
} | ||
|
||
set(type: string, args: string[], value: any, expiryInSeconds: number = parseInt(process.env.CACHE_EXPIRE_S || '100', 10)): void { | ||
const currentTime = new Date().getTime(); | ||
const expiry = currentTime + expiryInSeconds * 1000; | ||
this.inMemoryDb.set(`${type} ${JSON.stringify(args)}`, { value, expiry }); | ||
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() + expirySeconds * 1000, | ||
}); | ||
} | ||
|
||
get(type: string, args: string[]): any | null { | ||
get(type: string, args: string[]) { | ||
const key = `${type} ${JSON.stringify(args)}`; | ||
const cacheEntry = this.inMemoryDb.get(key); | ||
if (cacheEntry && cacheEntry.expiry > new Date().getTime()) { | ||
return cacheEntry.value; | ||
} else { | ||
this.inMemoryDb.delete(key); | ||
return null; | ||
const entry = this.inMemoryDb.get(key); | ||
if (!entry) { | ||
return null; | ||
} | ||
if (new Date().getTime() > entry.expiry) { | ||
this.inMemoryDb.delete(key); | ||
return null; | ||
} | ||
return entry.value; | ||
} | ||
|
||
evict(type: string, args: string[]): void { | ||
const key = `${type} ${JSON.stringify(args)}`; | ||
this.inMemoryDb.delete(key); | ||
} | ||
evict() { | ||
|
||
clearAll(): void { | ||
this.inMemoryDb.clear(); | ||
} | ||
} |