-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.js
More file actions
126 lines (109 loc) · 2.78 KB
/
Copy pathcache.js
File metadata and controls
126 lines (109 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* In-Memory Cache Layer for AI Summaries
* Reduces API calls by caching summaries for 24 hours
* Falls back to database persistence
*/
class SummaryCache {
constructor() {
this.cache = new Map();
this.ttl = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
}
/**
* Generate cache key from user bookmarks
* @param {number} userId - User FID
* @returns {string} - Cache key
*/
getCacheKey(userId) {
return `summary_${userId}`;
}
/**
* Get cached summary
* @param {number} userId - User FID
* @returns {object|null} - Cached summary or null if expired
*/
get(userId) {
const key = this.getCacheKey(userId);
const entry = this.cache.get(key);
if (!entry) {
return null;
}
// Check if cache is expired
const now = Date.now();
if (now - entry.timestamp > this.ttl) {
this.cache.delete(key);
return null;
}
return entry.data;
}
/**
* Set cache entry
* @param {number} userId - User FID
* @param {object} data - Summary data { summary, castCount, timestamp }
*/
set(userId, data) {
const key = this.getCacheKey(userId);
this.cache.set(key, {
data,
timestamp: Date.now()
});
// Log cache hit
console.log(`[CACHE] Set summary for user ${userId}`);
}
/**
* Invalidate cache for user (after new bookmarks)
* @param {number} userId - User FID
*/
invalidate(userId) {
const key = this.getCacheKey(userId);
if (this.cache.has(key)) {
this.cache.delete(key);
console.log(`[CACHE] Invalidated cache for user ${userId}`);
}
}
/**
* Get cache statistics
* @returns {object} - Cache stats { size, entries, memoryUsage }
*/
getStats() {
const entries = Array.from(this.cache.entries());
return {
size: this.cache.size,
entries: entries.map(([key, value]) => ({
key,
cached_at: new Date(value.timestamp).toISOString(),
expires_at: new Date(value.timestamp + this.ttl).toISOString()
})),
memoryUsage: JSON.stringify(Array.from(this.cache.values())).length
};
}
/**
* Clear all cache
*/
clear() {
this.cache.clear();
console.log('[CACHE] Cleared all cache');
}
/**
* Clear expired entries
*/
clearExpired() {
const now = Date.now();
let cleared = 0;
for (const [key, entry] of this.cache.entries()) {
if (now - entry.timestamp > this.ttl) {
this.cache.delete(key);
cleared++;
}
}
if (cleared > 0) {
console.log(`[CACHE] Cleared ${cleared} expired entries`);
}
}
}
// Create singleton instance
const summaryCache = new SummaryCache();
// Auto-cleanup expired entries every 6 hours
setInterval(() => {
summaryCache.clearExpired();
}, 6 * 60 * 60 * 1000);
module.exports = summaryCache;