2023-02-22 15:47:32 +03:00
|
|
|
class EventAwareCacheWrapper {
|
2023-02-22 17:16:38 +03:00
|
|
|
#cache;
|
2023-02-22 15:47:32 +03:00
|
|
|
/**
|
|
|
|
* @param {Object} deps
|
2023-02-22 17:16:38 +03:00
|
|
|
* @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
|
2023-02-22 15:47:32 +03:00
|
|
|
*/
|
|
|
|
constructor(deps) {
|
2023-02-22 17:16:38 +03:00
|
|
|
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) {
|
2023-08-09 09:33:15 +03:00
|
|
|
return this.#cache.get(key);
|
2023-02-22 17:16:38 +03:00
|
|
|
}
|
2023-02-22 15:47:32 +03:00
|
|
|
|
2023-02-22 17:16:38 +03:00
|
|
|
async set(key, value) {
|
2023-08-09 09:33:15 +03:00
|
|
|
return this.#cache.set(key, value);
|
2023-02-22 17:16:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
2023-08-09 09:33:15 +03:00
|
|
|
return this.#cache.reset();
|
2023-02-22 15:47:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = EventAwareCacheWrapper;
|