-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcacheManager.js
More file actions
262 lines (228 loc) · 6.93 KB
/
cacheManager.js
File metadata and controls
262 lines (228 loc) · 6.93 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
* Centralized cache management utility for all components
*/
/**
* Cache manager for efficient data storage and retrieval
*/
export class CacheManager {
/**
* Create a new cache manager
* @param {Object} options - Cache options
* @param {number} [options.maxSize=500] - Maximum cache size
* @param {number} [options.ttl=3600000] - Default TTL in ms (1 hour)
*/
constructor(options = {}) {
this.maxSize = options.maxSize || 500
this.ttl = options.ttl || 3600000 // Default: 1 hour
this.cacheStore = new Map()
this.metrics = {
hits: 0,
misses: 0,
evictions: 0
}
}
/**
* Get an item from cache
* @param {string} key - Cache key
* @returns {any|null} Cached value or null if not found/expired
*/
get(key) {
const item = this.cacheStore.get(key)
if (!item) {
this.metrics.misses++
return null
}
// Check if item has expired
if (Date.now() - item.timestamp > (item.ttl || this.ttl)) {
this.cacheStore.delete(key)
this.metrics.misses++
return null
}
this.metrics.hits++
return item.value
}
/**
* Set an item in cache
* @param {string} key - Cache key
* @param {any} value - Value to cache
* @param {number} [customTtl] - Optional custom TTL for this item
*/
set(key, value, customTtl = null) {
// Enforce cache size limits
if (this.cacheStore.size >= this.maxSize) {
// Remove oldest entry
const oldestKey = [...this.cacheStore.entries()].sort((a, b) => a[1].timestamp - b[1].timestamp)[0][0]
this.cacheStore.delete(oldestKey)
this.metrics.evictions++
}
this.cacheStore.set(key, {
value,
timestamp: Date.now(),
ttl: customTtl
})
}
/**
* Generate a standardized cache key
* @param {string} type - Type of data (e.g., 'response', 'knowledge', 'research')
* @param {string} content - Content to derive key from
* @returns {string} Normalized cache key
*/
generateKey(type, content) {
// Normalize content by trimming, lowercasing, and removing extra spaces
const normalized = content.trim().toLowerCase().replace(/\s+/g, ' ')
// Create a stable key from the normalized content (limit length)
return `${type}:${normalized.substring(0, 100)}`
}
/**
* Check if key exists in cache and is not expired
* @param {string} key - Cache key to check
* @returns {boolean} Whether key exists and is valid
*/
has(key) {
const item = this.cacheStore.get(key)
if (!item) {
return false
}
// Check expiration
if (Date.now() - item.timestamp > (item.ttl || this.ttl)) {
this.cacheStore.delete(key)
return false
}
return true
}
/**
* Delete an item from cache
* @param {string} key - Cache key to delete
* @returns {boolean} Whether the item was found and deleted
*/
delete(key) {
return this.cacheStore.delete(key)
}
/**
* Clear all cached items
*/
clear() {
this.cacheStore.clear()
}
/**
* Reset metrics counters
*/
resetMetrics() {
this.metrics = {
hits: 0,
misses: 0,
evictions: 0
}
}
/**
* Get cache metrics
* @returns {Object} Current cache metrics
*/
getMetrics() {
const totalRequests = this.metrics.hits + this.metrics.misses
return {
size: this.cacheStore.size,
maxSize: this.maxSize,
hitRate: totalRequests === 0 ? 0 : this.metrics.hits / totalRequests,
evictionRate: totalRequests === 0 ? 0 : this.metrics.evictions / totalRequests,
...this.metrics
}
}
/**
* Get total number of requests (hits + misses)
* @returns {number} Total requests
*/
getTotalRequests() {
return this.metrics.hits + this.metrics.misses
}
/**
* Get cache statistics for monitoring
* @returns {Object} Detailed cache statistics
*/
getDetailedStats() {
return {
size: this.cacheStore.size,
maxSize: this.maxSize,
ttl: this.ttl,
hitRate: this.getTotalRequests() === 0 ? 0 : this.metrics.hits / this.getTotalRequests(),
evictionRate: this.getTotalRequests() === 0 ? 0 : this.metrics.evictions / this.getTotalRequests(),
metrics: { ...this.metrics },
oldestEntry: this._getOldestEntryAge(),
newestEntry: this._getNewestEntryAge(),
keyTypes: this._analyzeKeyTypes()
}
}
/**
* Get age of oldest cache entry in seconds
* @private
*/
_getOldestEntryAge() {
if (this.cacheStore.size === 0) {
return null
}
const oldest = [...this.cacheStore.entries()].reduce(
(oldest, [_, entry]) => {
return entry.timestamp < oldest.timestamp ? entry : oldest
},
{ timestamp: Date.now() }
)
return Math.floor((Date.now() - oldest.timestamp) / 1000)
}
/**
* Get age of newest cache entry in seconds
* @private
*/
_getNewestEntryAge() {
if (this.cacheStore.size === 0) {
return null
}
const newest = [...this.cacheStore.entries()].reduce(
(newest, [_, entry]) => {
return entry.timestamp > newest.timestamp ? entry : newest
},
{ timestamp: 0 }
)
return Math.floor((Date.now() - newest.timestamp) / 1000)
}
/**
* Analyze key types in the cache
* @private
*/
_analyzeKeyTypes() {
const types = {}
for (const key of this.cacheStore.keys()) {
const typeMatch = key.match(/^([^:]+):/)
const type = typeMatch ? typeMatch[1] : 'unknown'
types[type] = (types[type] || 0) + 1
}
return types
}
}
/**
* Create a new cache manager instance with custom settings
* @param {Object} options - Cache configuration options
* @returns {CacheManager} New cache manager instance
*/
export function createCache(options = {}) {
return new CacheManager(options)
}
// Global cache for general-purpose use
export const globalCache = new CacheManager({
maxSize: 1000,
ttl: 7200000 // 2 hours
})
// Response cache for API responses
export const responseCache = new CacheManager({
maxSize: 500,
ttl: 1800000 // 30 minutes
})
// Knowledge cache for knowledge base queries
export const knowledgeCache = new CacheManager({
maxSize: 300,
ttl: 3600000 // 1 hour
})
// Research cache for research agent results
export const researchCache = new CacheManager({
maxSize: 200,
ttl: 7200000 // 2 hours
})