diff options
| author | Max Isom <[email protected]> | 2021-11-18 20:55:57 -0500 |
|---|---|---|
| committer | Max Isom <[email protected]> | 2021-11-18 20:55:57 -0500 |
| commit | 04c7e61fc076e2fa5ddf3d0faf6ebb73f4d52d82 (patch) | |
| tree | 71d1879e787b7c988939d963dbc4baa857171f5b /src/services/cache.ts | |
| parent | d805da906a1744c23e0b484b522b078102675770 (diff) | |
| download | muse-04c7e61fc076e2fa5ddf3d0faf6ebb73f4d52d82.tar.xz muse-04c7e61fc076e2fa5ddf3d0faf6ebb73f4d52d82.zip | |
Add FileCache model
Diffstat (limited to 'src/services/cache.ts')
| -rw-r--r-- | src/services/cache.ts | 56 |
1 files changed, 0 insertions, 56 deletions
diff --git a/src/services/cache.ts b/src/services/cache.ts deleted file mode 100644 index 95c6731..0000000 --- a/src/services/cache.ts +++ /dev/null @@ -1,56 +0,0 @@ -import {injectable} from 'inversify'; -import {KeyValueCache} 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), - expiresIn, - } = options[options.length - 1] as Options; - - if (key.length < 4) { - throw new Error(`Cache key ${key} is too short.`); - } - - const cachedResult = await KeyValueCache.findByPk(key); - - if (cachedResult) { - if (new Date() < cachedResult.expiresAt) { - debug(`Cache hit: ${key}`); - return JSON.parse(cachedResult.value) as F; - } - - await cachedResult.destroy(); - } - - debug(`Cache miss: ${key}`); - - const result = await func(...options as any[]); - - // Save result - await KeyValueCache.upsert({ - key, - value: JSON.stringify(result), - expiresAt: futureTimeToDate(expiresIn), - }); - - return result; - } -} |
