mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-04 04:10:33 +03:00
5ab81554fc
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.
39 lines
971 B
JavaScript
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;
|