@zenweb/cache 4.3.1 → 4.5.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/cache.d.ts CHANGED
@@ -18,14 +18,14 @@ export declare class CacheResult {
18
18
  */
19
19
  export declare class Cache {
20
20
  private redis;
21
- private option?;
22
- constructor(redis: Redis, option?: SetupOption | undefined);
21
+ private option;
22
+ constructor(redis: Redis, option: Required<SetupOption>);
23
23
  /**
24
24
  * 删除缓存
25
25
  */
26
26
  del(key: string): Promise<number>;
27
27
  /**
28
- * 直接设置缓存 - 不经过 JSON 序列化的数据
28
+ * 直接设置缓存 - 不经过序列化的数据
29
29
  * @param key
30
30
  * @param value
31
31
  * @param ttl 缓存有效时长 (秒),不设置取默认设置
@@ -34,7 +34,7 @@ export declare class Cache {
34
34
  /**
35
35
  * 缓存对象
36
36
  * @param key 缓存key
37
- * @param value 缓存值,除了 `Buffer` 以外的值会经过 JSON 序列化
37
+ * @param value 缓存值,除了 `Buffer` 以外的值会经过序列化
38
38
  * @param ttlopt 缓存有效时长 (秒),不设置取默认设置 | 缓存选项
39
39
  */
40
40
  set(key: string, value: any, ttlopt?: number | SetOption): Promise<SetResult>;
package/dist/cache.js CHANGED
@@ -40,7 +40,7 @@ class Cache {
40
40
  return this.redis.del(key);
41
41
  }
42
42
  /**
43
- * 直接设置缓存 - 不经过 JSON 序列化的数据
43
+ * 直接设置缓存 - 不经过序列化的数据
44
44
  * @param key
45
45
  * @param value
46
46
  * @param ttl 缓存有效时长 (秒),不设置取默认设置
@@ -54,19 +54,13 @@ class Cache {
54
54
  /**
55
55
  * 缓存对象
56
56
  * @param key 缓存key
57
- * @param value 缓存值,除了 `Buffer` 以外的值会经过 JSON 序列化
57
+ * @param value 缓存值,除了 `Buffer` 以外的值会经过序列化
58
58
  * @param ttlopt 缓存有效时长 (秒),不设置取默认设置 | 缓存选项
59
59
  */
60
60
  async set(key, value, ttlopt) {
61
- var _a;
62
61
  let compressed;
63
- let data = Buffer.isBuffer(value) ? value : JSON.stringify(value);
64
- const opt = Object.assign({
65
- ttl: 60,
66
- compressMinLength: 1024,
67
- compressStoreRatio: 0.95,
68
- compressLevel: 1,
69
- }, (_a = this.option) === null || _a === void 0 ? void 0 : _a.set, typeof ttlopt === 'object' ? ttlopt : undefined);
62
+ let data = Buffer.isBuffer(value) ? value : this.option.serializer.serialize(value);
63
+ const opt = Object.assign({}, this.option.set, typeof ttlopt === 'object' ? ttlopt : undefined);
70
64
  if (typeof ttlopt === 'number') {
71
65
  opt.ttl = ttlopt;
72
66
  }
@@ -107,17 +101,24 @@ class Cache {
107
101
  // 解析处理
108
102
  if (_opt.parse) {
109
103
  _getDebug('[%s] parse', key);
110
- return JSON.parse(data.toString());
104
+ return this.option.serializer.deserialize(data);
111
105
  }
112
106
  return new CacheResult(compressed, data);
113
107
  }
114
108
  }
115
109
  async lockGet(key, fetch, _opt) {
116
- var _a, _b;
117
- const opt = Object.assign({}, (_a = this.option) === null || _a === void 0 ? void 0 : _a.set, (_b = this.option) === null || _b === void 0 ? void 0 : _b.lockGet, _opt);
110
+ const opt = Object.assign({}, this.option.set, this.option.lockGet, _opt);
111
+ // 本地存储
112
+ if (opt.localStore && key in opt.localStore) {
113
+ return opt.localStore[key];
114
+ }
118
115
  // 获取数据并设置到缓存中
119
116
  const fetchAndSet = async () => {
120
117
  const obj = await fetch();
118
+ // 本地存储
119
+ if (opt.localStore) {
120
+ opt.localStore[key] = obj;
121
+ }
121
122
  const result = await this.set(key, obj, opt);
122
123
  if ((_opt === null || _opt === void 0 ? void 0 : _opt.parse) !== false) {
123
124
  return obj;
@@ -147,6 +148,10 @@ class Cache {
147
148
  if (!locker) {
148
149
  preRefresh();
149
150
  }
151
+ // 本地存储
152
+ if (opt.localStore) {
153
+ opt.localStore[key] = data;
154
+ }
150
155
  return data;
151
156
  }
152
157
  }
package/dist/index.js CHANGED
@@ -18,6 +18,7 @@ exports.Cache = exports.cached = exports.Locker = exports.$cache = exports.getCa
18
18
  const cache_1 = require("./cache");
19
19
  Object.defineProperty(exports, "Cache", { enumerable: true, get: function () { return cache_1.Cache; } });
20
20
  const ioredis_1 = require("ioredis");
21
+ const options_1 = require("./options");
21
22
  var global_1 = require("./global");
22
23
  Object.defineProperty(exports, "getCache", { enumerable: true, get: function () { return global_1.getCache; } });
23
24
  Object.defineProperty(exports, "$cache", { enumerable: true, get: function () { return global_1.$cache; } });
@@ -27,24 +28,13 @@ var locker_1 = require("./locker");
27
28
  Object.defineProperty(exports, "Locker", { enumerable: true, get: function () { return locker_1.Locker; } });
28
29
  var middleware_1 = require("./middleware");
29
30
  Object.defineProperty(exports, "cached", { enumerable: true, get: function () { return middleware_1.cached; } });
30
- /**
31
- * 默认选项
32
- */
33
- const defaultOption = {
34
- redis: {
35
- host: process.env.REDIS_HOST || '127.0.0.1',
36
- port: parseInt(process.env.REDIS_PORT || '') || 6379,
37
- password: process.env.REDIS_PASSWORD || '',
38
- db: parseInt(process.env.REDIS_DB || '') || 0,
39
- },
40
- };
41
31
  /**
42
32
  * 安装模块
43
33
  * @param option 配置
44
34
  */
45
35
  function setup(option) {
46
36
  return async function cache(setup) {
47
- const opt = Object.assign({}, defaultOption, option);
37
+ const opt = Object.assign({}, options_1.defaultSetupOption, option);
48
38
  setup.debug('option: %o', opt);
49
39
  const redis = opt.redis instanceof ioredis_1.Redis ? opt.redis : new ioredis_1.Redis(opt.redis);
50
40
  await redis.ping('check');
@@ -0,0 +1,7 @@
1
+ import type { RedisOptions } from "ioredis";
2
+ import type { LockGetOption, ObjectSerializer, SetOption, SetupOption } from "./types";
3
+ export declare const defaultRedisOption: RedisOptions;
4
+ export declare const defaultSetOption: SetOption;
5
+ export declare const defaultLockGetOption: LockGetOption;
6
+ export declare const defaultSerializer: ObjectSerializer;
7
+ export declare const defaultSetupOption: Required<SetupOption>;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultSetupOption = exports.defaultSerializer = exports.defaultLockGetOption = exports.defaultSetOption = exports.defaultRedisOption = void 0;
4
+ exports.defaultRedisOption = {
5
+ host: process.env.REDIS_HOST || '127.0.0.1',
6
+ port: parseInt(process.env.REDIS_PORT || '') || 6379,
7
+ password: process.env.REDIS_PASSWORD || '',
8
+ db: parseInt(process.env.REDIS_DB || '') || 0,
9
+ };
10
+ exports.defaultSetOption = {
11
+ ttl: 60,
12
+ compressMinLength: 1024,
13
+ compressStoreRatio: 0.95,
14
+ compressLevel: 1,
15
+ };
16
+ exports.defaultLockGetOption = {
17
+ preRefresh: 0,
18
+ refresh: false,
19
+ localStore: undefined,
20
+ };
21
+ exports.defaultSerializer = {
22
+ serialize: (data) => {
23
+ return Buffer.from(JSON.stringify(data));
24
+ },
25
+ deserialize: (data) => {
26
+ return JSON.parse(data.toString());
27
+ },
28
+ };
29
+ exports.defaultSetupOption = {
30
+ redis: exports.defaultRedisOption,
31
+ set: exports.defaultSetOption,
32
+ lockGet: exports.defaultLockGetOption,
33
+ serializer: exports.defaultSerializer,
34
+ };
package/dist/types.d.ts CHANGED
@@ -34,6 +34,13 @@ export interface SetOption {
34
34
  */
35
35
  compressLevel?: number;
36
36
  }
37
+ /**
38
+ * 对象序列化
39
+ */
40
+ export interface ObjectSerializer {
41
+ serialize(data: object): Buffer;
42
+ deserialize(data: Buffer): object;
43
+ }
37
44
  export interface SetupOption {
38
45
  /**
39
46
  * Redis 实例或 Redis 选项
@@ -47,13 +54,18 @@ export interface SetupOption {
47
54
  * 默认锁缓存选项
48
55
  */
49
56
  lockGet?: LockGetOption;
57
+ /**
58
+ * 自定义序列化器
59
+ * - 默认为 JSON 序列化
60
+ */
61
+ serializer?: ObjectSerializer;
50
62
  }
51
63
  /**
52
64
  * 取得缓存选项
53
65
  */
54
66
  export interface GetOption {
55
67
  /**
56
- * 是否解析 JSON 数据
68
+ * 是否反序列化数据为对象
57
69
  * @default true
58
70
  */
59
71
  parse?: boolean;
@@ -117,4 +129,13 @@ export interface LockGetOption extends GetOption, SetOption, LockOption {
117
129
  * @default false
118
130
  */
119
131
  refresh?: boolean;
132
+ /**
133
+ * 本地存储
134
+ * - 如果指定一个对象,则尝试先从对象中存取
135
+ * - 防止在同一次请求中重复从 Redis 获取
136
+ * - 一般情况下在 Context 中设置一个空对象并指定
137
+ */
138
+ localStore?: {
139
+ [key: string]: any;
140
+ };
120
141
  }
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.3.1",
4
+ "version": "4.5.0",
5
5
  "description": "Zenweb Cache module",
6
6
  "exports": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -11,7 +11,7 @@
11
11
  "scripts": {
12
12
  "vscode": "yarn dlx @yarnpkg/sdks vscode",
13
13
  "build": "rimraf dist && tsc",
14
- "prepublishOnly": "yarn run build",
14
+ "prepack": "yarn run build",
15
15
  "test": "ts-mocha -p tsconfig.json test/**/*.test.ts",
16
16
  "dev": "cd example && cross-env DEBUG=\\* NODE_ENV=development ts-node app"
17
17
  },