mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-19 08:31:43 +03:00
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
53 lines
1.4 KiB
JavaScript
53 lines
1.4 KiB
JavaScript
class EventAwareCacheWrapper {
|
|
#cache;
|
|
|
|
#lastReset;
|
|
|
|
/**
|
|
* @param {Object} deps
|
|
* @param {Object} deps.cache - cache instance extending adapter-base-cache
|
|
* @param {Object} [deps.eventRegistry] - event registry instance
|
|
* @param {Number} [deps.lastReset] - timestamp of last reset
|
|
* @param {String[]} [deps.resetEvents] - event to listen to triggering reset
|
|
*/
|
|
constructor(deps) {
|
|
this.#cache = deps.cache;
|
|
this.#lastReset = deps.lastReset || Date.now();
|
|
|
|
if (deps.resetEvents && deps.eventRegistry) {
|
|
this.#initListeners(deps.eventRegistry, deps.resetEvents);
|
|
}
|
|
}
|
|
|
|
#initListeners(eventRegistry, eventsToResetOn) {
|
|
eventsToResetOn.forEach((event) => {
|
|
eventRegistry.on(event, () => {
|
|
this.reset();
|
|
});
|
|
});
|
|
}
|
|
|
|
#buildResetAwareKey(key) {
|
|
return `${this.#lastReset}:${key}`;
|
|
}
|
|
|
|
async get(key) {
|
|
return this.#cache.get(this.#buildResetAwareKey(key));
|
|
}
|
|
|
|
async set(key, value) {
|
|
return this.#cache.set(this.#buildResetAwareKey(key), value);
|
|
}
|
|
|
|
/**
|
|
* Reset the cache without removing of flushing the keys
|
|
* The mechanism is based on adding a timestamp to the key
|
|
* This way the cache is invalidated but the keys are still there
|
|
*/
|
|
reset() {
|
|
this.#lastReset = Date.now();
|
|
}
|
|
}
|
|
|
|
module.exports = EventAwareCacheWrapper;
|