UNPKG

@modern-js/runtime-utils

Version:

A Progressive React Framework for modern web development.

69 lines (68 loc) 2.16 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var container_exports = {}; __export(container_exports, { MemoryContainer: () => MemoryContainer }); module.exports = __toCommonJS(container_exports); var import_lru_cache = require("lru-cache"); class MemoryContainer { async get(key) { return this.cache.get(key); } async set(key, value) { this.cache.set(key, value); return this; } async has(key) { return this.cache.has(key); } async delete(key) { const exist = await this.has(key); if (exist) { this.cache.delete(key); } return exist; } forEach(callbackFn) { this.cache.forEach((value, key) => { callbackFn(value, key, this); }); } constructor({ max, maxAge } = {}) { this.cache = new import_lru_cache.LRUCache({ maxSize: (max || 256) * MemoryContainer.MB, ttl: maxAge || MemoryContainer.hour, sizeCalculation: (value, key) => { return JSON.stringify(value).length; } }); } } MemoryContainer.BYTE = 1; MemoryContainer.KB = 1024 * MemoryContainer.BYTE; MemoryContainer.MB = 1024 * MemoryContainer.KB; MemoryContainer.ms = 1; MemoryContainer.second = MemoryContainer.ms * 1e3; MemoryContainer.minute = MemoryContainer.second * 60; MemoryContainer.hour = MemoryContainer.minute * 60; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { MemoryContainer });