@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 +6 -0
- package/dist/global.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/middleware.js +1 -1
- package/dist/utils.d.ts +5 -0
- package/dist/utils.js +39 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/global.js
CHANGED
package/dist/index.d.ts
CHANGED
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('
|
|
52
|
-
setup.defineCoreProperty('
|
|
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();
|
package/dist/middleware.js
CHANGED
|
@@ -15,7 +15,7 @@ function cached(key, opt) {
|
|
|
15
15
|
parse: false,
|
|
16
16
|
decompress,
|
|
17
17
|
});
|
|
18
|
-
const result = await ctx.core.
|
|
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;
|