- Add MemoryCache utility with get/set/invalidate/clear - Cache Sonarr series, Sonarr tags, Radarr movies, Radarr tags - 60-second TTL - first request fetches, subsequent requests served from cache - Queue, history, and torrent data remain uncached (changes frequently) - On cache hit, these 4 heavy API calls resolve instantly
37 lines
589 B
JavaScript
37 lines
589 B
JavaScript
const { logToFile } = require('./logger');
|
|
|
|
class MemoryCache {
|
|
constructor() {
|
|
this.store = new Map();
|
|
}
|
|
|
|
get(key) {
|
|
const entry = this.store.get(key);
|
|
if (!entry) return null;
|
|
if (Date.now() > entry.expiresAt) {
|
|
this.store.delete(key);
|
|
return null;
|
|
}
|
|
return entry.value;
|
|
}
|
|
|
|
set(key, value, ttlMs) {
|
|
this.store.set(key, {
|
|
value,
|
|
expiresAt: Date.now() + ttlMs
|
|
});
|
|
}
|
|
|
|
invalidate(key) {
|
|
this.store.delete(key);
|
|
}
|
|
|
|
clear() {
|
|
this.store.clear();
|
|
}
|
|
}
|
|
|
|
const cache = new MemoryCache();
|
|
|
|
module.exports = cache;
|