-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a hotcache class that we can use to cache the most common requests.
We are using this to cache some of the most used apis - like the statistics. This should reduce the CPU load in case of too many hits.
- Loading branch information
Showing
2 changed files
with
54 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class CacheObject { | ||
constructor(public value: any, public expiration: number) {} | ||
isExpired() { | ||
return this.expiration < Date.now() | ||
} | ||
} | ||
|
||
export class HotCache { | ||
private cache: { [key: string]: CacheObject } = {} | ||
|
||
get(key: string) { | ||
const cacheObject = this.cache[key] | ||
if (cacheObject && !cacheObject.isExpired()) { | ||
return cacheObject.value | ||
} | ||
return null | ||
} | ||
|
||
set(key: string, value: any, expiration: number) { | ||
this.cache[key] = new CacheObject(value, expiration) | ||
} | ||
} |
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