Reorganize project. Add folder for shared code between backend/dashboard. Switch from jest to ava for tests.
This commit is contained in:
parent
80a82fe348
commit
16111bbe84
162 changed files with 11056 additions and 9900 deletions
69
backend/src/SimpleCache.ts
Normal file
69
backend/src/SimpleCache.ts
Normal file
|
@ -0,0 +1,69 @@
|
|||
import Timeout = NodeJS.Timeout;
|
||||
|
||||
const CLEAN_INTERVAL = 1000;
|
||||
|
||||
export class SimpleCache<T = any> {
|
||||
protected readonly retentionTime: number;
|
||||
protected readonly maxItems: number;
|
||||
|
||||
protected cleanTimeout: Timeout;
|
||||
protected unloaded: boolean;
|
||||
|
||||
protected store: Map<string, { remove_at: number; value: T }>;
|
||||
|
||||
constructor(retentionTime: number, maxItems?: number) {
|
||||
this.retentionTime = retentionTime;
|
||||
this.maxItems = maxItems;
|
||||
|
||||
this.store = new Map();
|
||||
}
|
||||
|
||||
unload() {
|
||||
this.unloaded = true;
|
||||
clearTimeout(this.cleanTimeout);
|
||||
}
|
||||
|
||||
cleanLoop() {
|
||||
const now = Date.now();
|
||||
for (const [key, info] of this.store.entries()) {
|
||||
if (now >= info.remove_at) {
|
||||
this.store.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.unloaded) {
|
||||
this.cleanTimeout = setTimeout(() => this.cleanLoop(), CLEAN_INTERVAL);
|
||||
}
|
||||
}
|
||||
|
||||
set(key: string, value: T) {
|
||||
this.store.set(key, {
|
||||
remove_at: Date.now() + this.retentionTime,
|
||||
value,
|
||||
});
|
||||
|
||||
if (this.maxItems && this.store.size > this.maxItems) {
|
||||
const keyToDelete = this.store.keys().next().value;
|
||||
this.store.delete(keyToDelete);
|
||||
}
|
||||
}
|
||||
|
||||
get(key: string): T {
|
||||
const info = this.store.get(key);
|
||||
if (!info) return null;
|
||||
|
||||
return info.value;
|
||||
}
|
||||
|
||||
has(key: string) {
|
||||
return this.store.has(key);
|
||||
}
|
||||
|
||||
delete(key: string) {
|
||||
this.store.delete(key);
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.store.clear();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue