@pisell/pisellos
Version:
一个可扩展的前端模块化SDK框架,支持插件系统
305 lines (281 loc) • 8.9 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
* 简单的数据压缩函数
* @param data 要压缩的数据
* @returns 压缩后的字符串
*/
var compressData = function compressData(data) {
try {
// 处理undefined值
if (data === undefined) {
return 'undefined';
}
return JSON.stringify(data);
} catch (error) {
console.warn('数据压缩失败:', error);
return JSON.stringify(data);
}
};
/**
* 数据解压缩函数
* @param compressedData 压缩后的数据
* @returns 解压缩后的数据
*/
var decompressData = function decompressData(compressedData) {
try {
// 处理undefined值
if (compressedData === 'undefined') {
return undefined;
}
return JSON.parse(compressedData);
} catch (error) {
console.warn('数据解压缩失败:', error);
return null;
}
};
/**
* 双向链表节点
*/
/**
* LRU缓存类,用于缓存接口数据
* 限制最多存储100条数据,超过时移除最久未使用的数据
* 支持数据压缩以减小缓存压力
* 尾部优先策略:新节点插入尾部,最近使用的节点移动到尾部,删除头部节点
*/
var LRUCache = /*#__PURE__*/function () {
function LRUCache() {
var maxSize = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 100;
_classCallCheck(this, LRUCache);
_defineProperty(this, "head", null);
// 最久未使用的节点
_defineProperty(this, "tail", null);
// 最近使用的节点
_defineProperty(this, "keyMap", new Map());
// key到节点的映射(只存储引用)
_defineProperty(this, "maxSize", void 0);
this.maxSize = maxSize;
}
/**
* 将节点移动到链表尾部(最近使用)
* @param node 要移动的节点
*/
_createClass(LRUCache, [{
key: "moveToTail",
value: function 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 时间戳
*/
}, {
key: "addToTail",
value: function addToTail(key, compressedData, timestamp) {
// 先保存当前的tail引用
var oldTail = this.tail;
var node = {
key: key,
compressedData: compressedData,
timestamp: timestamp,
prev: oldTail,
// 使用保存的引用,避免循环引用
next: null
};
// 存储节点引用
this.keyMap.set(key, node);
// 更新链表关系
if (oldTail) {
oldTail.next = node;
}
this.tail = node;
// 如果是第一个节点,设置head
if (!this.head) {
this.head = node;
}
return node;
}
/**
* 从链表中移除节点
* @param node 要移除的节点
*/
}, {
key: "removeNode",
value: function 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;
}
// 从Map中移除引用
this.keyMap.delete(node.key);
}
/**
* 移除最久未使用的节点(链表头部)
*/
}, {
key: "removeOldest",
value: function removeOldest() {
if (this.head) {
this.removeNode(this.head);
}
}
/**
* 设置缓存
* @param key 缓存键
* @param data 缓存数据
*/
}, {
key: "set",
value: function set(key, data) {
// 压缩数据
var compressedData = compressData(data);
var timestamp = Date.now();
// 检查是否已存在该key
var 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
*/
}, {
key: "get",
value: function get(key) {
var node = this.keyMap.get(key);
if (node) {
// 解压缩数据
var decompressedData = decompressData(node.compressedData);
if (decompressedData !== null) {
// 更新访问时间戳并移动到链表尾部
node.timestamp = Date.now();
this.moveToTail(node);
return decompressedData;
} else {
// 如果解压缩失败,删除该缓存项
this.removeNode(node);
return undefined;
}
}
return undefined;
}
/**
* 检查是否存在缓存
* @param key 缓存键
* @returns 是否存在
*/
}, {
key: "has",
value: function has(key) {
return this.keyMap.has(key);
}
/**
* 清除所有缓存
*/
}, {
key: "clear",
value: function clear() {
// 清除所有节点的引用关系,帮助垃圾回收
var current = this.head;
while (current) {
var next = current.next;
current.prev = null;
current.next = null;
current = next;
}
// 清除Map和头尾指针
this.keyMap.clear();
this.head = null;
this.tail = null;
}
/**
* 获取缓存大小
* @returns 当前缓存数量
*/
}, {
key: "size",
value: function size() {
return this.keyMap.size;
}
/**
* 获取缓存统计信息
* @returns 缓存统计信息
*/
}, {
key: "getStats",
value: function getStats() {
return {
size: this.keyMap.size,
maxSize: this.maxSize,
usage: Math.round(this.keyMap.size / this.maxSize * 100)
};
}
/**
* 删除指定的缓存项
* @param key 要删除的缓存键
* @returns 是否删除成功
*/
}, {
key: "delete",
value: function _delete(key) {
var node = this.keyMap.get(key);
if (node) {
this.removeNode(node);
return true;
}
return false;
}
}]);
return LRUCache;
}(); // 创建全局缓存实例
var scanCache = new LRUCache();
export default scanCache;