@effectai/sdk
Version:
Effect Network Javscript/Typescript SDK (for [https://effect.network](https://effect.network))
56 lines • 1.46 kB
JavaScript
import { get as idbGet, set as idbSet } from "idb-keyval";
export const memoryCache = new Map();
export class MemoryCache {
size() {
return memoryCache.size;
}
get(key) {
return memoryCache.get(key);
}
set(key, value) {
memoryCache.set(key, value);
}
}
export class LocalStorageCache {
constructor() {
if (typeof localStorage === "undefined" || localStorage === null) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const LocalStorage = require("node-localstorage").LocalStorage;
this.localStorage = new LocalStorage("./scratch");
}
else {
this.localStorage = localStorage;
}
}
get(key) {
const item = this.localStorage.getItem(key);
return item ? JSON.parse(item) : undefined;
}
set(key, value) {
this.localStorage.setItem(key, JSON.stringify(value, null, 2));
}
}
export class IDBCache {
async get(key) {
return await idbGet(key);
}
set(key, value) {
idbSet(key, value);
}
}
export class CacheManager {
constructor(cache) {
this.cache = cache;
}
get(key) {
return this.cache.get(key);
}
set(key, value) {
this.cache.set(key, value);
}
}
export const createCacheManager = (cache) => {
const manager = new CacheManager(cache);
return manager;
};
//# sourceMappingURL=cache.js.map