104f84f252
As discussed with the product team we want to enforce kebab-case file names for all files, with the exception of files which export a single class, in which case they should be PascalCase and reflect the class which they export. This will help find classes faster, and should push better naming for them too. Some files and packages have been excluded from this linting, specifically when a library or framework depends on the naming of a file for the functionality e.g. Ember, knex-migrator, adapter-manager
55 lines
1.2 KiB
JavaScript
55 lines
1.2 KiB
JavaScript
const TTLCache = require('@isaacs/ttlcache');
|
|
const Base = require('@tryghost/adapter-base-cache');
|
|
|
|
/**
|
|
* Cache adapter compatible wrapper around TTLCache
|
|
* Distinct features of this cache adapter:
|
|
* - it is in-memory only
|
|
* - it supports time-to-live (TTL)
|
|
* - it supports a max number of items
|
|
*/
|
|
class AdapterCacheMemoryTTL extends Base {
|
|
#cache;
|
|
|
|
/**
|
|
*
|
|
* @param {Object} [deps]
|
|
* @param {Number} [deps.max] - The max number of items to keep in the cache.
|
|
* @param {Number} [deps.ttl] - The max time in ms to store items
|
|
*/
|
|
constructor({max = Infinity, ttl = Infinity} = {}) {
|
|
super();
|
|
|
|
this.#cache = new TTLCache({max, ttl});
|
|
}
|
|
|
|
get(key) {
|
|
return this.#cache.get(key);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {String} key
|
|
* @param {*} value
|
|
* @param {Object} [options]
|
|
* @param {Number} [options.ttl]
|
|
*/
|
|
set(key, value, {ttl} = {}) {
|
|
this.#cache.set(key, value, {ttl});
|
|
}
|
|
|
|
reset() {
|
|
this.#cache.clear();
|
|
}
|
|
|
|
/**
|
|
* Helper method to assist "getAll" type of operations
|
|
* @returns {Array<String>} all keys present in the cache
|
|
*/
|
|
keys() {
|
|
return [...this.#cache.keys()];
|
|
}
|
|
}
|
|
|
|
module.exports = AdapterCacheMemoryTTL;
|