@credo-ts/core
Version:
<p align="center"> <br /> <img alt="Credo Logo" src="https://github.com/openwallet-foundation/credo-ts/blob/c7886cb8377ceb8ee4efe8d264211e561a75072d/images/credo-logo.png" height="250px" /> </p> <h1 align="center"><b>Credo - Core</b></h1
50 lines (46 loc) • 1.3 kB
JavaScript
import LRUMap from "lru_map";
//#region src/modules/cache/InMemoryLruCache.ts
/**
* In memory LRU cache.
*
* This cache can be used with multiple agent context instances, however all instances will share the same cache.
* If you need the cache to be isolated per agent context instance, make sure to use a different cache implementation.
*/
var InMemoryLruCache = class {
constructor({ limit }) {
this.cache = new LRUMap.LRUMap(limit);
}
async get(_agentContext, key) {
this.removeExpiredItems();
const item = this.cache.get(key);
if (!item) return null;
return item.value;
}
async set(_agentContext, key, value, expiresInSeconds) {
this.removeExpiredItems();
let expiresDate;
if (expiresInSeconds) {
expiresDate = /* @__PURE__ */ new Date();
expiresDate.setSeconds(expiresDate.getSeconds() + expiresInSeconds);
}
this.cache.set(key, {
expiresAt: expiresDate?.getTime(),
value
});
}
clear() {
this.cache.clear();
}
async remove(_agentContext, key) {
this.removeExpiredItems();
this.cache.delete(key);
}
removeExpiredItems() {
this.cache.forEach((value, key) => {
if (value.expiresAt && Date.now() > value.expiresAt) this.cache.delete(key);
});
}
};
//#endregion
export { InMemoryLruCache };
//# sourceMappingURL=InMemoryLruCache.mjs.map