feat(cache): add cache table and flush cache option to settings

also increases tmdb cache times to about 6 hours (12 hours for detail requests)
This commit is contained in:
sct
2021-01-31 13:11:12 +00:00
parent 3c5ae360fd
commit 996bd9f14e
12 changed files with 363 additions and 178 deletions

View File

@@ -148,7 +148,7 @@ class TheMovieDb extends ExternalAPI {
append_to_response: 'credits,external_ids,videos',
},
},
900
43200
);
return data;
@@ -174,7 +174,7 @@ class TheMovieDb extends ExternalAPI {
'aggregate_credits,credits,external_ids,keywords,videos',
},
},
900
43200
);
return data;

View File

@@ -11,3 +11,15 @@ export interface PublicSettingsResponse {
series4kEnabled: boolean;
hideAvailable: boolean;
}
export interface CacheItem {
id: string;
name: string;
stats: {
hits: number;
misses: number;
keys: number;
ksize: number;
vsize: number;
};
}

View File

@@ -1,45 +1,49 @@
import NodeCache from 'node-cache';
type AvailableCacheIds = 'tmdb' | 'radarr' | 'sonarr' | 'rt';
interface Cache {
id: AvailableCacheIds;
data: NodeCache;
}
export type AvailableCacheIds = 'tmdb' | 'radarr' | 'sonarr' | 'rt';
const DEFAULT_TTL = 300;
const DEFAULT_CHECK_PERIOD = 120;
class Cache {
public id: AvailableCacheIds;
public data: NodeCache;
public name: string;
constructor(
id: AvailableCacheIds,
name: string,
options: { stdTtl?: number; checkPeriod?: number } = {}
) {
this.id = id;
this.name = name;
this.data = new NodeCache({
stdTTL: options.stdTtl ?? DEFAULT_TTL,
checkperiod: options.checkPeriod ?? DEFAULT_CHECK_PERIOD,
});
}
public getStats() {
return this.data.getStats();
}
public flush(): void {
this.data.flushAll();
}
}
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
}),
},
tmdb: new Cache('tmdb', 'TMDb API', {
stdTtl: 21600,
checkPeriod: 60 * 30,
}),
radarr: new Cache('radarr', 'Radarr API'),
sonarr: new Cache('sonarr', 'Sonarr API'),
rt: new Cache('rt', 'Rotten Tomatoes API', {
stdTtl: 43200,
checkPeriod: 60 * 30,
}),
};
public getCache(id: AvailableCacheIds): Cache {

View File

@@ -16,6 +16,7 @@ import { SettingsAboutResponse } from '../../interfaces/api/settingsInterfaces';
import notificationRoutes from './notifications';
import sonarrRoutes from './sonarr';
import radarrRoutes from './radarr';
import cacheManager, { AvailableCacheIds } from '../../lib/cache';
const settingsRoutes = Router();
@@ -273,6 +274,32 @@ settingsRoutes.get<{ jobId: string }>(
}
);
settingsRoutes.get('/cache', (req, res) => {
const caches = cacheManager.getAllCaches();
return res.status(200).json(
Object.values(caches).map((cache) => ({
id: cache.id,
name: cache.name,
stats: cache.getStats(),
}))
);
});
settingsRoutes.get<{ cacheId: AvailableCacheIds }>(
'/cache/:cacheId/flush',
(req, res, next) => {
const cache = cacheManager.getCache(req.params.cacheId);
if (cache) {
cache.flush();
return res.status(204).send();
}
next({ status: 404, message: 'Cache does not exist.' });
}
);
settingsRoutes.get(
'/initialize',
isAuthenticated(Permission.ADMIN),