Created
May 5, 2021 08:43
-
-
Save mkhmylife/24856924135b86c4fb6face2d7559a92 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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