Add cache clearing of moderation requests
This removes the memory leak of not removing the moderation api cache
This commit is contained in:
parent
33a16bd629
commit
01231151b3
1 changed files with 29 additions and 1 deletions
|
@ -1,10 +1,13 @@
|
|||
import { Collection, InteractionResponse, Message } from "discord.js";
|
||||
import { Collection, InteractionResponse, Message, SnowflakeUtil } from "discord.js";
|
||||
|
||||
import { openai } from "./index";
|
||||
import { formatRequestOrResponse } from "./toOpenAIMessages";
|
||||
import config from "./config";
|
||||
|
||||
export default class Moderation {
|
||||
/** Represents cache of messages that have been checked aganist OpenAI moderation API. */
|
||||
private static cache = new Collection<string, boolean>();
|
||||
private static worker: NodeJS.Timeout | null = null;
|
||||
|
||||
public static async checkMessage(message: Message | InteractionResponse): Promise<boolean> {
|
||||
if (this.cache.has(message.id)) {
|
||||
|
@ -43,4 +46,29 @@ export default class Moderation {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static removeExpiredCacheEntries() {
|
||||
const now = Date.now();
|
||||
for (const i of this.cache.keys()) {
|
||||
if (now - SnowflakeUtil.timestampFrom(i) >= config.limits.time * 2) {
|
||||
this.cache.delete(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
public static startCacheClearWorker() {
|
||||
if (this.worker) return;
|
||||
this.worker = setInterval(() => {
|
||||
this.removeExpiredCacheEntries();
|
||||
},
|
||||
1000 * 60 * 15 /* 15 minutes */,
|
||||
);
|
||||
}
|
||||
|
||||
public static stopCacheClearWorker() {
|
||||
if (!this.worker) return;
|
||||
clearInterval(this.worker);
|
||||
this.worker = null;
|
||||
}
|
||||
}
|
||||
|
||||
Moderation.startCacheClearWorker();
|
||||
|
|
Loading…
Reference in a new issue