Skip to content

Instantly share code, notes, and snippets.

@mkhmylife
Created May 5, 2021 08:43
Show Gist options
  • Save mkhmylife/24856924135b86c4fb6face2d7559a92 to your computer and use it in GitHub Desktop.
Save mkhmylife/24856924135b86c4fb6face2d7559a92 to your computer and use it in GitHub Desktop.
const CACHE_TTL = 1000 * 60; // 1 minute
export default class CacheService {
private memoryCache: string | undefined;
private cacheExpiredAt: number = Date.now();
public get(): any {
if (!this.memoryCache) {
throw new Error("Memory cache is empty");
}
return this.memoryCache;
}
public has(): boolean {
const now = Date.now();
return this.memoryCache !== undefined;
}
public expired(): boolean {
const now = Date.now();
return now >= this.cacheExpiredAt;
}
public set(t: any) {
this.memoryCache = t;
const now = Date.now();
this.cacheExpiredAt = now + CACHE_TTL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment