@zenweb/cache 4.2.1 → 4.3.0
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 +7 -0
- package/dist/cache.d.ts +5 -5
- package/dist/cache.js +3 -3
- package/dist/middleware.js +6 -5
- package/dist/types.d.ts +3 -1
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +3 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/cache.d.ts
CHANGED
|
@@ -34,10 +34,10 @@ export declare class Cache {
|
|
|
34
34
|
/**
|
|
35
35
|
* 缓存对象
|
|
36
36
|
* @param key 缓存key
|
|
37
|
-
* @param value
|
|
37
|
+
* @param value 缓存值,除了 `Buffer` 以外的值会经过 JSON 序列化
|
|
38
38
|
* @param ttlopt 缓存有效时长 (秒),不设置取默认设置 | 缓存选项
|
|
39
39
|
*/
|
|
40
|
-
set(key: string, value:
|
|
40
|
+
set(key: string, value: any, ttlopt?: number | SetOption): Promise<SetResult>;
|
|
41
41
|
/**
|
|
42
42
|
* 直接取得缓存
|
|
43
43
|
*/
|
|
@@ -46,7 +46,7 @@ export declare class Cache {
|
|
|
46
46
|
* 取得缓存对象
|
|
47
47
|
* @param key 缓存key
|
|
48
48
|
*/
|
|
49
|
-
get
|
|
49
|
+
get(key: string, opt: {
|
|
50
50
|
parse: false;
|
|
51
51
|
} & GetOption): Promise<CacheResult | undefined>;
|
|
52
52
|
get<T = unknown>(key: string, opt?: {
|
|
@@ -59,10 +59,10 @@ export declare class Cache {
|
|
|
59
59
|
* @param key 缓存KEY
|
|
60
60
|
* @param fetch 缓存设置方法回调
|
|
61
61
|
*/
|
|
62
|
-
lockGet<T
|
|
62
|
+
lockGet<T = unknown>(key: string, fetch: () => Promise<T> | T, _opt: {
|
|
63
63
|
parse: false;
|
|
64
64
|
} & LockGetOption): Promise<CacheResult>;
|
|
65
|
-
lockGet<T
|
|
65
|
+
lockGet<T = unknown>(key: string, fetch: () => Promise<T> | T, _opt?: {
|
|
66
66
|
parse?: true;
|
|
67
67
|
} & LockGetOption): Promise<T>;
|
|
68
68
|
/**
|
package/dist/cache.js
CHANGED
|
@@ -54,13 +54,13 @@ class Cache {
|
|
|
54
54
|
/**
|
|
55
55
|
* 缓存对象
|
|
56
56
|
* @param key 缓存key
|
|
57
|
-
* @param value
|
|
57
|
+
* @param value 缓存值,除了 `Buffer` 以外的值会经过 JSON 序列化
|
|
58
58
|
* @param ttlopt 缓存有效时长 (秒),不设置取默认设置 | 缓存选项
|
|
59
59
|
*/
|
|
60
60
|
async set(key, value, ttlopt) {
|
|
61
61
|
var _a;
|
|
62
62
|
let compressed;
|
|
63
|
-
let data = Buffer.isBuffer(value) ? value :
|
|
63
|
+
let data = Buffer.isBuffer(value) ? value : JSON.stringify(value);
|
|
64
64
|
const opt = Object.assign({
|
|
65
65
|
ttl: 60,
|
|
66
66
|
compressMinLength: 1024,
|
|
@@ -138,7 +138,7 @@ class Cache {
|
|
|
138
138
|
// 使用锁设置缓存
|
|
139
139
|
let locker;
|
|
140
140
|
let refresh = opt.refresh || false; // 强制刷新
|
|
141
|
-
let retryCount = typeof opt.retryCount === 'number' ? opt.retryCount :
|
|
141
|
+
let retryCount = typeof opt.retryCount === 'number' ? opt.retryCount : 10;
|
|
142
142
|
while (true) {
|
|
143
143
|
if (!refresh) {
|
|
144
144
|
// 尝试从 redis 中获取
|
package/dist/middleware.js
CHANGED
|
@@ -19,14 +19,15 @@ function cached(key, opt) {
|
|
|
19
19
|
const decompress = ctx.acceptsEncodings('gzip') === false;
|
|
20
20
|
const _opt = Object.assign({
|
|
21
21
|
type: 'json'
|
|
22
|
-
}, opt
|
|
22
|
+
}, opt);
|
|
23
|
+
const result = await ctx.core.cache2.lockGet(_key, async function () {
|
|
24
|
+
await next();
|
|
25
|
+
return Buffer.isBuffer(ctx.body) ? ctx.body : Buffer.from(ctx.body);
|
|
26
|
+
}, {
|
|
27
|
+
..._opt,
|
|
23
28
|
parse: false,
|
|
24
29
|
decompress,
|
|
25
30
|
});
|
|
26
|
-
const result = await ctx.core.cache2.lockGet(_key, async function () {
|
|
27
|
-
await next();
|
|
28
|
-
return ctx.body;
|
|
29
|
-
}, _opt);
|
|
30
31
|
if (result.compressed) {
|
|
31
32
|
ctx.set('Content-Encoding', 'gzip');
|
|
32
33
|
}
|
package/dist/types.d.ts
CHANGED
package/dist/utils.d.ts
CHANGED
|
@@ -21,4 +21,4 @@ export declare function runRedisScript(redis: Redis, hash: string, script: strin
|
|
|
21
21
|
* 生成一个参数缓存key
|
|
22
22
|
* - 多个元素用 : 连接
|
|
23
23
|
*/
|
|
24
|
-
export declare function
|
|
24
|
+
export declare function cacheKey(...key: (string | number | object)[]): string;
|
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.cacheKey = exports.runRedisScript = exports.sleep = exports.debug = void 0;
|
|
4
4
|
const debug_1 = require("debug");
|
|
5
5
|
const crypto = require("crypto");
|
|
6
6
|
exports.debug = (0, debug_1.default)('zenweb:cache');
|
|
@@ -69,11 +69,11 @@ function plainifyOrHash(obj) {
|
|
|
69
69
|
* 生成一个参数缓存key
|
|
70
70
|
* - 多个元素用 : 连接
|
|
71
71
|
*/
|
|
72
|
-
function
|
|
72
|
+
function cacheKey(...key) {
|
|
73
73
|
return key.map(i => {
|
|
74
74
|
if (typeof i === 'string')
|
|
75
75
|
return i;
|
|
76
76
|
return plainifyOrHash(i);
|
|
77
77
|
}).join(':');
|
|
78
78
|
}
|
|
79
|
-
exports.
|
|
79
|
+
exports.cacheKey = cacheKey;
|