This repository has been archived on 2023-10-27. You can view files and clone it, but cannot push or open issues or pull requests.
Files
scoresaber-reloaded/src/db/cache.js

118 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-10-17 23:38:18 +01:00
import eventBus from "../utils/broadcast-channel-pubsub";
2023-10-17 21:42:37 +01:00
export default (name, getObjKey) => {
let cache = {};
// update data cached on another node
2023-10-17 23:38:18 +01:00
eventBus.on("cache-key-set-" + name, ({ key, value }, isLocal) =>
2023-10-17 23:41:42 +01:00
!isLocal ? set(key, value, false) : null
2023-10-17 23:38:18 +01:00
);
eventBus.on("cache-all-set" + name, ({ data }, isLocal) =>
2023-10-17 23:41:42 +01:00
!isLocal ? setAll(data, false) : null
2023-10-17 23:38:18 +01:00
);
eventBus.on("cache-merge-" + name, ({ data }, isLocal) =>
2023-10-17 23:41:42 +01:00
!isLocal ? merge(data, false) : null
2023-10-17 23:38:18 +01:00
);
eventBus.on("cache-key-forget-" + name, ({ key }, isLocal) =>
2023-10-17 23:41:42 +01:00
!isLocal ? forget(key, false) : null
2023-10-17 23:38:18 +01:00
);
eventBus.on("cache-flush-" + name, (_, isLocal) =>
2023-10-17 23:41:42 +01:00
!isLocal ? flush(false) : null
2023-10-17 23:38:18 +01:00
);
2023-10-17 21:42:37 +01:00
const set = (key, value, emitEvent = true) => {
cache[key] = value;
2023-10-17 23:38:18 +01:00
if (emitEvent) eventBus.publish("cache-key-set-" + name, { key, value });
2023-10-17 21:42:37 +01:00
return value;
};
const setAll = (data, emitEvent = true) => {
cache = data;
2023-10-17 23:38:18 +01:00
if (emitEvent) eventBus.publish("cache-all-set-" + name, { data });
2023-10-17 21:42:37 +01:00
return cache;
2023-10-17 23:38:18 +01:00
};
2023-10-17 21:42:37 +01:00
const merge = (data, emitEvent = true) => {
2023-10-17 23:38:18 +01:00
cache = { ...cache, ...data };
2023-10-17 21:42:37 +01:00
2023-10-17 23:38:18 +01:00
if (emitEvent) eventBus.publish("cache-merge-" + name, { data });
2023-10-17 21:42:37 +01:00
return cache;
2023-10-17 23:38:18 +01:00
};
2023-10-17 21:42:37 +01:00
const get = async (key, fetchFunc) => {
if (cache.hasOwnProperty(key)) return cache[key];
const value = await fetchFunc();
return set(key, value);
};
const getByFilter = async (fetchFunc, filterFunc) => {
if (filterFunc) {
const obj = Object.values(cache).find(filterFunc);
if (obj) return obj;
}
const value = await fetchFunc();
if (value === undefined) return value;
const key = getObjKey(value);
return set(key, value);
2023-10-17 23:38:18 +01:00
};
2023-10-17 21:42:37 +01:00
const getAll = () => cache;
2023-10-17 23:38:18 +01:00
const has = (key) => cache[key] !== undefined;
2023-10-17 21:42:37 +01:00
const getKeys = () => Object.keys(cache);
const forget = (key, emitEvent = true) => {
delete cache[key];
2023-10-17 23:38:18 +01:00
if (emitEvent) eventBus.publish("cache-key-forget-" + name, { key });
2023-10-17 21:42:37 +01:00
return cache;
2023-10-17 23:38:18 +01:00
};
2023-10-17 21:42:37 +01:00
const forgetByFilter = (filterFunc, emitEvent = true) => {
if (!filterFunc) return false;
2023-10-17 23:38:18 +01:00
Object.keys(cache)
.filter((key) => filterFunc(cache[key]))
.forEach((key) => {
delete cache[key];
2023-10-17 21:42:37 +01:00
2023-10-17 23:38:18 +01:00
if (emitEvent) eventBus.publish("cache-key-forget-" + name, { key });
2023-10-17 21:42:37 +01:00
});
return true;
2023-10-17 23:38:18 +01:00
};
2023-10-17 21:42:37 +01:00
const flush = (emitEvent = true) => {
cache = {};
2023-10-17 23:38:18 +01:00
if (emitEvent) eventBus.publish("cache-flush-" + name, {});
2023-10-17 21:42:37 +01:00
return cache;
2023-10-17 23:38:18 +01:00
};
2023-10-17 21:42:37 +01:00
return {
has,
get,
getByFilter,
getAll,
set,
setAll,
merge,
getKeys,
forget,
forgetByFilter,
flush,
2023-10-17 23:38:18 +01:00
};
};