@zenweb/cache 4.6.0 → 4.7.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/dist/index.js +1 -0
- package/dist/middleware.d.ts +3 -10
- package/dist/middleware.js +5 -19
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -34,6 +34,7 @@ Object.defineProperty(exports, "cached", { enumerable: true, get: function () {
|
|
|
34
34
|
*/
|
|
35
35
|
function setup(option) {
|
|
36
36
|
return async function cache(setup) {
|
|
37
|
+
setup.assertModuleExists('result', 'need setup @zenweb/result');
|
|
37
38
|
const opt = Object.assign({}, options_1.defaultSetupOption, option);
|
|
38
39
|
setup.debug('option: %o', opt);
|
|
39
40
|
const redis = opt.redis instanceof ioredis_1.Redis ? opt.redis : new ioredis_1.Redis(opt.redis);
|
package/dist/middleware.d.ts
CHANGED
|
@@ -1,17 +1,10 @@
|
|
|
1
1
|
import { Context, Middleware } from "@zenweb/core";
|
|
2
2
|
import { LockGetOption } from "./types";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 输出内容类型
|
|
6
|
-
* - 默认 json
|
|
7
|
-
* - 需要指定输出类型,因为缓存的是 body 主体,并不包含类型
|
|
8
|
-
*/
|
|
9
|
-
type?: string;
|
|
10
|
-
}
|
|
3
|
+
type CachedMiddlewareOption = Omit<LockGetOption, 'parse' | 'decompress'>;
|
|
11
4
|
/**
|
|
12
5
|
* 缓存中间件
|
|
13
|
-
* -
|
|
14
|
-
* -
|
|
6
|
+
* - 缓存 ctx.success() 结果
|
|
7
|
+
* - fail() 时不缓存
|
|
15
8
|
* @param key 不指定 `key` 则默认使用 `ctx.path`,`key: null` 不适用使用缓存
|
|
16
9
|
*/
|
|
17
10
|
export declare function cached(key?: string | null | ((ctx: Context) => string | null | Promise<string | null>), opt?: CachedMiddlewareOption): Middleware;
|
package/dist/middleware.js
CHANGED
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.cached = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* 缓存中间件
|
|
6
|
-
* -
|
|
7
|
-
* -
|
|
6
|
+
* - 缓存 ctx.success() 结果
|
|
7
|
+
* - fail() 时不缓存
|
|
8
8
|
* @param key 不指定 `key` 则默认使用 `ctx.path`,`key: null` 不适用使用缓存
|
|
9
9
|
*/
|
|
10
10
|
function cached(key, opt) {
|
|
@@ -16,25 +16,11 @@ function cached(key, opt) {
|
|
|
16
16
|
if (!_key) {
|
|
17
17
|
return next();
|
|
18
18
|
}
|
|
19
|
-
const decompress = ctx.acceptsEncodings('gzip') === false;
|
|
20
|
-
const _opt = Object.assign({
|
|
21
|
-
type: 'json'
|
|
22
|
-
}, opt);
|
|
23
19
|
const result = await ctx.core.cache2.lockGet(_key, async function () {
|
|
24
20
|
await next();
|
|
25
|
-
return
|
|
26
|
-
},
|
|
27
|
-
|
|
28
|
-
parse: false,
|
|
29
|
-
decompress,
|
|
30
|
-
});
|
|
31
|
-
if (result.compressed) {
|
|
32
|
-
ctx.set('Content-Encoding', 'gzip');
|
|
33
|
-
}
|
|
34
|
-
if (_opt.type) {
|
|
35
|
-
ctx.type = _opt.type;
|
|
36
|
-
}
|
|
37
|
-
ctx.body = result.data;
|
|
21
|
+
return ctx.successData;
|
|
22
|
+
}, opt);
|
|
23
|
+
ctx.success(result);
|
|
38
24
|
};
|
|
39
25
|
}
|
|
40
26
|
exports.cached = cached;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenweb/cache",
|
|
3
3
|
"packageManager": "yarn@4.0.2",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.7.0",
|
|
5
5
|
"description": "Zenweb Cache module",
|
|
6
6
|
"exports": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@zenweb/core": "^4.2.3",
|
|
48
|
+
"@zenweb/result": "^4.1.2",
|
|
48
49
|
"ioredis": "^5.3.2"
|
|
49
50
|
}
|
|
50
51
|
}
|