Skip to content

Commit

Permalink
Removed the unnecessary else after return as per the linting suggestion.
Browse files Browse the repository at this point in the history
  • Loading branch information
TanmayDhobale authored Mar 11, 2024
1 parent 14569ce commit 924c392
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions src/db/Cache.ts
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();
}
}

0 comments on commit 924c392

Please sign in to comment.