@zenweb/cache 4.0.0 → 4.0.1-2

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,4 +1,7 @@
1
1
  # Changelog
2
2
 
3
+ ## [4.0.1] - 2023-8-17
4
+ cached 中间件增加 type 参数
5
+
3
6
  ## [4.0.0] - 2023-8-8
4
7
  发布
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();
@@ -1,8 +1,18 @@
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
11
  /**
4
12
  * 缓存中间件
13
+ * - 缓存中间件缓存的是 ctx.body 最终结果,所以可以缓存任何返回类型
5
14
  * - 会根据客户端请求头判断是否需要解压
6
15
  * @param key 缓存 key 如果不指定则默认使用 ctx.path
7
16
  */
8
- export declare function cached(key?: string | ((ctx: Context) => string | Promise<string>), opt?: Omit<LockGetOption, 'parse' | 'decompress'>): Middleware;
17
+ export declare function cached(key?: string | ((ctx: Context) => string | Promise<string>), opt?: CachedMiddlewareOption): Middleware;
18
+ export {};
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.cached = void 0;
4
4
  /**
5
5
  * 缓存中间件
6
+ * - 缓存中间件缓存的是 ctx.body 最终结果,所以可以缓存任何返回类型
6
7
  * - 会根据客户端请求头判断是否需要解压
7
8
  * @param key 缓存 key 如果不指定则默认使用 ctx.path
8
9
  */
@@ -14,13 +15,16 @@ function cached(key, opt) {
14
15
  parse: false,
15
16
  decompress,
16
17
  });
17
- const result = await ctx.core.cache.lockGet(_key, async function () {
18
+ const result = await ctx.core.cache2.lockGet(_key, async function () {
18
19
  await next();
19
20
  return ctx.body;
20
21
  }, _opt);
21
22
  if (!decompress && result.compressed) {
22
23
  ctx.set('Content-Encoding', 'gzip');
23
24
  }
25
+ if (!ctx.type) {
26
+ ctx.type = (opt === null || opt === void 0 ? void 0 : opt.type) || 'json';
27
+ }
24
28
  ctx.body = result.data;
25
29
  };
26
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenweb/cache",
3
- "version": "4.0.0",
3
+ "version": "4.0.1-2",
4
4
  "description": "Zenweb Cache module",
5
5
  "exports": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",