Created
December 14, 2022 09:54
-
-
Save MarcelloDiSimone/613614c0d0ad9ad9f7acbf7cc83ab890 to your computer and use it in GitHub Desktop.
A Cache for data with a configurable ttl, useful for filtering duplicate events
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
class ExpiringDataCache { | |
private _cache: any[] = []; | |
public expirationTime = 100; | |
set cache(value: any) { | |
this.expiredCache.push({value, ttl: Date.now() + this.expirationTime}); | |
} | |
get cache() { | |
return this.expiredCache.map(item => item.value); | |
} | |
private get expiredCache() { | |
return (this._cache = this._cache.filter(entry => entry.ttl > Date.now())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment