perf: cache slow-changing data (series, movies, tags) with 60s TTL
- 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
This commit is contained in:
36
server/utils/cache.js
Normal file
36
server/utils/cache.js
Normal file
@@ -0,0 +1,36 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user