···
178
-
* Sets up hourly purge of expired items
178
+
* Sets up scheduled tasks for cache maintenance
private setupPurgeSchedule() {
182
-
// Run purge every hour
182
+
// Run purge every hour at 45 minutes (only expired items, analytics cleanup)
schedule("45 * * * *", async () => {
await this.purgeExpiredItems();
185
+
await this.lazyUserCleanup();
188
+
// Schedule emoji updates daily at 3 AM to avoid peak hours
189
+
schedule("0 3 * * *", async () => {
190
+
console.log("Scheduled emoji update starting...");
191
+
if (this.onEmojiExpired) {
192
+
await this.onEmojiExpired();
193
+
console.log("Scheduled emoji update completed");
···
* @returns int indicating number of items purged
async purgeExpiredItems(): Promise<number> {
219
-
const result = this.db.run("DELETE FROM users WHERE expiration < ?", [
229
+
// Only purge emojis - users will use lazy loading with longer TTL
const result2 = this.db.run("DELETE FROM emojis WHERE expiration < ?", [
226
-
// Clean up old analytics data (older than 30 days)
234
+
// Clean up old analytics data (older than 30 days) - moved to off-peak hours
const thirtyDaysAgo = Date.now() - 30 * 24 * 60 * 60 * 1000;
228
-
this.db.run("DELETE FROM request_analytics WHERE timestamp < ?", [
236
+
const currentHour = new Date().getHours();
237
+
// Only run analytics cleanup during off-peak hours (2-6 AM)
238
+
if (currentHour >= 2 && currentHour < 6) {
239
+
this.db.run("DELETE FROM request_analytics WHERE timestamp < ?", [
242
+
console.log(`Analytics cleanup completed - removed records older than 30 days`);
245
+
// Emojis are now updated on schedule, not on expiration
246
+
return result2.changes;
232
-
if (this.onEmojiExpired) {
233
-
if (result2.changes > 0) {
234
-
this.onEmojiExpired();
250
+
* Lazy cleanup of truly expired users (older than 7 days) during off-peak hours only
251
+
* This runs much less frequently than the old aggressive purging
254
+
private async lazyUserCleanup(): Promise<void> {
255
+
const currentHour = new Date().getHours();
256
+
// Only run during off-peak hours (3-5 AM) and not every time
257
+
if (currentHour >= 3 && currentHour < 5 && Math.random() < 0.1) { // 10% chance
258
+
const sevenDaysAgo = Date.now() - 7 * 24 * 60 * 60 * 1000;
259
+
const result = this.db.run("DELETE FROM users WHERE expiration < ?", [
262
+
if (result.changes > 0) {
263
+
console.log(`Lazy user cleanup: removed ${result.changes} expired users`);
238
-
return result.changes + result2.changes;
···
expirationHours?: number,
const id = crypto.randomUUID();
339
+
// Users get longer TTL (7 days) for lazy loading, unless custom expiration specified
340
+
const userDefaultTTL = 7 * 24; // 7 days in hours
313
-
Date.now() + (expirationHours || this.defaultExpiration) * 3600000;
342
+
Date.now() + (expirationHours || userDefaultTTL) * 3600000;