@pisell/pisellos 0.0.215 → 0.0.217
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/modules/Customer/index.d.ts +33 -0
- package/dist/modules/Customer/index.js +316 -58
- package/dist/modules/Customer/types.d.ts +17 -1
- package/dist/modules/Customer/types.js +2 -0
- package/dist/modules/ProductList/index.d.ts +6 -0
- package/dist/modules/ProductList/index.js +20 -0
- package/dist/solution/BookingTicket/index.d.ts +33 -0
- package/dist/solution/BookingTicket/index.js +167 -21
- package/dist/solution/BookingTicket/types.d.ts +3 -1
- package/dist/solution/BookingTicket/types.js +2 -0
- package/dist/solution/BookingTicket/utils/scan/cloudSearch.js +15 -3
- package/dist/solution/BookingTicket/utils/scan/handleScan.d.ts +2 -2
- package/dist/solution/BookingTicket/utils/scan/handleScan.js +99 -55
- package/dist/solution/BookingTicket/utils/scan/index.d.ts +12 -4
- package/dist/solution/BookingTicket/utils/scan/index.js +32 -20
- package/dist/solution/BookingTicket/utils/scan/scanCache.d.ts +78 -0
- package/dist/solution/BookingTicket/utils/scan/scanCache.js +305 -0
- package/lib/modules/Customer/index.d.ts +33 -0
- package/lib/modules/Customer/index.js +168 -32
- package/lib/modules/Customer/types.d.ts +17 -1
- package/lib/modules/Customer/types.js +2 -0
- package/lib/modules/ProductList/index.d.ts +6 -0
- package/lib/modules/ProductList/index.js +18 -0
- package/lib/solution/BookingTicket/index.d.ts +33 -0
- package/lib/solution/BookingTicket/index.js +98 -8
- package/lib/solution/BookingTicket/types.d.ts +3 -1
- package/lib/solution/BookingTicket/types.js +2 -0
- package/lib/solution/BookingTicket/utils/scan/cloudSearch.js +23 -3
- package/lib/solution/BookingTicket/utils/scan/handleScan.d.ts +2 -2
- package/lib/solution/BookingTicket/utils/scan/handleScan.js +58 -16
- package/lib/solution/BookingTicket/utils/scan/index.d.ts +12 -4
- package/lib/solution/BookingTicket/utils/scan/index.js +16 -20
- package/lib/solution/BookingTicket/utils/scan/scanCache.d.ts +78 -0
- package/lib/solution/BookingTicket/utils/scan/scanCache.js +231 -0
- package/package.json +1 -1
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
7
|
var __export = (target, all) => {
|
|
6
8
|
for (var name in all)
|
|
@@ -14,6 +16,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
14
16
|
}
|
|
15
17
|
return to;
|
|
16
18
|
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
17
27
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
28
|
|
|
19
29
|
// src/solution/BookingTicket/utils/scan/handleScan.ts
|
|
@@ -24,6 +34,7 @@ __export(handleScan_exports, {
|
|
|
24
34
|
});
|
|
25
35
|
module.exports = __toCommonJS(handleScan_exports);
|
|
26
36
|
var import_cloudSearch = require("./cloudSearch");
|
|
37
|
+
var import_scanCache = __toESM(require("./scanCache"));
|
|
27
38
|
var promiseAny = async (promises) => {
|
|
28
39
|
if (promises.length === 0) {
|
|
29
40
|
throw new Error("No promises provided");
|
|
@@ -47,9 +58,49 @@ var promiseAny = async (promises) => {
|
|
|
47
58
|
});
|
|
48
59
|
});
|
|
49
60
|
};
|
|
50
|
-
var
|
|
61
|
+
var handleScanFn = (getRequestList, localSearch) => {
|
|
51
62
|
return async (scanResult) => {
|
|
52
63
|
const { value } = scanResult || {};
|
|
64
|
+
try {
|
|
65
|
+
const localResult = localSearch == null ? void 0 : localSearch(value);
|
|
66
|
+
if ((localResult == null ? void 0 : localResult.length) > 0) {
|
|
67
|
+
console.log("本地搜索到数据>>>>>>>", localResult);
|
|
68
|
+
return {
|
|
69
|
+
searchType: "local_product",
|
|
70
|
+
response: { data: { list: localResult } }
|
|
71
|
+
};
|
|
72
|
+
} else {
|
|
73
|
+
console.log("本地搜索无数据>>>>>>>");
|
|
74
|
+
}
|
|
75
|
+
} catch (error) {
|
|
76
|
+
console.error("本地搜索到数据失败>>>>>>>", error);
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
if (import_scanCache.default.has(value)) {
|
|
80
|
+
const result = import_scanCache.default.get(value);
|
|
81
|
+
console.log("缓存中搜索到数据>>>>>>>", result);
|
|
82
|
+
return result;
|
|
83
|
+
} else {
|
|
84
|
+
console.log("缓存中无数据>>>>>>>");
|
|
85
|
+
}
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.error("缓存中搜索数据失败>>>>>>>", error);
|
|
88
|
+
}
|
|
89
|
+
const requestList = (getRequestList == null ? void 0 : getRequestList(value)) || [];
|
|
90
|
+
if (requestList.length === 0) {
|
|
91
|
+
throw new Error("requestList is empty");
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const result = await promiseAny(requestList);
|
|
95
|
+
import_scanCache.default.set(value, result);
|
|
96
|
+
return result;
|
|
97
|
+
} catch (error) {
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
var handleGlobalScan = (request, localSearch) => {
|
|
103
|
+
const getRequestList = (value) => {
|
|
53
104
|
let requestList = [];
|
|
54
105
|
if (value.startsWith("WL")) {
|
|
55
106
|
requestList = [(0, import_cloudSearch.searchWallet)(request, value)];
|
|
@@ -60,17 +111,12 @@ var handleGlobalScan = (request) => {
|
|
|
60
111
|
(0, import_cloudSearch.searchProduct)(request, value)
|
|
61
112
|
];
|
|
62
113
|
}
|
|
63
|
-
|
|
64
|
-
const result = await promiseAny(requestList);
|
|
65
|
-
return result;
|
|
66
|
-
} catch (error) {
|
|
67
|
-
throw error;
|
|
68
|
-
}
|
|
114
|
+
return requestList;
|
|
69
115
|
};
|
|
116
|
+
return handleScanFn(getRequestList, localSearch);
|
|
70
117
|
};
|
|
71
|
-
var handleCustomerScan = (request) => {
|
|
72
|
-
|
|
73
|
-
const { value } = scanResult || {};
|
|
118
|
+
var handleCustomerScan = (request, localSearch) => {
|
|
119
|
+
const getRequestList = (value) => {
|
|
74
120
|
let requestList = [];
|
|
75
121
|
if (value.startsWith("WL")) {
|
|
76
122
|
requestList = [(0, import_cloudSearch.searchWallet)(request, value)];
|
|
@@ -80,13 +126,9 @@ var handleCustomerScan = (request) => {
|
|
|
80
126
|
(0, import_cloudSearch.searchWallet)(request, value)
|
|
81
127
|
];
|
|
82
128
|
}
|
|
83
|
-
|
|
84
|
-
const result = await Promise.race(requestList);
|
|
85
|
-
return result;
|
|
86
|
-
} catch (error) {
|
|
87
|
-
throw error;
|
|
88
|
-
}
|
|
129
|
+
return requestList;
|
|
89
130
|
};
|
|
131
|
+
return handleScanFn(getRequestList, localSearch);
|
|
90
132
|
};
|
|
91
133
|
// Annotate the CommonJS export names for ESM import in node:
|
|
92
134
|
0 && (module.exports = {
|
|
@@ -2,10 +2,18 @@ import { WatchEventItem } from '../../../../utils/watch';
|
|
|
2
2
|
import { BookingTicketImpl } from '../../index';
|
|
3
3
|
import Tasks from '../../../../utils/task';
|
|
4
4
|
interface ScanResult {
|
|
5
|
-
type:
|
|
5
|
+
type: ScanResultType;
|
|
6
6
|
value: string;
|
|
7
|
+
data?: {
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
declare enum ScanResultType {
|
|
12
|
+
Scanner = "scanner",
|
|
13
|
+
NFC = "nfc",
|
|
14
|
+
NativeScanner = "nativeScanner"
|
|
7
15
|
}
|
|
8
|
-
export
|
|
16
|
+
export default class Scan {
|
|
9
17
|
private solution;
|
|
10
18
|
private watchKey;
|
|
11
19
|
private listenerConfig;
|
|
@@ -15,7 +23,7 @@ export declare class Scan {
|
|
|
15
23
|
* @param watchKey 监听的key
|
|
16
24
|
* @param event 事件
|
|
17
25
|
*/
|
|
18
|
-
addListener(event: WatchEventItem,
|
|
26
|
+
addListener(event: WatchEventItem, scanCallback?: (result: ScanResult) => Promise<any>): {
|
|
19
27
|
remove: () => void;
|
|
20
28
|
};
|
|
21
29
|
/**
|
|
@@ -39,7 +47,7 @@ export declare class Scan {
|
|
|
39
47
|
* @returns 监听配置
|
|
40
48
|
*/
|
|
41
49
|
getListenerConfig(eventKey: string): {
|
|
42
|
-
|
|
50
|
+
scanCallback?: ((result: ScanResult) => Promise<any>) | undefined;
|
|
43
51
|
task: Tasks;
|
|
44
52
|
} | undefined;
|
|
45
53
|
/**
|
|
@@ -29,7 +29,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
29
29
|
// src/solution/BookingTicket/utils/scan/index.ts
|
|
30
30
|
var scan_exports = {};
|
|
31
31
|
__export(scan_exports, {
|
|
32
|
-
|
|
32
|
+
default: () => Scan
|
|
33
33
|
});
|
|
34
34
|
module.exports = __toCommonJS(scan_exports);
|
|
35
35
|
var import_watch = __toESM(require("../../../../utils/watch"));
|
|
@@ -37,17 +37,13 @@ var import_task = __toESM(require("../../../../utils/task"));
|
|
|
37
37
|
var Scan = class {
|
|
38
38
|
constructor(solution, watchKey) {
|
|
39
39
|
this.listenerConfig = /* @__PURE__ */ new Map();
|
|
40
|
-
var _a, _b, _c, _d, _e
|
|
40
|
+
var _a, _b, _c, _d, _e;
|
|
41
41
|
this.solution = solution;
|
|
42
42
|
this.watchKey = watchKey;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
(_a = solution == null ? void 0 : solution.window) == null ? void 0 : _a.interaction
|
|
46
|
-
);
|
|
47
|
-
(_f = (_e = (_d = (_c = (_b = this.solution) == null ? void 0 : _b.window) == null ? void 0 : _c.interaction) == null ? void 0 : _d.utils) == null ? void 0 : _e.mountFunction) == null ? void 0 : _f.call(
|
|
48
|
-
_e,
|
|
43
|
+
(_e = (_d = (_c = (_b = (_a = this.solution) == null ? void 0 : _a.window) == null ? void 0 : _b.interaction) == null ? void 0 : _c.utils) == null ? void 0 : _d.mountFunction) == null ? void 0 : _e.call(
|
|
44
|
+
_d,
|
|
49
45
|
"global",
|
|
50
|
-
"
|
|
46
|
+
"peripheralsResult",
|
|
51
47
|
(strVal) => {
|
|
52
48
|
try {
|
|
53
49
|
const result = JSON.parse(strVal);
|
|
@@ -64,14 +60,14 @@ var Scan = class {
|
|
|
64
60
|
* @param watchKey 监听的key
|
|
65
61
|
* @param event 事件
|
|
66
62
|
*/
|
|
67
|
-
addListener(event,
|
|
63
|
+
addListener(event, scanCallback) {
|
|
68
64
|
import_watch.default.subscribe(this.watchKey, event);
|
|
69
65
|
const task = new import_task.default();
|
|
70
66
|
task.addAction(
|
|
71
67
|
event.key,
|
|
72
68
|
(...args) => this.handleScanResultFn(event.key, ...args)
|
|
73
69
|
);
|
|
74
|
-
this.listenerConfig.set(event.key, {
|
|
70
|
+
this.listenerConfig.set(event.key, { scanCallback, task });
|
|
75
71
|
return {
|
|
76
72
|
remove: () => {
|
|
77
73
|
this.removeListener(event);
|
|
@@ -124,30 +120,30 @@ var Scan = class {
|
|
|
124
120
|
async handleScanResultFn(...args) {
|
|
125
121
|
var _a;
|
|
126
122
|
const [eventKey, other] = args;
|
|
127
|
-
const { type, value } = (other == null ? void 0 : other.scanResult) || {};
|
|
128
|
-
const
|
|
129
|
-
if (
|
|
123
|
+
const { type, value, data } = (other == null ? void 0 : other.scanResult) || {};
|
|
124
|
+
const scanCallback = (_a = this.getListenerConfig(eventKey)) == null ? void 0 : _a.scanCallback;
|
|
125
|
+
if (scanCallback) {
|
|
130
126
|
try {
|
|
131
|
-
const result = await
|
|
127
|
+
const result = await scanCallback({ type, value });
|
|
128
|
+
console.log(`${eventKey}_result>>>>>>>`, result);
|
|
132
129
|
import_watch.default.publishByEventKey(this.watchKey, eventKey, {
|
|
133
130
|
type: eventKey,
|
|
134
131
|
status: "success",
|
|
135
132
|
scanType: type,
|
|
136
133
|
scanCode: value,
|
|
134
|
+
scanData: data,
|
|
137
135
|
result
|
|
138
136
|
});
|
|
139
137
|
} catch (error) {
|
|
138
|
+
console.log(`${eventKey}_result_error>>>>>>>`, error);
|
|
140
139
|
import_watch.default.publishByEventKey(this.watchKey, eventKey, {
|
|
141
140
|
type: eventKey,
|
|
142
141
|
status: "failed",
|
|
143
142
|
scanType: type,
|
|
144
|
-
scanCode: value
|
|
143
|
+
scanCode: value,
|
|
144
|
+
scanData: data
|
|
145
145
|
});
|
|
146
146
|
}
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
149
|
};
|
|
150
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
151
|
-
0 && (module.exports = {
|
|
152
|
-
Scan
|
|
153
|
-
});
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LRU缓存类,用于缓存接口数据
|
|
3
|
+
* 限制最多存储100条数据,超过时移除最久未使用的数据
|
|
4
|
+
* 支持数据压缩以减小缓存压力
|
|
5
|
+
* 尾部优先策略:新节点插入尾部,最近使用的节点移动到尾部,删除头部节点
|
|
6
|
+
*/
|
|
7
|
+
declare class LRUCache<T> {
|
|
8
|
+
private head;
|
|
9
|
+
private tail;
|
|
10
|
+
private keyMap;
|
|
11
|
+
private readonly maxSize;
|
|
12
|
+
constructor(maxSize?: number);
|
|
13
|
+
/**
|
|
14
|
+
* 将节点移动到链表尾部(最近使用)
|
|
15
|
+
* @param node 要移动的节点
|
|
16
|
+
*/
|
|
17
|
+
private moveToTail;
|
|
18
|
+
/**
|
|
19
|
+
* 添加新节点到链表尾部
|
|
20
|
+
* @param key 缓存键
|
|
21
|
+
* @param compressedData 压缩后的数据
|
|
22
|
+
* @param timestamp 时间戳
|
|
23
|
+
*/
|
|
24
|
+
private addToTail;
|
|
25
|
+
/**
|
|
26
|
+
* 从链表中移除节点
|
|
27
|
+
* @param node 要移除的节点
|
|
28
|
+
*/
|
|
29
|
+
private removeNode;
|
|
30
|
+
/**
|
|
31
|
+
* 移除最久未使用的节点(链表头部)
|
|
32
|
+
*/
|
|
33
|
+
private removeOldest;
|
|
34
|
+
/**
|
|
35
|
+
* 设置缓存
|
|
36
|
+
* @param key 缓存键
|
|
37
|
+
* @param data 缓存数据
|
|
38
|
+
*/
|
|
39
|
+
set(key: string, data: T): void;
|
|
40
|
+
/**
|
|
41
|
+
* 获取缓存
|
|
42
|
+
* @param key 缓存键
|
|
43
|
+
* @returns 缓存数据或undefined
|
|
44
|
+
*/
|
|
45
|
+
get(key: string): T | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* 检查是否存在缓存
|
|
48
|
+
* @param key 缓存键
|
|
49
|
+
* @returns 是否存在
|
|
50
|
+
*/
|
|
51
|
+
has(key: string): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* 清除所有缓存
|
|
54
|
+
*/
|
|
55
|
+
clear(): void;
|
|
56
|
+
/**
|
|
57
|
+
* 获取缓存大小
|
|
58
|
+
* @returns 当前缓存数量
|
|
59
|
+
*/
|
|
60
|
+
size(): number;
|
|
61
|
+
/**
|
|
62
|
+
* 获取缓存统计信息
|
|
63
|
+
* @returns 缓存统计信息
|
|
64
|
+
*/
|
|
65
|
+
getStats(): {
|
|
66
|
+
size: number;
|
|
67
|
+
maxSize: number;
|
|
68
|
+
usage: number;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* 删除指定的缓存项
|
|
72
|
+
* @param key 要删除的缓存键
|
|
73
|
+
* @returns 是否删除成功
|
|
74
|
+
*/
|
|
75
|
+
delete(key: string): boolean;
|
|
76
|
+
}
|
|
77
|
+
declare const scanCache: LRUCache<any>;
|
|
78
|
+
export default scanCache;
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/solution/BookingTicket/utils/scan/scanCache.ts
|
|
20
|
+
var scanCache_exports = {};
|
|
21
|
+
__export(scanCache_exports, {
|
|
22
|
+
default: () => scanCache_default
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(scanCache_exports);
|
|
25
|
+
var compressData = (data) => {
|
|
26
|
+
try {
|
|
27
|
+
if (data === void 0) {
|
|
28
|
+
return "undefined";
|
|
29
|
+
}
|
|
30
|
+
return JSON.stringify(data);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.warn("数据压缩失败:", error);
|
|
33
|
+
return JSON.stringify(data);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var decompressData = (compressedData) => {
|
|
37
|
+
try {
|
|
38
|
+
if (compressedData === "undefined") {
|
|
39
|
+
return void 0;
|
|
40
|
+
}
|
|
41
|
+
return JSON.parse(compressedData);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
console.warn("数据解压缩失败:", error);
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var LRUCache = class {
|
|
48
|
+
constructor(maxSize = 100) {
|
|
49
|
+
this.head = null;
|
|
50
|
+
// 最久未使用的节点
|
|
51
|
+
this.tail = null;
|
|
52
|
+
// 最近使用的节点
|
|
53
|
+
this.keyMap = /* @__PURE__ */ new Map();
|
|
54
|
+
this.maxSize = maxSize;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 将节点移动到链表尾部(最近使用)
|
|
58
|
+
* @param node 要移动的节点
|
|
59
|
+
*/
|
|
60
|
+
moveToTail(node) {
|
|
61
|
+
if (this.tail === node)
|
|
62
|
+
return;
|
|
63
|
+
if (node.prev) {
|
|
64
|
+
node.prev.next = node.next;
|
|
65
|
+
}
|
|
66
|
+
if (node.next) {
|
|
67
|
+
node.next.prev = node.prev;
|
|
68
|
+
}
|
|
69
|
+
if (this.head === node) {
|
|
70
|
+
this.head = node.next;
|
|
71
|
+
}
|
|
72
|
+
if (this.tail) {
|
|
73
|
+
this.tail.next = node;
|
|
74
|
+
}
|
|
75
|
+
node.prev = this.tail;
|
|
76
|
+
node.next = null;
|
|
77
|
+
this.tail = node;
|
|
78
|
+
if (!this.head) {
|
|
79
|
+
this.head = node;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 添加新节点到链表尾部
|
|
84
|
+
* @param key 缓存键
|
|
85
|
+
* @param compressedData 压缩后的数据
|
|
86
|
+
* @param timestamp 时间戳
|
|
87
|
+
*/
|
|
88
|
+
addToTail(key, compressedData, timestamp) {
|
|
89
|
+
const oldTail = this.tail;
|
|
90
|
+
const node = {
|
|
91
|
+
key,
|
|
92
|
+
compressedData,
|
|
93
|
+
timestamp,
|
|
94
|
+
prev: oldTail,
|
|
95
|
+
// 使用保存的引用,避免循环引用
|
|
96
|
+
next: null
|
|
97
|
+
};
|
|
98
|
+
this.keyMap.set(key, node);
|
|
99
|
+
if (oldTail) {
|
|
100
|
+
oldTail.next = node;
|
|
101
|
+
}
|
|
102
|
+
this.tail = node;
|
|
103
|
+
if (!this.head) {
|
|
104
|
+
this.head = node;
|
|
105
|
+
}
|
|
106
|
+
return node;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* 从链表中移除节点
|
|
110
|
+
* @param node 要移除的节点
|
|
111
|
+
*/
|
|
112
|
+
removeNode(node) {
|
|
113
|
+
if (node.prev) {
|
|
114
|
+
node.prev.next = node.next;
|
|
115
|
+
}
|
|
116
|
+
if (node.next) {
|
|
117
|
+
node.next.prev = node.prev;
|
|
118
|
+
}
|
|
119
|
+
if (this.head === node) {
|
|
120
|
+
this.head = node.next;
|
|
121
|
+
}
|
|
122
|
+
if (this.tail === node) {
|
|
123
|
+
this.tail = node.prev;
|
|
124
|
+
}
|
|
125
|
+
this.keyMap.delete(node.key);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* 移除最久未使用的节点(链表头部)
|
|
129
|
+
*/
|
|
130
|
+
removeOldest() {
|
|
131
|
+
if (this.head) {
|
|
132
|
+
this.removeNode(this.head);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* 设置缓存
|
|
137
|
+
* @param key 缓存键
|
|
138
|
+
* @param data 缓存数据
|
|
139
|
+
*/
|
|
140
|
+
set(key, data) {
|
|
141
|
+
const compressedData = compressData(data);
|
|
142
|
+
const timestamp = Date.now();
|
|
143
|
+
const existingNode = this.keyMap.get(key);
|
|
144
|
+
if (existingNode) {
|
|
145
|
+
existingNode.compressedData = compressedData;
|
|
146
|
+
existingNode.timestamp = timestamp;
|
|
147
|
+
this.moveToTail(existingNode);
|
|
148
|
+
} else {
|
|
149
|
+
if (this.keyMap.size >= this.maxSize) {
|
|
150
|
+
this.removeOldest();
|
|
151
|
+
}
|
|
152
|
+
this.addToTail(key, compressedData, timestamp);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* 获取缓存
|
|
157
|
+
* @param key 缓存键
|
|
158
|
+
* @returns 缓存数据或undefined
|
|
159
|
+
*/
|
|
160
|
+
get(key) {
|
|
161
|
+
const node = this.keyMap.get(key);
|
|
162
|
+
if (node) {
|
|
163
|
+
const decompressedData = decompressData(node.compressedData);
|
|
164
|
+
if (decompressedData !== null) {
|
|
165
|
+
node.timestamp = Date.now();
|
|
166
|
+
this.moveToTail(node);
|
|
167
|
+
return decompressedData;
|
|
168
|
+
} else {
|
|
169
|
+
this.removeNode(node);
|
|
170
|
+
return void 0;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return void 0;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* 检查是否存在缓存
|
|
177
|
+
* @param key 缓存键
|
|
178
|
+
* @returns 是否存在
|
|
179
|
+
*/
|
|
180
|
+
has(key) {
|
|
181
|
+
return this.keyMap.has(key);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* 清除所有缓存
|
|
185
|
+
*/
|
|
186
|
+
clear() {
|
|
187
|
+
let current = this.head;
|
|
188
|
+
while (current) {
|
|
189
|
+
const next = current.next;
|
|
190
|
+
current.prev = null;
|
|
191
|
+
current.next = null;
|
|
192
|
+
current = next;
|
|
193
|
+
}
|
|
194
|
+
this.keyMap.clear();
|
|
195
|
+
this.head = null;
|
|
196
|
+
this.tail = null;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* 获取缓存大小
|
|
200
|
+
* @returns 当前缓存数量
|
|
201
|
+
*/
|
|
202
|
+
size() {
|
|
203
|
+
return this.keyMap.size;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* 获取缓存统计信息
|
|
207
|
+
* @returns 缓存统计信息
|
|
208
|
+
*/
|
|
209
|
+
getStats() {
|
|
210
|
+
return {
|
|
211
|
+
size: this.keyMap.size,
|
|
212
|
+
maxSize: this.maxSize,
|
|
213
|
+
usage: Math.round(this.keyMap.size / this.maxSize * 100)
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* 删除指定的缓存项
|
|
218
|
+
* @param key 要删除的缓存键
|
|
219
|
+
* @returns 是否删除成功
|
|
220
|
+
*/
|
|
221
|
+
delete(key) {
|
|
222
|
+
const node = this.keyMap.get(key);
|
|
223
|
+
if (node) {
|
|
224
|
+
this.removeNode(node);
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
var scanCache = new LRUCache();
|
|
231
|
+
var scanCache_default = scanCache;
|