Skip to content

Instantly share code, notes, and snippets.

@MarcelloDiSimone
Created December 14, 2022 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcelloDiSimone/613614c0d0ad9ad9f7acbf7cc83ab890 to your computer and use it in GitHub Desktop.
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
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