@abtnode/util 1.17.8-beta-20260108-224855-28496abb → 1.17.8-beta-20260111-112953-aed5ff39

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.
@@ -0,0 +1,85 @@
1
+ const { DBCache, getAbtNodeRedisAndSQLiteUrl } = require('@abtnode/db-cache');
2
+ const { BLOCKLET_CACHE_TTL } = require('@abtnode/constant');
3
+
4
+ const BLOCKLET_INFO_SHORT_CACHE_TTL = 1000 * 60 * 30; // 30 minutes
5
+ const logger = console;
6
+
7
+ /**
8
+ * Blocklet Info 缓存, 用于 blocklet.js 请求,快速返回数据
9
+ *
10
+ * 注意:
11
+ * - Redis Adapter 不使用 LRU cache(Redis 本身已经是内存数据库)
12
+ * - SQLite Adapter 会使用 LRU cache 作为 L1 缓存层
13
+ */
14
+ const blockletInfoShortCache = new DBCache(() => ({
15
+ prefix: 'blocklet-info-short-cache',
16
+ ttl: BLOCKLET_INFO_SHORT_CACHE_TTL, // 30 minutes
17
+ enableLruCache: true, // 仅对 SQLite 生效
18
+ ...getAbtNodeRedisAndSQLiteUrl(),
19
+ }));
20
+
21
+ /**
22
+ * @param {string} did: blockletDid
23
+ * @param {string} componentId: componentDid 的格式为 did/children.did
24
+ * @param {string} type: json or js
25
+ * @returns {string}
26
+ */
27
+ const getBlockletInfoCachePrefixKey = (did) => {
28
+ return `blocklet-info-${did}`;
29
+ };
30
+
31
+ /**
32
+ * 删除 Blocklet Info 缓存 (包括内存缓存和 DBCache)
33
+ *
34
+ * CLUSTER MODE NOTE:
35
+ * - Memory cache: Cleared via delByPrefix with cluster sync.
36
+ * - DBCache: Cleared once (Redis/SQLite handles multi-process access).
37
+ *
38
+ * @param {string} did: blockletDid
39
+ */
40
+ const clearBlockletInfoCache = async (did) => {
41
+ try {
42
+ // 由于 cacheKey 包含动态参数(pathPrefix 等),无法枚举所有可能的 key
43
+ // 所以使用前缀匹配删除所有与该 DID 相关的缓存
44
+ const prefix = getBlockletInfoCachePrefixKey(did);
45
+ // DBCache 也使用前缀删除(del 方法会删除该 key 及其 group 下的所有 key)
46
+ await blockletInfoShortCache.del(prefix);
47
+ logger.info('Cleared blocklet info cache', { did, prefix });
48
+ } catch (error) {
49
+ logger.error('Clear blocklet info cache failed', { did, error });
50
+ }
51
+ };
52
+
53
+ /**
54
+ * Blocklet state 缓存, 用于内部数据获取,避免重复查询数据库
55
+ */
56
+ const blockletCache = new DBCache(() => ({
57
+ prefix: 'blocklet-state',
58
+ ttl: BLOCKLET_CACHE_TTL,
59
+ enableLruCache: false, // 明确禁用 LRU cache
60
+ ...getAbtNodeRedisAndSQLiteUrl(),
61
+ }));
62
+
63
+ /**
64
+ * 删除 Blocklet state 缓存
65
+ * @param {string} did: blockletDid
66
+ */
67
+ const deleteBlockletCache = async (did) => {
68
+ try {
69
+ await blockletCache.del(did);
70
+ logger.info('Deleted blocklet cache', { did });
71
+ } catch (error) {
72
+ logger.error('deleteBlockletCache failed', { did, error });
73
+ }
74
+ };
75
+
76
+ module.exports = {
77
+ // Blocklet state cache
78
+ blockletCache,
79
+ deleteBlockletCache,
80
+
81
+ // Blocklet info cache
82
+ blockletInfoShortCache,
83
+ getBlockletInfoCachePrefixKey,
84
+ clearBlockletInfoCache,
85
+ };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.17.8-beta-20260108-224855-28496abb",
6
+ "version": "1.17.8-beta-20260111-112953-aed5ff39",
7
7
  "description": "ArcBlock's JavaScript utility",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -18,14 +18,14 @@
18
18
  "author": "polunzh <polunzh@gmail.com> (http://github.com/polunzh)",
19
19
  "license": "Apache-2.0",
20
20
  "dependencies": {
21
- "@abtnode/constant": "1.17.8-beta-20260108-224855-28496abb",
22
- "@abtnode/db-cache": "1.17.8-beta-20260108-224855-28496abb",
21
+ "@abtnode/constant": "1.17.8-beta-20260111-112953-aed5ff39",
22
+ "@abtnode/db-cache": "1.17.8-beta-20260111-112953-aed5ff39",
23
23
  "@arcblock/did": "^1.28.4",
24
24
  "@arcblock/event-hub": "^1.28.4",
25
25
  "@arcblock/pm2": "^6.0.12",
26
- "@blocklet/constant": "1.17.8-beta-20260108-224855-28496abb",
26
+ "@blocklet/constant": "1.17.8-beta-20260111-112953-aed5ff39",
27
27
  "@blocklet/error": "^0.3.5",
28
- "@blocklet/meta": "1.17.8-beta-20260108-224855-28496abb",
28
+ "@blocklet/meta": "1.17.8-beta-20260111-112953-aed5ff39",
29
29
  "@blocklet/xss": "^0.3.16",
30
30
  "@ocap/client": "^1.28.4",
31
31
  "@ocap/mcrypto": "^1.28.4",
@@ -90,5 +90,5 @@
90
90
  "express": "^4.18.2",
91
91
  "fs-extra": "^11.2.0"
92
92
  },
93
- "gitHead": "e4407556f8c8f909afdfc6abc05228b0b495dcbf"
93
+ "gitHead": "b4c76fd49b8c98c9554c37c798f58ddf334fed4c"
94
94
  }