@zenweb/cache 4.6.0 → 4.7.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/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);
@@ -1,17 +1,10 @@
1
1
  import { Context, Middleware } from "@zenweb/core";
2
2
  import { LockGetOption } from "./types";
3
- interface CachedMiddlewareOption extends Omit<LockGetOption, 'parse' | 'decompress'> {
4
- /**
5
- * 输出内容类型
6
- * - 默认 json
7
- * - 需要指定输出类型,因为缓存的是 body 主体,并不包含类型
8
- */
9
- type?: string;
10
- }
3
+ type CachedMiddlewareOption = Omit<LockGetOption, 'parse' | 'decompress'>;
11
4
  /**
12
5
  * 缓存中间件
13
- * - 缓存中间件缓存的是 `ctx.body` 最终结果,所以可以缓存任何返回类型
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;
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.cached = void 0;
4
4
  /**
5
5
  * 缓存中间件
6
- * - 缓存中间件缓存的是 `ctx.body` 最终结果,所以可以缓存任何返回类型
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 Buffer.isBuffer(ctx.body) ? ctx.body : Buffer.from(ctx.body);
26
- }, {
27
- ..._opt,
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/dist/utils.js CHANGED
@@ -73,6 +73,12 @@ function cacheKey(...key) {
73
73
  return key.map(i => {
74
74
  if (typeof i === 'string')
75
75
  return i;
76
+ if (typeof i === 'number')
77
+ return String(i);
78
+ if (typeof i === 'undefined')
79
+ return '';
80
+ if (i instanceof Date)
81
+ return i.toJSON();
76
82
  return plainifyOrHash(i);
77
83
  }).join(':');
78
84
  }
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.6.0",
4
+ "version": "4.7.1",
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
  }