koatty_cacheable 1.3.8 → 1.4.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/CHANGELOG.md CHANGED
@@ -2,6 +2,24 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.4.1](https://github.com/thinkkoa/koatty_cacheable/compare/v1.4.0...v1.4.1) (2023-08-04)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * default value ([38f2e57](https://github.com/thinkkoa/koatty_cacheable/commit/38f2e57d52d13482907ca9eac6006d1e841bdadd))
11
+ * 初始化缓存改为appReady执行 ([7e9b1ab](https://github.com/thinkkoa/koatty_cacheable/commit/7e9b1abe6acdbdf1b6ad34efa053cbaca1e7b6d7))
12
+
13
+ ## [1.4.0](https://github.com/thinkkoa/koatty_cacheable/compare/v1.3.8...v1.4.0) (2023-02-18)
14
+
15
+
16
+ ### Bug Fixes
17
+
18
+ * cache subkey ([408e3c7](https://github.com/thinkkoa/koatty_cacheable/commit/408e3c709a4dfff6e7d224a22d26e58854a805ac))
19
+ * 启动时链接 ([e7ec709](https://github.com/thinkkoa/koatty_cacheable/commit/e7ec7094be106e4e48783c9b214b8369e8a18297))
20
+ * 支持引入变量做缓存key ([4ce81aa](https://github.com/thinkkoa/koatty_cacheable/commit/4ce81aaaf9610f4a62ddf43e3c4389dd003c2db8))
21
+ * 获取单例 ([0759972](https://github.com/thinkkoa/koatty_cacheable/commit/0759972adf7e86312f60f0bf77604bddf06ce0de))
22
+
5
23
  ### [1.3.8](https://github.com/thinkkoa/koatty_cacheable/compare/v1.3.6...v1.3.8) (2023-01-13)
6
24
 
7
25
  ### [1.3.7](https://github.com/thinkkoa/koatty_cacheable/compare/v1.3.6...v1.3.7) (2022-05-27)
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2023-01-13 14:17:21
3
+ * @Date: 2023-08-04 14:37:52
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -15,20 +15,50 @@ import { CacheStore } from 'koatty_store';
15
15
  *
16
16
  * @export
17
17
  * @param {string} cacheName cache name
18
- * @param {number} [timeout=3600] cache timeout
18
+ * @param {CacheAbleOpt} [opt] cache options
19
+ * e.g:
20
+ * {
21
+ * params: ["id"],
22
+ * timeout: 30
23
+ * }
24
+ * Use the 'id' parameters of the method as cache subkeys, the cache expiration time 30s
19
25
  * @returns {MethodDecorator}
20
26
  */
21
- export declare function CacheAble(cacheName: string, timeout?: number): MethodDecorator;
27
+ export declare function CacheAble(cacheName: string, opt?: CacheAbleOpt): MethodDecorator;
28
+
29
+ /**
30
+ * @description:
31
+ * @return {*}
32
+ */
33
+ export declare interface CacheAbleOpt {
34
+ params?: string[];
35
+ timeout?: number;
36
+ }
22
37
 
23
38
  /**
24
39
  * Decorating the execution of this method will trigger a cache clear operation. Redis server config from db.ts.
25
40
  *
26
41
  * @export
27
42
  * @param {string} cacheName cacheName cache name
28
- * @param {eventTimes} [eventTime="Before"]
43
+ * @param {CacheEvictOpt} [opt] cache options
44
+ * e.g:
45
+ * {
46
+ * params: ["id"],
47
+ * eventTime: "Before"
48
+ * }
49
+ * Use the 'id' parameters of the method as cache subkeys, and clear the cache before the method executed
29
50
  * @returns
30
51
  */
31
- export declare function CacheEvict(cacheName: string, eventTime?: eventTimes): (target: any, methodName: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
52
+ export declare function CacheEvict(cacheName: string, opt?: CacheEvictOpt): (target: any, methodName: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
53
+
54
+ /**
55
+ * @description:
56
+ * @return {*}
57
+ */
58
+ export declare interface CacheEvictOpt {
59
+ params?: string[];
60
+ eventTime?: "Before";
61
+ }
32
62
 
33
63
  /**
34
64
  *
@@ -36,7 +66,7 @@ export declare function CacheEvict(cacheName: string, eventTime?: eventTimes): (
36
66
  export declare type eventTimes = "Before" | "After";
37
67
 
38
68
  /**
39
- * get instances of cacheStore
69
+ * get instances of storeCache
40
70
  *
41
71
  * @export
42
72
  * @param {Application} app
package/dist/index.js CHANGED
@@ -1,76 +1,62 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2023-01-13 14:17:07
3
+ * @Date: 2023-08-04 14:37:39
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
7
7
  */
8
8
  'use strict';
9
9
 
10
- var helper = require('koatty_lib');
10
+ var koatty_lib = require('koatty_lib');
11
11
  var koatty_logger = require('koatty_logger');
12
12
  var koatty_store = require('koatty_store');
13
13
  var koatty_container = require('koatty_container');
14
14
 
15
- function _interopNamespaceDefault(e) {
16
- var n = Object.create(null);
17
- if (e) {
18
- Object.keys(e).forEach(function (k) {
19
- if (k !== 'default') {
20
- var d = Object.getOwnPropertyDescriptor(e, k);
21
- Object.defineProperty(n, k, d.get ? d : {
22
- enumerable: true,
23
- get: function () { return e[k]; }
24
- });
25
- }
26
- });
27
- }
28
- n.default = e;
29
- return Object.freeze(n);
30
- }
31
-
32
- var helper__namespace = /*#__PURE__*/_interopNamespaceDefault(helper);
33
-
34
15
  /*
35
16
  * @Author: richen
36
17
  * @Date: 2020-07-06 19:53:43
37
- * @LastEditTime: 2023-01-13 14:16:02
18
+ * @LastEditTime: 2023-08-04 14:33:58
38
19
  * @Description:
39
20
  * @Copyright (c) - <richenlin(at)gmail.com>
40
21
  */
41
- // cacheStore
42
- const cacheStore = {
22
+ const PreKey = "k";
23
+ // storeCache
24
+ const storeCache = {
43
25
  store: null
44
26
  };
45
27
  /**
46
- * get instances of cacheStore
28
+ * get instances of storeCache
47
29
  *
48
30
  * @export
49
31
  * @param {Application} app
50
32
  * @returns {*} {CacheStore}
51
33
  */
52
34
  async function GetCacheStore(app) {
53
- var _a;
54
- if (cacheStore.store && cacheStore.store.getConnection) {
55
- return cacheStore.store;
35
+ var _a, _b;
36
+ if (storeCache.store && storeCache.store.getConnection) {
37
+ return storeCache.store;
56
38
  }
57
- const opt = (_a = app.config("CacheStore", "db")) !== null && _a !== void 0 ? _a : {};
58
- if (helper__namespace.isEmpty(opt)) {
39
+ const opt = (_b = (_a = app.config("CacheStore")) !== null && _a !== void 0 ? _a : app.config("CacheStore", "db")) !== null && _b !== void 0 ? _b : {};
40
+ if (koatty_lib.Helper.isEmpty(opt)) {
59
41
  koatty_logger.DefaultLogger.Warn(`Missing CacheStore server configuration. Please write a configuration item with the key name 'CacheStore' in the db.ts file.`);
60
42
  }
61
- cacheStore.store = koatty_store.Store.getInstance(opt);
62
- if (!helper__namespace.isFunction(cacheStore.store.getConnection)) {
43
+ storeCache.store = koatty_store.CacheStore.getInstance(opt);
44
+ if (!koatty_lib.Helper.isFunction(storeCache.store.getConnection)) {
63
45
  throw Error(`CacheStore connection failed. `);
64
46
  }
65
- return cacheStore.store;
47
+ await storeCache.store.client.getConnection();
48
+ return storeCache.store;
66
49
  }
67
50
  /**
68
51
  * initiation CacheStore connection and client.
69
52
  *
70
53
  */
71
54
  async function InitCacheStore() {
55
+ if (storeCache.store !== null) {
56
+ return;
57
+ }
72
58
  const app = koatty_container.IOCContainer.getApp();
73
- app && app.once("appStart", async function () {
59
+ app && app.once("appReady", async function () {
74
60
  await GetCacheStore(app);
75
61
  });
76
62
  }
@@ -81,18 +67,33 @@ async function InitCacheStore() {
81
67
  *
82
68
  * @export
83
69
  * @param {string} cacheName cache name
84
- * @param {number} [timeout=3600] cache timeout
70
+ * @param {CacheAbleOpt} [opt] cache options
71
+ * e.g:
72
+ * {
73
+ * params: ["id"],
74
+ * timeout: 30
75
+ * }
76
+ * Use the 'id' parameters of the method as cache subkeys, the cache expiration time 30s
85
77
  * @returns {MethodDecorator}
86
78
  */
87
- function CacheAble(cacheName, timeout = 3600) {
79
+ function CacheAble(cacheName, opt = {
80
+ params: [],
81
+ timeout: 3600,
82
+ }) {
88
83
  return (target, methodName, descriptor) => {
89
84
  const componentType = koatty_container.IOCContainer.getType(target);
90
85
  if (componentType !== "SERVICE" && componentType !== "COMPONENT") {
91
86
  throw Error("This decorator only used in the service、component class.");
92
87
  }
93
- let identifier = koatty_container.IOCContainer.getIdentifier(target);
94
- identifier = identifier || (target.constructor ? (target.constructor.name || "") : "");
95
88
  const { value, configurable, enumerable } = descriptor;
89
+ opt = {
90
+ ...{
91
+ params: [],
92
+ timeout: 3600,
93
+ }, ...opt
94
+ };
95
+ // 获取定义的参数位置
96
+ const paramIndexes = getParamIndex(opt.params, getArgs(value));
96
97
  descriptor = {
97
98
  configurable,
98
99
  enumerable,
@@ -106,26 +107,35 @@ function CacheAble(cacheName, timeout = 3600) {
106
107
  });
107
108
  if (cacheFlag) {
108
109
  // tslint:disable-next-line: one-variable-per-declaration
109
- let key = "", res;
110
+ let key = PreKey;
110
111
  if (props && props.length > 0) {
111
- key = `${identifier}:${methodName}:${helper__namespace.murmurHash(JSON.stringify(props))}`;
112
+ for (const item of paramIndexes) {
113
+ if (props[item] !== undefined) {
114
+ const value = koatty_lib.Helper.toString(props[item]);
115
+ key += `:${value}`;
116
+ }
117
+ }
118
+ // 防止key超长
119
+ if (key.length > 32) {
120
+ key = koatty_lib.Helper.murmurHash(key);
121
+ }
112
122
  }
113
- else {
114
- key = `${identifier}:${methodName}`;
115
- }
116
- res = await store.hget(cacheName, key).catch(() => null);
117
- if (!helper__namespace.isEmpty(res)) {
123
+ let res = await store.get(`${cacheName}:${key}`).catch(() => null);
124
+ if (!koatty_lib.Helper.isEmpty(res)) {
118
125
  return JSON.parse(res);
119
126
  }
120
127
  // tslint:disable-next-line: no-invalid-this
121
128
  res = await value.apply(this, props);
122
129
  // prevent cache penetration
123
- if (helper__namespace.isEmpty(res)) {
130
+ if (koatty_lib.Helper.isEmpty(res)) {
124
131
  res = "";
125
- timeout = 60;
132
+ opt.timeout = 5;
133
+ }
134
+ if (!opt.timeout) {
135
+ opt.timeout = 3600;
126
136
  }
127
137
  // async set store
128
- store.hset(cacheName, key, JSON.stringify(res), timeout).catch(() => null);
138
+ store.set(`${cacheName}:${key}`, JSON.stringify(res), opt.timeout).catch(() => null);
129
139
  return res;
130
140
  }
131
141
  else {
@@ -144,17 +154,31 @@ function CacheAble(cacheName, timeout = 3600) {
144
154
  *
145
155
  * @export
146
156
  * @param {string} cacheName cacheName cache name
147
- * @param {eventTimes} [eventTime="Before"]
157
+ * @param {CacheEvictOpt} [opt] cache options
158
+ * e.g:
159
+ * {
160
+ * params: ["id"],
161
+ * eventTime: "Before"
162
+ * }
163
+ * Use the 'id' parameters of the method as cache subkeys, and clear the cache before the method executed
148
164
  * @returns
149
165
  */
150
- function CacheEvict(cacheName, eventTime = "Before") {
166
+ function CacheEvict(cacheName, opt = {
167
+ eventTime: "Before",
168
+ }) {
151
169
  return (target, methodName, descriptor) => {
152
170
  const componentType = koatty_container.IOCContainer.getType(target);
153
171
  if (componentType !== "SERVICE" && componentType !== "COMPONENT") {
154
172
  throw Error("This decorator only used in the service、component class.");
155
173
  }
156
- koatty_container.IOCContainer.getIdentifier(target);
157
174
  const { value, configurable, enumerable } = descriptor;
175
+ // 获取定义的参数位置
176
+ opt = {
177
+ ...{
178
+ eventTime: "Before",
179
+ }, ...opt
180
+ };
181
+ const paramIndexes = getParamIndex(opt.params, getArgs(value));
158
182
  descriptor = {
159
183
  configurable,
160
184
  enumerable,
@@ -167,15 +191,28 @@ function CacheEvict(cacheName, eventTime = "Before") {
167
191
  return null;
168
192
  });
169
193
  if (cacheFlag) {
170
- if (eventTime === "Before") {
171
- await store.del(cacheName).catch(() => null);
194
+ let key = PreKey;
195
+ if (props && props.length > 0) {
196
+ for (const item of paramIndexes) {
197
+ if (props[item] !== undefined) {
198
+ const value = koatty_lib.Helper.toString(props[item]);
199
+ key += `:${value}`;
200
+ }
201
+ }
202
+ // 防止key超长
203
+ if (key.length > 32) {
204
+ key = koatty_lib.Helper.murmurHash(key);
205
+ }
206
+ }
207
+ if (opt.eventTime === "Before") {
208
+ await store.del(`${cacheName}:${key}`).catch(() => null);
172
209
  // tslint:disable-next-line: no-invalid-this
173
210
  return value.apply(this, props);
174
211
  }
175
212
  else {
176
213
  // tslint:disable-next-line: no-invalid-this
177
214
  const res = await value.apply(this, props);
178
- store.del(cacheName).catch(() => null);
215
+ store.del(`${cacheName}:${key}`).catch(() => null);
179
216
  return res;
180
217
  }
181
218
  }
@@ -189,6 +226,41 @@ function CacheEvict(cacheName, eventTime = "Before") {
189
226
  InitCacheStore();
190
227
  return descriptor;
191
228
  };
229
+ }
230
+ /**
231
+ * @description:
232
+ * @param {*} func
233
+ * @return {*}
234
+ */
235
+ function getArgs(func) {
236
+ // 首先匹配函数括弧里的参数
237
+ const args = func.toString().match(/.*?\(([^)]*)\)/);
238
+ if (args.length > 1) {
239
+ // 分解参数成数组
240
+ return args[1].split(",").map(function (a) {
241
+ // 去空格和内联注释
242
+ return a.replace(/\/\*.*\*\//, "").trim();
243
+ }).filter(function (ae) {
244
+ // 确保没有undefineds
245
+ return ae;
246
+ });
247
+ }
248
+ return [];
249
+ }
250
+ /**
251
+ * @description:
252
+ * @param {string} params
253
+ * @param {string} args
254
+ * @return {*}
255
+ */
256
+ function getParamIndex(params, args) {
257
+ const res = [];
258
+ for (let i = 0; i < params.length; i++) {
259
+ if (params.includes(params[i])) {
260
+ res.push(i);
261
+ }
262
+ }
263
+ return res;
192
264
  }
193
265
 
194
266
  exports.CacheAble = CacheAble;
package/dist/index.mjs CHANGED
@@ -1,55 +1,60 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2023-01-13 14:17:07
3
+ * @Date: 2023-08-04 14:37:39
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
7
7
  */
8
- import * as helper from 'koatty_lib';
8
+ import { Helper } from 'koatty_lib';
9
9
  import { DefaultLogger } from 'koatty_logger';
10
- import { Store } from 'koatty_store';
10
+ import { CacheStore } from 'koatty_store';
11
11
  import { IOCContainer } from 'koatty_container';
12
12
 
13
13
  /*
14
14
  * @Author: richen
15
15
  * @Date: 2020-07-06 19:53:43
16
- * @LastEditTime: 2023-01-13 14:16:02
16
+ * @LastEditTime: 2023-08-04 14:33:58
17
17
  * @Description:
18
18
  * @Copyright (c) - <richenlin(at)gmail.com>
19
19
  */
20
- // cacheStore
21
- const cacheStore = {
20
+ const PreKey = "k";
21
+ // storeCache
22
+ const storeCache = {
22
23
  store: null
23
24
  };
24
25
  /**
25
- * get instances of cacheStore
26
+ * get instances of storeCache
26
27
  *
27
28
  * @export
28
29
  * @param {Application} app
29
30
  * @returns {*} {CacheStore}
30
31
  */
31
32
  async function GetCacheStore(app) {
32
- var _a;
33
- if (cacheStore.store && cacheStore.store.getConnection) {
34
- return cacheStore.store;
33
+ var _a, _b;
34
+ if (storeCache.store && storeCache.store.getConnection) {
35
+ return storeCache.store;
35
36
  }
36
- const opt = (_a = app.config("CacheStore", "db")) !== null && _a !== void 0 ? _a : {};
37
- if (helper.isEmpty(opt)) {
37
+ const opt = (_b = (_a = app.config("CacheStore")) !== null && _a !== void 0 ? _a : app.config("CacheStore", "db")) !== null && _b !== void 0 ? _b : {};
38
+ if (Helper.isEmpty(opt)) {
38
39
  DefaultLogger.Warn(`Missing CacheStore server configuration. Please write a configuration item with the key name 'CacheStore' in the db.ts file.`);
39
40
  }
40
- cacheStore.store = Store.getInstance(opt);
41
- if (!helper.isFunction(cacheStore.store.getConnection)) {
41
+ storeCache.store = CacheStore.getInstance(opt);
42
+ if (!Helper.isFunction(storeCache.store.getConnection)) {
42
43
  throw Error(`CacheStore connection failed. `);
43
44
  }
44
- return cacheStore.store;
45
+ await storeCache.store.client.getConnection();
46
+ return storeCache.store;
45
47
  }
46
48
  /**
47
49
  * initiation CacheStore connection and client.
48
50
  *
49
51
  */
50
52
  async function InitCacheStore() {
53
+ if (storeCache.store !== null) {
54
+ return;
55
+ }
51
56
  const app = IOCContainer.getApp();
52
- app && app.once("appStart", async function () {
57
+ app && app.once("appReady", async function () {
53
58
  await GetCacheStore(app);
54
59
  });
55
60
  }
@@ -60,18 +65,33 @@ async function InitCacheStore() {
60
65
  *
61
66
  * @export
62
67
  * @param {string} cacheName cache name
63
- * @param {number} [timeout=3600] cache timeout
68
+ * @param {CacheAbleOpt} [opt] cache options
69
+ * e.g:
70
+ * {
71
+ * params: ["id"],
72
+ * timeout: 30
73
+ * }
74
+ * Use the 'id' parameters of the method as cache subkeys, the cache expiration time 30s
64
75
  * @returns {MethodDecorator}
65
76
  */
66
- function CacheAble(cacheName, timeout = 3600) {
77
+ function CacheAble(cacheName, opt = {
78
+ params: [],
79
+ timeout: 3600,
80
+ }) {
67
81
  return (target, methodName, descriptor) => {
68
82
  const componentType = IOCContainer.getType(target);
69
83
  if (componentType !== "SERVICE" && componentType !== "COMPONENT") {
70
84
  throw Error("This decorator only used in the service、component class.");
71
85
  }
72
- let identifier = IOCContainer.getIdentifier(target);
73
- identifier = identifier || (target.constructor ? (target.constructor.name || "") : "");
74
86
  const { value, configurable, enumerable } = descriptor;
87
+ opt = {
88
+ ...{
89
+ params: [],
90
+ timeout: 3600,
91
+ }, ...opt
92
+ };
93
+ // 获取定义的参数位置
94
+ const paramIndexes = getParamIndex(opt.params, getArgs(value));
75
95
  descriptor = {
76
96
  configurable,
77
97
  enumerable,
@@ -85,26 +105,35 @@ function CacheAble(cacheName, timeout = 3600) {
85
105
  });
86
106
  if (cacheFlag) {
87
107
  // tslint:disable-next-line: one-variable-per-declaration
88
- let key = "", res;
108
+ let key = PreKey;
89
109
  if (props && props.length > 0) {
90
- key = `${identifier}:${methodName}:${helper.murmurHash(JSON.stringify(props))}`;
110
+ for (const item of paramIndexes) {
111
+ if (props[item] !== undefined) {
112
+ const value = Helper.toString(props[item]);
113
+ key += `:${value}`;
114
+ }
115
+ }
116
+ // 防止key超长
117
+ if (key.length > 32) {
118
+ key = Helper.murmurHash(key);
119
+ }
91
120
  }
92
- else {
93
- key = `${identifier}:${methodName}`;
94
- }
95
- res = await store.hget(cacheName, key).catch(() => null);
96
- if (!helper.isEmpty(res)) {
121
+ let res = await store.get(`${cacheName}:${key}`).catch(() => null);
122
+ if (!Helper.isEmpty(res)) {
97
123
  return JSON.parse(res);
98
124
  }
99
125
  // tslint:disable-next-line: no-invalid-this
100
126
  res = await value.apply(this, props);
101
127
  // prevent cache penetration
102
- if (helper.isEmpty(res)) {
128
+ if (Helper.isEmpty(res)) {
103
129
  res = "";
104
- timeout = 60;
130
+ opt.timeout = 5;
131
+ }
132
+ if (!opt.timeout) {
133
+ opt.timeout = 3600;
105
134
  }
106
135
  // async set store
107
- store.hset(cacheName, key, JSON.stringify(res), timeout).catch(() => null);
136
+ store.set(`${cacheName}:${key}`, JSON.stringify(res), opt.timeout).catch(() => null);
108
137
  return res;
109
138
  }
110
139
  else {
@@ -123,17 +152,31 @@ function CacheAble(cacheName, timeout = 3600) {
123
152
  *
124
153
  * @export
125
154
  * @param {string} cacheName cacheName cache name
126
- * @param {eventTimes} [eventTime="Before"]
155
+ * @param {CacheEvictOpt} [opt] cache options
156
+ * e.g:
157
+ * {
158
+ * params: ["id"],
159
+ * eventTime: "Before"
160
+ * }
161
+ * Use the 'id' parameters of the method as cache subkeys, and clear the cache before the method executed
127
162
  * @returns
128
163
  */
129
- function CacheEvict(cacheName, eventTime = "Before") {
164
+ function CacheEvict(cacheName, opt = {
165
+ eventTime: "Before",
166
+ }) {
130
167
  return (target, methodName, descriptor) => {
131
168
  const componentType = IOCContainer.getType(target);
132
169
  if (componentType !== "SERVICE" && componentType !== "COMPONENT") {
133
170
  throw Error("This decorator only used in the service、component class.");
134
171
  }
135
- IOCContainer.getIdentifier(target);
136
172
  const { value, configurable, enumerable } = descriptor;
173
+ // 获取定义的参数位置
174
+ opt = {
175
+ ...{
176
+ eventTime: "Before",
177
+ }, ...opt
178
+ };
179
+ const paramIndexes = getParamIndex(opt.params, getArgs(value));
137
180
  descriptor = {
138
181
  configurable,
139
182
  enumerable,
@@ -146,15 +189,28 @@ function CacheEvict(cacheName, eventTime = "Before") {
146
189
  return null;
147
190
  });
148
191
  if (cacheFlag) {
149
- if (eventTime === "Before") {
150
- await store.del(cacheName).catch(() => null);
192
+ let key = PreKey;
193
+ if (props && props.length > 0) {
194
+ for (const item of paramIndexes) {
195
+ if (props[item] !== undefined) {
196
+ const value = Helper.toString(props[item]);
197
+ key += `:${value}`;
198
+ }
199
+ }
200
+ // 防止key超长
201
+ if (key.length > 32) {
202
+ key = Helper.murmurHash(key);
203
+ }
204
+ }
205
+ if (opt.eventTime === "Before") {
206
+ await store.del(`${cacheName}:${key}`).catch(() => null);
151
207
  // tslint:disable-next-line: no-invalid-this
152
208
  return value.apply(this, props);
153
209
  }
154
210
  else {
155
211
  // tslint:disable-next-line: no-invalid-this
156
212
  const res = await value.apply(this, props);
157
- store.del(cacheName).catch(() => null);
213
+ store.del(`${cacheName}:${key}`).catch(() => null);
158
214
  return res;
159
215
  }
160
216
  }
@@ -168,6 +224,41 @@ function CacheEvict(cacheName, eventTime = "Before") {
168
224
  InitCacheStore();
169
225
  return descriptor;
170
226
  };
227
+ }
228
+ /**
229
+ * @description:
230
+ * @param {*} func
231
+ * @return {*}
232
+ */
233
+ function getArgs(func) {
234
+ // 首先匹配函数括弧里的参数
235
+ const args = func.toString().match(/.*?\(([^)]*)\)/);
236
+ if (args.length > 1) {
237
+ // 分解参数成数组
238
+ return args[1].split(",").map(function (a) {
239
+ // 去空格和内联注释
240
+ return a.replace(/\/\*.*\*\//, "").trim();
241
+ }).filter(function (ae) {
242
+ // 确保没有undefineds
243
+ return ae;
244
+ });
245
+ }
246
+ return [];
247
+ }
248
+ /**
249
+ * @description:
250
+ * @param {string} params
251
+ * @param {string} args
252
+ * @return {*}
253
+ */
254
+ function getParamIndex(params, args) {
255
+ const res = [];
256
+ for (let i = 0; i < params.length; i++) {
257
+ if (params.includes(params[i])) {
258
+ res.push(i);
259
+ }
260
+ }
261
+ return res;
171
262
  }
172
263
 
173
264
  export { CacheAble, CacheEvict, GetCacheStore };
package/dist/package.json CHANGED
@@ -1,90 +1,91 @@
1
1
  {
2
- "name": "koatty_cacheable",
3
- "version": "1.3.8",
4
- "description": "Cacheable for koatty.",
5
- "scripts": {
6
- "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
7
- "build:cp": "node scripts/postBuild && copyfiles package.json LICENSE README.md dist/",
8
- "build:js": "del-cli --force dist && npx rollup --bundleConfigAsCjs -c .rollup.config.js",
9
- "build:doc": "del-cli --force docs/api && npx api-documenter markdown --input temp --output docs/api",
10
- "build:dts": "del-cli --force temp && npx tsc && npx api-extractor run --local --verbose",
11
- "eslint": "eslint --ext .ts,.js ./",
12
- "prepublishOnly": "npm test && npm run build",
13
- "prerelease": "npm test && npm run build",
14
- "pub": "git push --follow-tags origin && npm publish",
15
- "release": "standard-version",
16
- "test": "npm run eslint && jest --passWithNoTests"
17
- },
18
- "main": "./dist/index.js",
19
- "exports": {
20
- "require": "./dist/index.js",
21
- "import": "./dist/index.mjs"
22
- },
23
- "repository": {
24
- "type": "git",
25
- "url": "git+https://github.com/thinkkoa/koatty_cacheable.git"
26
- },
27
- "keywords": [
28
- "cache",
29
- "store",
30
- "koatty",
31
- "thinkkoa"
32
- ],
33
- "engines": {
34
- "node": ">10.0.0"
35
- },
36
- "author": {
37
- "name": "richenlin",
38
- "email": "richenlin@gmail.com"
39
- },
40
- "license": "BSD-3-Clause",
41
- "bugs": {
42
- "url": "https://github.com/thinkkoa/koatty_cacheable/issues"
43
- },
44
- "homepage": "https://github.com/thinkkoa/koatty_cacheable",
45
- "maintainers": [
46
- {
47
- "name": "richenlin",
48
- "email": "richenlin@gmail.com"
49
- }
50
- ],
51
- "devDependencies": {
52
- "@commitlint/cli": "^17.x.x",
53
- "@commitlint/config-conventional": "^17.x.x",
54
- "@microsoft/api-documenter": "^7.x.x",
55
- "@microsoft/api-extractor": "^7.x.x",
56
- "@rollup/plugin-json": "^6.x.x",
57
- "@types/jest": "^29.x.x",
58
- "@types/koa": "^2.x.x",
59
- "@types/node": "^18.x.x",
60
- "@typescript-eslint/eslint-plugin": "^5.x.x",
61
- "@typescript-eslint/parser": "^5.x.x",
62
- "conventional-changelog-cli": "^2.x.x",
63
- "copyfiles": "^2.x.x",
64
- "del-cli": "^4.x.x",
65
- "eslint": "^8.x.x",
66
- "eslint-plugin-jest": "^27.x.x",
67
- "husky": "^4.x.x",
68
- "jest": "^29.x.x",
69
- "jest-html-reporters": "^3.x.x",
70
- "rollup": "^3.x.x",
71
- "rollup-plugin-typescript2": "^0.x.x",
72
- "standard-version": "^9.x.x",
73
- "ts-jest": "^29.x.x",
74
- "ts-node": "^10.x.x",
75
- "typescript": "^4.x.x"
76
- },
77
- "dependencies": {
78
- "koatty_container": "^1.x.x",
79
- "koatty_lib": "^1.x.x",
80
- "koatty_logger": "^2.x.x",
81
- "koatty_store": "^1.x.x",
82
- "tslib": "^2.4.1"
83
- },
84
- "peerDependencies": {
85
- "koatty_container": "^1.x.x",
86
- "koatty_lib": "^1.x.x",
87
- "koatty_logger": "^2.x.x",
88
- "koatty_store": "^1.x.x"
2
+ "name": "koatty_cacheable",
3
+ "version": "1.4.1",
4
+ "description": "Cacheable for koatty.",
5
+ "scripts": {
6
+ "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
7
+ "build:cp": "node scripts/postBuild && copyfiles package.json LICENSE README.md dist/",
8
+ "build:js": "del-cli --force dist && npx rollup --bundleConfigAsCjs -c .rollup.config.js",
9
+ "build:doc": "del-cli --force docs/api && npx api-documenter markdown --input temp --output docs/api",
10
+ "build:dts": "del-cli --force temp && npx tsc && npx api-extractor run --local --verbose",
11
+ "eslint": "eslint --ext .ts,.js ./",
12
+ "prepublishOnly": "npm test && npm run build && git push --follow-tags origin",
13
+ "prerelease": "npm test && npm run build",
14
+ "release": "standard-version",
15
+ "release:pre": "npm run release -- --prerelease",
16
+ "release:major": "npm run release -- --release-as major",
17
+ "release:minor": "npm run release -- --release-as minor",
18
+ "test": "npm run eslint && jest --passWithNoTests"
19
+ },
20
+ "main": "./dist/index.js",
21
+ "exports": {
22
+ "require": "./dist/index.js",
23
+ "import": "./dist/index.mjs"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/thinkkoa/koatty_cacheable.git"
28
+ },
29
+ "keywords": [
30
+ "cache",
31
+ "store",
32
+ "koatty",
33
+ "thinkkoa"
34
+ ],
35
+ "engines": {
36
+ "node": ">10.0.0"
37
+ },
38
+ "author": {
39
+ "name": "richenlin",
40
+ "email": "richenlin@gmail.com"
41
+ },
42
+ "license": "BSD-3-Clause",
43
+ "bugs": {
44
+ "url": "https://github.com/thinkkoa/koatty_cacheable/issues"
45
+ },
46
+ "homepage": "https://github.com/thinkkoa/koatty_cacheable",
47
+ "maintainers": [
48
+ {
49
+ "name": "richenlin",
50
+ "email": "richenlin@gmail.com"
89
51
  }
52
+ ],
53
+ "devDependencies": {
54
+ "@commitlint/cli": "^17.x.x",
55
+ "@commitlint/config-conventional": "^17.x.x",
56
+ "@microsoft/api-documenter": "^7.x.x",
57
+ "@microsoft/api-extractor": "^7.x.x",
58
+ "@rollup/plugin-json": "^6.x.x",
59
+ "@types/jest": "^29.x.x",
60
+ "@types/koa": "^2.x.x",
61
+ "@types/node": "^18.x.x",
62
+ "@typescript-eslint/eslint-plugin": "^5.x.x",
63
+ "@typescript-eslint/parser": "^5.x.x",
64
+ "conventional-changelog-cli": "^2.x.x",
65
+ "copyfiles": "^2.x.x",
66
+ "del-cli": "^4.x.x",
67
+ "eslint": "^8.x.x",
68
+ "eslint-plugin-jest": "^27.x.x",
69
+ "husky": "^4.x.x",
70
+ "jest": "^29.x.x",
71
+ "jest-html-reporters": "^3.x.x",
72
+ "rollup": "^3.x.x",
73
+ "rollup-plugin-typescript2": "^0.x.x",
74
+ "standard-version": "^9.x.x",
75
+ "ts-jest": "^29.x.x",
76
+ "ts-node": "^10.x.x",
77
+ "typescript": "^4.x.x"
78
+ },
79
+ "dependencies": {
80
+ "koatty_container": "^1.x.x",
81
+ "koatty_lib": "^1.x.x",
82
+ "koatty_logger": "^2.x.x",
83
+ "koatty_store": "^1.x.x"
84
+ },
85
+ "peerDependencies": {
86
+ "koatty_container": "^1.x.x",
87
+ "koatty_lib": "^1.x.x",
88
+ "koatty_logger": "^2.x.x",
89
+ "koatty_store": "^1.x.x"
90
+ }
90
91
  }
package/package.json CHANGED
@@ -1,90 +1,91 @@
1
1
  {
2
- "name": "koatty_cacheable",
3
- "version": "1.3.8",
4
- "description": "Cacheable for koatty.",
5
- "scripts": {
6
- "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
7
- "build:cp": "node scripts/postBuild && copyfiles package.json LICENSE README.md dist/",
8
- "build:js": "del-cli --force dist && npx rollup --bundleConfigAsCjs -c .rollup.config.js",
9
- "build:doc": "del-cli --force docs/api && npx api-documenter markdown --input temp --output docs/api",
10
- "build:dts": "del-cli --force temp && npx tsc && npx api-extractor run --local --verbose",
11
- "eslint": "eslint --ext .ts,.js ./",
12
- "prepublishOnly": "npm test && npm run build",
13
- "prerelease": "npm test && npm run build",
14
- "pub": "git push --follow-tags origin && npm publish",
15
- "release": "standard-version",
16
- "test": "npm run eslint && jest --passWithNoTests"
17
- },
18
- "main": "./dist/index.js",
19
- "exports": {
20
- "require": "./dist/index.js",
21
- "import": "./dist/index.mjs"
22
- },
23
- "repository": {
24
- "type": "git",
25
- "url": "git+https://github.com/thinkkoa/koatty_cacheable.git"
26
- },
27
- "keywords": [
28
- "cache",
29
- "store",
30
- "koatty",
31
- "thinkkoa"
32
- ],
33
- "engines": {
34
- "node": ">10.0.0"
35
- },
36
- "author": {
37
- "name": "richenlin",
38
- "email": "richenlin@gmail.com"
39
- },
40
- "license": "BSD-3-Clause",
41
- "bugs": {
42
- "url": "https://github.com/thinkkoa/koatty_cacheable/issues"
43
- },
44
- "homepage": "https://github.com/thinkkoa/koatty_cacheable",
45
- "maintainers": [
46
- {
47
- "name": "richenlin",
48
- "email": "richenlin@gmail.com"
49
- }
50
- ],
51
- "devDependencies": {
52
- "@commitlint/cli": "^17.x.x",
53
- "@commitlint/config-conventional": "^17.x.x",
54
- "@microsoft/api-documenter": "^7.x.x",
55
- "@microsoft/api-extractor": "^7.x.x",
56
- "@rollup/plugin-json": "^6.x.x",
57
- "@types/jest": "^29.x.x",
58
- "@types/koa": "^2.x.x",
59
- "@types/node": "^18.x.x",
60
- "@typescript-eslint/eslint-plugin": "^5.x.x",
61
- "@typescript-eslint/parser": "^5.x.x",
62
- "conventional-changelog-cli": "^2.x.x",
63
- "copyfiles": "^2.x.x",
64
- "del-cli": "^4.x.x",
65
- "eslint": "^8.x.x",
66
- "eslint-plugin-jest": "^27.x.x",
67
- "husky": "^4.x.x",
68
- "jest": "^29.x.x",
69
- "jest-html-reporters": "^3.x.x",
70
- "rollup": "^3.x.x",
71
- "rollup-plugin-typescript2": "^0.x.x",
72
- "standard-version": "^9.x.x",
73
- "ts-jest": "^29.x.x",
74
- "ts-node": "^10.x.x",
75
- "typescript": "^4.x.x"
76
- },
77
- "dependencies": {
78
- "koatty_container": "^1.x.x",
79
- "koatty_lib": "^1.x.x",
80
- "koatty_logger": "^2.x.x",
81
- "koatty_store": "^1.x.x",
82
- "tslib": "^2.4.1"
83
- },
84
- "peerDependencies": {
85
- "koatty_container": "^1.x.x",
86
- "koatty_lib": "^1.x.x",
87
- "koatty_logger": "^2.x.x",
88
- "koatty_store": "^1.x.x"
2
+ "name": "koatty_cacheable",
3
+ "version": "1.4.1",
4
+ "description": "Cacheable for koatty.",
5
+ "scripts": {
6
+ "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
7
+ "build:cp": "node scripts/postBuild && copyfiles package.json LICENSE README.md dist/",
8
+ "build:js": "del-cli --force dist && npx rollup --bundleConfigAsCjs -c .rollup.config.js",
9
+ "build:doc": "del-cli --force docs/api && npx api-documenter markdown --input temp --output docs/api",
10
+ "build:dts": "del-cli --force temp && npx tsc && npx api-extractor run --local --verbose",
11
+ "eslint": "eslint --ext .ts,.js ./",
12
+ "prepublishOnly": "npm test && npm run build && git push --follow-tags origin",
13
+ "prerelease": "npm test && npm run build",
14
+ "release": "standard-version",
15
+ "release:pre": "npm run release -- --prerelease",
16
+ "release:major": "npm run release -- --release-as major",
17
+ "release:minor": "npm run release -- --release-as minor",
18
+ "test": "npm run eslint && jest --passWithNoTests"
19
+ },
20
+ "main": "./dist/index.js",
21
+ "exports": {
22
+ "require": "./dist/index.js",
23
+ "import": "./dist/index.mjs"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/thinkkoa/koatty_cacheable.git"
28
+ },
29
+ "keywords": [
30
+ "cache",
31
+ "store",
32
+ "koatty",
33
+ "thinkkoa"
34
+ ],
35
+ "engines": {
36
+ "node": ">10.0.0"
37
+ },
38
+ "author": {
39
+ "name": "richenlin",
40
+ "email": "richenlin@gmail.com"
41
+ },
42
+ "license": "BSD-3-Clause",
43
+ "bugs": {
44
+ "url": "https://github.com/thinkkoa/koatty_cacheable/issues"
45
+ },
46
+ "homepage": "https://github.com/thinkkoa/koatty_cacheable",
47
+ "maintainers": [
48
+ {
49
+ "name": "richenlin",
50
+ "email": "richenlin@gmail.com"
89
51
  }
52
+ ],
53
+ "devDependencies": {
54
+ "@commitlint/cli": "^17.x.x",
55
+ "@commitlint/config-conventional": "^17.x.x",
56
+ "@microsoft/api-documenter": "^7.x.x",
57
+ "@microsoft/api-extractor": "^7.x.x",
58
+ "@rollup/plugin-json": "^6.x.x",
59
+ "@types/jest": "^29.x.x",
60
+ "@types/koa": "^2.x.x",
61
+ "@types/node": "^18.x.x",
62
+ "@typescript-eslint/eslint-plugin": "^5.x.x",
63
+ "@typescript-eslint/parser": "^5.x.x",
64
+ "conventional-changelog-cli": "^2.x.x",
65
+ "copyfiles": "^2.x.x",
66
+ "del-cli": "^4.x.x",
67
+ "eslint": "^8.x.x",
68
+ "eslint-plugin-jest": "^27.x.x",
69
+ "husky": "^4.x.x",
70
+ "jest": "^29.x.x",
71
+ "jest-html-reporters": "^3.x.x",
72
+ "rollup": "^3.x.x",
73
+ "rollup-plugin-typescript2": "^0.x.x",
74
+ "standard-version": "^9.x.x",
75
+ "ts-jest": "^29.x.x",
76
+ "ts-node": "^10.x.x",
77
+ "typescript": "^4.x.x"
78
+ },
79
+ "dependencies": {
80
+ "koatty_container": "^1.x.x",
81
+ "koatty_lib": "^1.x.x",
82
+ "koatty_logger": "^2.x.x",
83
+ "koatty_store": "^1.x.x"
84
+ },
85
+ "peerDependencies": {
86
+ "koatty_container": "^1.x.x",
87
+ "koatty_lib": "^1.x.x",
88
+ "koatty_logger": "^2.x.x",
89
+ "koatty_store": "^1.x.x"
90
+ }
90
91
  }