2021-09-20 04:04:34 +02:00
|
|
|
import {injectable} from 'inversify';
|
|
|
|
import {Cache} from '../models/index.js';
|
|
|
|
import debug from '../utils/debug.js';
|
|
|
|
|
|
|
|
type Seconds = number;
|
|
|
|
|
|
|
|
type Options = {
|
|
|
|
expiresIn: Seconds;
|
|
|
|
key?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const futureTimeToDate = (time: Seconds) => new Date(new Date().getTime() + (time * 1000));
|
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export default class CacheProvider {
|
|
|
|
async wrap<T extends [...any[], Options], F>(func: (...options: any) => Promise<F>, ...options: T): Promise<F> {
|
|
|
|
if (options.length === 0) {
|
|
|
|
throw new Error('Missing cache options');
|
|
|
|
}
|
|
|
|
|
|
|
|
const functionArgs = options.slice(0, options.length - 1);
|
|
|
|
|
|
|
|
const {
|
|
|
|
key = JSON.stringify(functionArgs),
|
2021-09-20 04:24:46 +02:00
|
|
|
expiresIn,
|
2021-09-20 04:04:34 +02:00
|
|
|
} = options[options.length - 1] as Options;
|
|
|
|
|
|
|
|
const cachedResult = await Cache.findByPk(key);
|
|
|
|
|
|
|
|
if (cachedResult) {
|
|
|
|
if (new Date() < cachedResult.expiresAt) {
|
|
|
|
debug(`Cache hit: ${key}`);
|
2021-09-20 04:24:46 +02:00
|
|
|
return JSON.parse(cachedResult.value) as F;
|
2021-09-20 04:04:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await cachedResult.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(`Cache miss: ${key}`);
|
|
|
|
|
|
|
|
const result = await func(...options as any[]);
|
|
|
|
|
|
|
|
// Save result
|
|
|
|
await Cache.upsert({
|
|
|
|
key,
|
|
|
|
value: JSON.stringify(result),
|
2021-09-20 04:24:46 +02:00
|
|
|
expiresAt: futureTimeToDate(expiresIn),
|
2021-09-20 04:04:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|