UNPKG

unstorage

Version:
56 lines (55 loc) 1.15 kB
import { defineDriver } from "./utils/index.mjs"; import LRU from "lru-cache"; export default defineDriver((opts = {}) => { const cache = new LRU({ max: 1e3, sizeCalculation: opts.maxSize || opts.maxEntrySize ? (value, key) => { return key.length + byteLength(value); } : void 0, ...opts }); return { name: "lru-cache", options: opts, hasItem(key) { return cache.has(key); }, getItem(key) { return cache.get(key) || null; }, getItemRaw(key) { return cache.get(key) || null; }, setItem(key, value) { cache.set(key, value); }, setItemRaw(key, value) { cache.set(key, value); }, removeItem(key) { cache.delete(key); }, getKeys() { return Array.from(cache.keys()); }, clear() { cache.clear(); }, dispose() { cache.clear(); } }; }); function byteLength(value) { if (typeof Buffer !== void 0) { try { return Buffer.byteLength(value); } catch { } } try { return typeof value === "string" ? value.length : JSON.stringify(value).length; } catch { } return 0; }