55 lines
1002 B
JavaScript
55 lines
1002 B
JavaScript
// Simple in-memory cache with TTL support
|
|
class CacheManager {
|
|
constructor() {
|
|
this.cache = new Map();
|
|
}
|
|
|
|
set(key, value, ttlSeconds = 300) {
|
|
const expiresAt = Date.now() + (ttlSeconds * 1000);
|
|
this.cache.set(key, { value, expiresAt });
|
|
}
|
|
|
|
get(key) {
|
|
const item = this.cache.get(key);
|
|
if (!item) return null;
|
|
|
|
if (Date.now() > item.expiresAt) {
|
|
this.cache.delete(key);
|
|
return null;
|
|
}
|
|
|
|
return item.value;
|
|
}
|
|
|
|
has(key) {
|
|
return this.get(key) !== null;
|
|
}
|
|
|
|
delete(key) {
|
|
this.cache.delete(key);
|
|
}
|
|
|
|
clear() {
|
|
this.cache.clear();
|
|
}
|
|
|
|
// Invalidate all cache keys matching a pattern
|
|
invalidatePattern(pattern) {
|
|
for (const key of this.cache.keys()) {
|
|
if (key.includes(pattern)) {
|
|
this.cache.delete(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Get cache statistics
|
|
getStats() {
|
|
return {
|
|
size: this.cache.size,
|
|
keys: Array.from(this.cache.keys()),
|
|
};
|
|
}
|
|
}
|
|
|
|
module.exports = new CacheManager();
|