@ixily/activ
Version:
Alpha Capture Trade Idea Verification. Blockchain ownership proven trade ideas and strategies.
112 lines • 3.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PreCacheModule = void 0;
const state = {
slots: new Map(),
expiringSlots: new Map(),
};
const getSlots = (name, howMany = 20) => {
if (!state.slots.has(name)) {
const cache = {
slots: howMany,
storage: new Map(),
orderedIndex: [],
};
const flushCache = () => {
if (cache.orderedIndex.length > cache.slots) {
const toDelete = cache.orderedIndex.splice(0, cache.orderedIndex.length - cache.slots);
toDelete.forEach((key) => {
cache.storage.delete(key);
});
}
};
const setSlots = (slots) => {
cache.slots = slots;
flushCache();
};
const add = (key, data) => {
cache.storage.set(key, data);
cache.orderedIndex.push(key);
flushCache();
};
const get = (key) => {
return cache.storage.get(key);
};
const slots = {
cache,
setSlots,
add,
get,
flushCache,
};
state.slots.set(name, slots);
}
return state.slots.get(name);
};
const getExpiringSlots = (name, howMany = 20, expiration = 10000) => {
if (!state.expiringSlots.has(name)) {
const cache = {
slots: howMany,
storage: new Map(),
createdAtStorage: new Map(),
orderedIndex: [],
expiration,
};
const flushCache = () => {
for (const key of cache.orderedIndex) {
const exp = cache.createdAtStorage.get(key);
if (Date.now() - exp > cache.expiration) {
cache.storage.delete(key);
cache.createdAtStorage.delete(key);
}
}
if (cache.orderedIndex.length > cache.slots) {
const toDelete = cache.orderedIndex.splice(0, cache.orderedIndex.length - cache.slots);
toDelete.forEach((key) => {
cache.storage.delete(key);
});
}
};
const setSlots = (slots) => {
cache.slots = slots;
flushCache();
};
const add = (key, data) => {
cache.storage.set(key, data);
cache.orderedIndex.push(key);
cache.createdAtStorage.set(key, Date.now());
flushCache();
};
const get = (key) => {
const dt = cache.storage.get(key);
if (dt === undefined) {
return undefined;
}
const exp = cache.createdAtStorage.get(key);
if (Date.now() - exp > cache.expiration) {
cache.storage.delete(key);
cache.createdAtStorage.delete(key);
return undefined;
}
return dt;
};
const setExpiration = (expiration) => {
cache.expiration = expiration;
};
const slots = {
cache,
setSlots,
add,
get,
flushCache,
setExpiration,
};
state.expiringSlots.set(name, slots);
}
return state.expiringSlots.get(name);
};
exports.PreCacheModule = {
getSlots,
getExpiringSlots,
};
//# sourceMappingURL=pre-cache.module.js.map