feat(cache): external API cache (#786)

This commit is contained in:
sct
2021-01-31 17:24:45 +09:00
committed by GitHub
parent b239598e64
commit 20289b5960
23 changed files with 1210 additions and 1014 deletions

56
server/lib/cache.ts Normal file
View File

@@ -0,0 +1,56 @@
import NodeCache from 'node-cache';
type AvailableCacheIds = 'tmdb' | 'radarr' | 'sonarr' | 'rt';
interface Cache {
id: AvailableCacheIds;
data: NodeCache;
}
const DEFAULT_TTL = 300;
const DEFAULT_CHECK_PERIOD = 120;
class CacheManager {
private availableCaches: Record<AvailableCacheIds, Cache> = {
tmdb: {
id: 'tmdb',
data: new NodeCache({
stdTTL: DEFAULT_TTL,
checkperiod: DEFAULT_CHECK_PERIOD,
}),
},
radarr: {
id: 'radarr',
data: new NodeCache({
stdTTL: DEFAULT_TTL,
checkperiod: DEFAULT_CHECK_PERIOD,
}),
},
sonarr: {
id: 'sonarr',
data: new NodeCache({
stdTTL: DEFAULT_TTL,
checkperiod: DEFAULT_CHECK_PERIOD,
}),
},
rt: {
id: 'rt',
data: new NodeCache({
stdTTL: 21600, // 12 hours TTL
checkperiod: 60 * 30, // 30 minutes check period
}),
},
};
public getCache(id: AvailableCacheIds): Cache {
return this.availableCaches[id];
}
public getAllCaches(): Record<string, Cache> {
return this.availableCaches;
}
}
const cacheManager = new CacheManager();
export default cacheManager;