@zenweb/cache 4.0.1 → 4.1.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## [4.1.1] - 2023-8-18
4
+ 增加 normalKey number 参数
5
+
6
+ ## [4.1.0] - 2023-8-18
7
+ 增加 normalKey 方法
8
+
3
9
  ## [4.0.1] - 2023-8-17
4
10
  cached 中间件增加 type 参数
5
11
 
package/dist/global.js CHANGED
@@ -7,7 +7,7 @@ const core_1 = require("@zenweb/core");
7
7
  */
8
8
  function getCache() {
9
9
  const ins = (0, core_1.getCore)();
10
- return ins.cache;
10
+ return ins.cache2;
11
11
  }
12
12
  exports.getCache = getCache;
13
13
  /**
package/dist/index.d.ts CHANGED
@@ -18,10 +18,10 @@ declare module '@zenweb/core' {
18
18
  /**
19
19
  * Redis 实例
20
20
  */
21
- redis: Redis;
21
+ redis2: Redis;
22
22
  /**
23
23
  * 缓存实例
24
24
  */
25
- cache: Cache;
25
+ cache2: Cache;
26
26
  }
27
27
  }
package/dist/index.js CHANGED
@@ -48,8 +48,8 @@ function setup(option) {
48
48
  setup.debug('option: %o', opt);
49
49
  const redis = opt.redis instanceof ioredis_1.Redis ? opt.redis : new ioredis_1.Redis(opt.redis);
50
50
  await redis.ping('check');
51
- setup.defineCoreProperty('redis', { value: redis });
52
- setup.defineCoreProperty('cache', { value: new cache_1.Cache(redis, opt) });
51
+ setup.defineCoreProperty('redis2', { value: redis });
52
+ setup.defineCoreProperty('cache2', { value: new cache_1.Cache(redis, opt) });
53
53
  setup.destroy(async () => {
54
54
  setup.debug('quit redis...');
55
55
  await redis.quit();
@@ -15,7 +15,7 @@ function cached(key, opt) {
15
15
  parse: false,
16
16
  decompress,
17
17
  });
18
- const result = await ctx.core.cache.lockGet(_key, async function () {
18
+ const result = await ctx.core.cache2.lockGet(_key, async function () {
19
19
  await next();
20
20
  return ctx.body;
21
21
  }, _opt);
package/dist/utils.d.ts CHANGED
@@ -17,3 +17,8 @@ export declare function sleep(ms: number): Promise<void>;
17
17
  * @returns 执行结果
18
18
  */
19
19
  export declare function runRedisScript(redis: Redis, hash: string, script: string, keys: RedisKey[], values?: RedisValue[]): Promise<unknown>;
20
+ /**
21
+ * 生成一个参数缓存key
22
+ * - 多个元素用 : 连接
23
+ */
24
+ export declare function normalKey(...key: (string | number | object)[]): string;
package/dist/utils.js CHANGED
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.runRedisScript = exports.sleep = exports.debug = void 0;
3
+ exports.normalKey = exports.runRedisScript = exports.sleep = exports.debug = void 0;
4
4
  const debug_1 = require("debug");
5
+ const crypto = require("crypto");
5
6
  exports.debug = (0, debug_1.default)('zenweb:cache');
6
7
  /**
7
8
  * Promise 等待
@@ -39,3 +40,40 @@ async function runRedisScript(redis, hash, script, keys, values = []) {
39
40
  }
40
41
  }
41
42
  exports.runRedisScript = runRedisScript;
43
+ /**
44
+ * 将一个嵌套对象转换为平面字符串结构
45
+ */
46
+ function plainify(obj) {
47
+ if (Array.isArray(obj)) {
48
+ return obj.map(plainify).sort().join(',');
49
+ }
50
+ if (obj && typeof obj === 'object') {
51
+ return Object.keys(obj).sort().map(k => `${k}=${plainify(obj[k])}`).join('&');
52
+ }
53
+ return String(obj);
54
+ }
55
+ /**
56
+ * 对象使用 `plainify` 处理,如果对象过长则使用摘要
57
+ * @param obj
58
+ * @returns
59
+ */
60
+ function plainifyOrHash(obj) {
61
+ let p = plainify(obj);
62
+ if (Buffer.byteLength(p, 'utf8') > 16) {
63
+ // 参数太长只取摘要
64
+ p = crypto.createHash('sha1').update(p, 'utf8').digest().subarray(0, 10).toString('base64url');
65
+ }
66
+ return p;
67
+ }
68
+ /**
69
+ * 生成一个参数缓存key
70
+ * - 多个元素用 : 连接
71
+ */
72
+ function normalKey(...key) {
73
+ return key.map(i => {
74
+ if (typeof i === 'string')
75
+ return i;
76
+ return plainifyOrHash(i);
77
+ }).join(':');
78
+ }
79
+ exports.normalKey = normalKey;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenweb/cache",
3
- "version": "4.0.1",
3
+ "version": "4.1.1",
4
4
  "description": "Zenweb Cache module",
5
5
  "exports": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",