mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-18 23:55:02 +00:00
Initial work on new automod
This commit is contained in:
parent
8914fe83c4
commit
846e623e3e
6 changed files with 888 additions and 5 deletions
60
src/SimpleCache.ts
Normal file
60
src/SimpleCache.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
import Timeout = NodeJS.Timeout;
|
||||
|
||||
const CLEAN_INTERVAL = 1000;
|
||||
|
||||
export class SimpleCache {
|
||||
protected readonly retentionTime;
|
||||
protected cleanTimeout: Timeout;
|
||||
protected unloaded: boolean;
|
||||
|
||||
protected store: Map<string, { remove_at: number; value: any }>;
|
||||
|
||||
constructor(retentionTime) {
|
||||
this.retentionTime = retentionTime;
|
||||
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) {
|
||||
this.store.set(key, {
|
||||
remove_at: Date.now() + this.retentionTime,
|
||||
value,
|
||||
});
|
||||
}
|
||||
|
||||
get(key: string) {
|
||||
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