UNPKG

@pisell/pisellos

Version:

一个可扩展的前端模块化SDK框架,支持插件系统

232 lines (230 loc) 5.5 kB
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); // src/solution/BookingTicket/utils/scan/scanCache.ts var scanCache_exports = {}; __export(scanCache_exports, { default: () => scanCache_default }); module.exports = __toCommonJS(scanCache_exports); var compressData = (data) => { try { if (data === void 0) { return "undefined"; } return JSON.stringify(data); } catch (error) { console.warn("数据压缩失败:", error); return JSON.stringify(data); } }; var decompressData = (compressedData) => { try { if (compressedData === "undefined") { return void 0; } return JSON.parse(compressedData); } catch (error) { console.warn("数据解压缩失败:", error); return null; } }; var LRUCache = class { constructor(maxSize = 100) { this.head = null; // 最久未使用的节点 this.tail = null; // 最近使用的节点 this.keyMap = /* @__PURE__ */ new Map(); this.maxSize = maxSize; } /** * 将节点移动到链表尾部(最近使用) * @param node 要移动的节点 */ moveToTail(node) { if (this.tail === node) return; if (node.prev) { node.prev.next = node.next; } if (node.next) { node.next.prev = node.prev; } if (this.head === node) { this.head = node.next; } if (this.tail) { this.tail.next = node; } node.prev = this.tail; node.next = null; this.tail = node; if (!this.head) { this.head = node; } } /** * 添加新节点到链表尾部 * @param key 缓存键 * @param compressedData 压缩后的数据 * @param timestamp 时间戳 */ addToTail(key, compressedData, timestamp) { const oldTail = this.tail; const node = { key, compressedData, timestamp, prev: oldTail, // 使用保存的引用,避免循环引用 next: null }; this.keyMap.set(key, node); if (oldTail) { oldTail.next = node; } this.tail = node; if (!this.head) { this.head = node; } return node; } /** * 从链表中移除节点 * @param node 要移除的节点 */ removeNode(node) { if (node.prev) { node.prev.next = node.next; } if (node.next) { node.next.prev = node.prev; } if (this.head === node) { this.head = node.next; } if (this.tail === node) { this.tail = node.prev; } this.keyMap.delete(node.key); } /** * 移除最久未使用的节点(链表头部) */ removeOldest() { if (this.head) { this.removeNode(this.head); } } /** * 设置缓存 * @param key 缓存键 * @param data 缓存数据 */ set(key, data) { const compressedData = compressData(data); const timestamp = Date.now(); const existingNode = this.keyMap.get(key); if (existingNode) { existingNode.compressedData = compressedData; existingNode.timestamp = timestamp; this.moveToTail(existingNode); } else { if (this.keyMap.size >= this.maxSize) { this.removeOldest(); } this.addToTail(key, compressedData, timestamp); } } /** * 获取缓存 * @param key 缓存键 * @returns 缓存数据或undefined */ get(key) { const node = this.keyMap.get(key); if (node) { const decompressedData = decompressData(node.compressedData); if (decompressedData !== null) { node.timestamp = Date.now(); this.moveToTail(node); return decompressedData; } else { this.removeNode(node); return void 0; } } return void 0; } /** * 检查是否存在缓存 * @param key 缓存键 * @returns 是否存在 */ has(key) { return this.keyMap.has(key); } /** * 清除所有缓存 */ clear() { let current = this.head; while (current) { const next = current.next; current.prev = null; current.next = null; current = next; } this.keyMap.clear(); this.head = null; this.tail = null; } /** * 获取缓存大小 * @returns 当前缓存数量 */ size() { return this.keyMap.size; } /** * 获取缓存统计信息 * @returns 缓存统计信息 */ getStats() { return { size: this.keyMap.size, maxSize: this.maxSize, usage: Math.round(this.keyMap.size / this.maxSize * 100) }; } /** * 删除指定的缓存项 * @param key 要删除的缓存键 * @returns 是否删除成功 */ delete(key) { const node = this.keyMap.get(key); if (node) { this.removeNode(node); return true; } return false; } }; var scanCache = new LRUCache(); var scanCache_default = scanCache;