chanjs 2.8.8 → 2.8.9

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/storage/cache.js +19 -0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "chanjs",
4
- "version": "2.8.8",
4
+ "version": "2.8.9",
5
5
  "description": "chanjs基于 Node.js + Express 5 的标准 HMVC 框架(NHMVC),纯 JavaScript(ESM)开发。",
6
6
  "main": "index.js",
7
7
  "module": "index.js",
package/storage/cache.js CHANGED
@@ -46,6 +46,25 @@ class Cache {
46
46
  clear = () => this.map.clear();
47
47
  size() { return this.map.size; }
48
48
 
49
+ /**
50
+ * 按 key 前缀批量删除(解耦调用方对 cache.map 内部结构的依赖)
51
+ * 先收集匹配 key 再统一删除,避免迭代中改 Map 的行为差异。
52
+ * @param {string} prefix - key 前缀(如 "web:" / "home:")
53
+ * @returns {number} 实际删除的 key 数量
54
+ */
55
+ clearByPrefix(prefix) {
56
+ if (typeof prefix !== "string" || prefix === "") return 0;
57
+ const toDelete = [];
58
+ for (const key of this.map.keys()) {
59
+ if (typeof key === "string" && key.startsWith(prefix)) toDelete.push(key);
60
+ }
61
+ let count = 0;
62
+ for (const key of toDelete) {
63
+ if (this.map.delete(key)) count++;
64
+ }
65
+ return count;
66
+ }
67
+
49
68
  /** 访问并刷新LRU,过期项自动清理(单次 delete+set 维护 Map 插入序) */
50
69
  _touch(key) {
51
70
  const item = this.map.get(key);