Ghost/ghost/event-aware-cache-wrapper/lib/EventAwareCacheWrapper.js
Naz 5ab81554fc Removed synthetic timestamp-based cache resets
refs https://github.com/TryGhost/Arch/issues/5

- Current event-aware cache wrapper has been using a timestamp as a way to create keys in Redis cache and reset them all at once. We are now moving on to the updated Redis adapter that supports "reset()" natively, so there's no need for synthetic resets.
2023-08-11 17:13:47 +08:00

39 lines
971 B
JavaScript

class EventAwareCacheWrapper {
#cache;
/**
* @param {Object} deps
* @param {Object} deps.cache - cache instance extending adapter-base-cache
* @param {Object} [deps.eventRegistry] - event registry instance
* @param {String[]} [deps.resetEvents] - event to listen to triggering reset
*/
constructor(deps) {
this.#cache = deps.cache;
if (deps.resetEvents && deps.eventRegistry) {
this.#initListeners(deps.eventRegistry, deps.resetEvents);
}
}
#initListeners(eventRegistry, eventsToResetOn) {
eventsToResetOn.forEach((event) => {
eventRegistry.on(event, () => {
this.reset();
});
});
}
async get(key) {
return this.#cache.get(key);
}
async set(key, value) {
return this.#cache.set(key, value);
}
reset() {
return this.#cache.reset();
}
}
module.exports = EventAwareCacheWrapper;