koatty_cacheable 1.4.3 → 1.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/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
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.5.0](https://github.com/thinkkoa/koatty_cacheable/compare/v1.4.3...v1.5.0) (2024-04-01)
6
+
7
+
8
+ ### Features
9
+
10
+ * delayedDoubleDeletion ([532f735](https://github.com/thinkkoa/koatty_cacheable/commit/532f735818fc66fa144023cd24282efe350833ba))
11
+
5
12
  ### [1.4.3](https://github.com/thinkkoa/koatty_cacheable/compare/v1.4.2...v1.4.3) (2024-01-17)
6
13
 
7
14
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2024-01-17 22:08:12
3
+ * @Date: 2024-04-01 15:47:51
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -9,9 +9,11 @@ import { Application } from 'koatty_container';
9
9
  import { CacheStore } from 'koatty_store';
10
10
 
11
11
  /**
12
- * Decorate this method to support caching. Redis server config from db.ts.
13
- * The cache method returns a value to ensure that the next time the method is executed with the same parameters,
14
- * the results can be obtained directly from the cache without the need to execute the method again.
12
+ * Decorate this method to support caching.
13
+ * The cache method returns a value to ensure that the next time
14
+ * the method is executed with the same parameters, the results can be obtained
15
+ * directly from the cache without the need to execute the method again.
16
+ * CacheStore server config defined in db.ts.
15
17
  *
16
18
  * @export
17
19
  * @param {string} cacheName cache name
@@ -36,7 +38,8 @@ export declare interface CacheAbleOpt {
36
38
  }
37
39
 
38
40
  /**
39
- * Decorating the execution of this method will trigger a cache clear operation. Redis server config from db.ts.
41
+ * Decorating the execution of this method will trigger a cache clear operation.
42
+ * CacheStore server config defined in db.ts.
40
43
  *
41
44
  * @export
42
45
  * @param {string} cacheName cacheName cache name
@@ -44,9 +47,10 @@ export declare interface CacheAbleOpt {
44
47
  * e.g:
45
48
  * {
46
49
  * params: ["id"],
47
- * eventTime: "Before"
50
+ * delayedDoubleDeletion: true
48
51
  * }
49
- * Use the 'id' parameters of the method as cache subkeys, and clear the cache before the method executed
52
+ * Use the 'id' parameters of the method as cache subkeys,
53
+ * and clear the cache after the method executed
50
54
  * @returns
51
55
  */
52
56
  export declare function CacheEvict(cacheName: string, opt?: CacheEvictOpt): (target: any, methodName: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
@@ -57,14 +61,9 @@ export declare function CacheEvict(cacheName: string, opt?: CacheEvictOpt): (tar
57
61
  */
58
62
  export declare interface CacheEvictOpt {
59
63
  params?: string[];
60
- eventTime?: "Before";
64
+ delayedDoubleDeletion?: boolean;
61
65
  }
62
66
 
63
- /**
64
- *
65
- */
66
- export declare type eventTimes = "Before" | "After";
67
-
68
67
  /**
69
68
  * get instances of storeCache
70
69
  *
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2024-01-17 22:08:01
3
+ * @Date: 2024-04-01 15:47:40
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -15,7 +15,7 @@ var koatty_container = require('koatty_container');
15
15
  /*
16
16
  * @Author: richen
17
17
  * @Date: 2020-07-06 19:53:43
18
- * @LastEditTime: 2024-01-17 21:54:53
18
+ * @LastEditTime: 2024-04-01 15:44:15
19
19
  * @Description:
20
20
  * @Copyright (c) - <richenlin(at)gmail.com>
21
21
  */
@@ -61,9 +61,11 @@ async function InitCacheStore() {
61
61
  });
62
62
  }
63
63
  /**
64
- * Decorate this method to support caching. Redis server config from db.ts.
65
- * The cache method returns a value to ensure that the next time the method is executed with the same parameters,
66
- * the results can be obtained directly from the cache without the need to execute the method again.
64
+ * Decorate this method to support caching.
65
+ * The cache method returns a value to ensure that the next time
66
+ * the method is executed with the same parameters, the results can be obtained
67
+ * directly from the cache without the need to execute the method again.
68
+ * CacheStore server config defined in db.ts.
67
69
  *
68
70
  * @export
69
71
  * @param {string} cacheName cache name
@@ -146,7 +148,8 @@ function CacheAble(cacheName, opt = {
146
148
  };
147
149
  }
148
150
  /**
149
- * Decorating the execution of this method will trigger a cache clear operation. Redis server config from db.ts.
151
+ * Decorating the execution of this method will trigger a cache clear operation.
152
+ * CacheStore server config defined in db.ts.
150
153
  *
151
154
  * @export
152
155
  * @param {string} cacheName cacheName cache name
@@ -154,13 +157,14 @@ function CacheAble(cacheName, opt = {
154
157
  * e.g:
155
158
  * {
156
159
  * params: ["id"],
157
- * eventTime: "Before"
160
+ * delayedDoubleDeletion: true
158
161
  * }
159
- * Use the 'id' parameters of the method as cache subkeys, and clear the cache before the method executed
162
+ * Use the 'id' parameters of the method as cache subkeys,
163
+ * and clear the cache after the method executed
160
164
  * @returns
161
165
  */
162
166
  function CacheEvict(cacheName, opt = {
163
- eventTime: "Before",
167
+ delayedDoubleDeletion: true,
164
168
  }) {
165
169
  return (target, methodName, descriptor) => {
166
170
  const componentType = koatty_container.IOCContainer.getType(target);
@@ -200,17 +204,15 @@ function CacheEvict(cacheName, opt = {
200
204
  key = koatty_lib.Helper.murmurHash(key);
201
205
  }
202
206
  }
203
- if (opt.eventTime === "Before") {
204
- await store.del(key).catch(() => null);
205
- // tslint:disable-next-line: no-invalid-this
206
- return value.apply(this, props);
207
- }
208
- else {
209
- // tslint:disable-next-line: no-invalid-this
210
- const res = await value.apply(this, props);
211
- store.del(key).catch(() => null);
212
- return res;
207
+ // tslint:disable-next-line: no-invalid-this
208
+ const res = await value.apply(this, props);
209
+ store.del(key).catch(() => null);
210
+ if (opt.delayedDoubleDeletion) {
211
+ asyncDelayedExecution(2000, () => {
212
+ store.del(key).catch(() => null);
213
+ });
213
214
  }
215
+ return res;
214
216
  }
215
217
  else {
216
218
  // tslint:disable-next-line: no-invalid-this
@@ -236,6 +238,24 @@ function getParamIndex(params) {
236
238
  }
237
239
  }
238
240
  return res;
241
+ }
242
+ /**
243
+ *
244
+ * @param ms
245
+ * @returns
246
+ */
247
+ function delay(ms) {
248
+ return new Promise(resolve => setTimeout(resolve, ms));
249
+ }
250
+ /**
251
+ * async delayed execution func
252
+ * @param ms
253
+ * @param fn
254
+ * @returns
255
+ */
256
+ async function asyncDelayedExecution(ms, fn) {
257
+ await delay(ms); // delay ms second
258
+ return fn();
239
259
  }
240
260
 
241
261
  exports.CacheAble = CacheAble;
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2024-01-17 22:08:01
3
+ * @Date: 2024-04-01 15:47:40
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -13,7 +13,7 @@ import { IOCContainer } from 'koatty_container';
13
13
  /*
14
14
  * @Author: richen
15
15
  * @Date: 2020-07-06 19:53:43
16
- * @LastEditTime: 2024-01-17 21:54:53
16
+ * @LastEditTime: 2024-04-01 15:44:15
17
17
  * @Description:
18
18
  * @Copyright (c) - <richenlin(at)gmail.com>
19
19
  */
@@ -59,9 +59,11 @@ async function InitCacheStore() {
59
59
  });
60
60
  }
61
61
  /**
62
- * Decorate this method to support caching. Redis server config from db.ts.
63
- * The cache method returns a value to ensure that the next time the method is executed with the same parameters,
64
- * the results can be obtained directly from the cache without the need to execute the method again.
62
+ * Decorate this method to support caching.
63
+ * The cache method returns a value to ensure that the next time
64
+ * the method is executed with the same parameters, the results can be obtained
65
+ * directly from the cache without the need to execute the method again.
66
+ * CacheStore server config defined in db.ts.
65
67
  *
66
68
  * @export
67
69
  * @param {string} cacheName cache name
@@ -144,7 +146,8 @@ function CacheAble(cacheName, opt = {
144
146
  };
145
147
  }
146
148
  /**
147
- * Decorating the execution of this method will trigger a cache clear operation. Redis server config from db.ts.
149
+ * Decorating the execution of this method will trigger a cache clear operation.
150
+ * CacheStore server config defined in db.ts.
148
151
  *
149
152
  * @export
150
153
  * @param {string} cacheName cacheName cache name
@@ -152,13 +155,14 @@ function CacheAble(cacheName, opt = {
152
155
  * e.g:
153
156
  * {
154
157
  * params: ["id"],
155
- * eventTime: "Before"
158
+ * delayedDoubleDeletion: true
156
159
  * }
157
- * Use the 'id' parameters of the method as cache subkeys, and clear the cache before the method executed
160
+ * Use the 'id' parameters of the method as cache subkeys,
161
+ * and clear the cache after the method executed
158
162
  * @returns
159
163
  */
160
164
  function CacheEvict(cacheName, opt = {
161
- eventTime: "Before",
165
+ delayedDoubleDeletion: true,
162
166
  }) {
163
167
  return (target, methodName, descriptor) => {
164
168
  const componentType = IOCContainer.getType(target);
@@ -198,17 +202,15 @@ function CacheEvict(cacheName, opt = {
198
202
  key = Helper.murmurHash(key);
199
203
  }
200
204
  }
201
- if (opt.eventTime === "Before") {
202
- await store.del(key).catch(() => null);
203
- // tslint:disable-next-line: no-invalid-this
204
- return value.apply(this, props);
205
- }
206
- else {
207
- // tslint:disable-next-line: no-invalid-this
208
- const res = await value.apply(this, props);
209
- store.del(key).catch(() => null);
210
- return res;
205
+ // tslint:disable-next-line: no-invalid-this
206
+ const res = await value.apply(this, props);
207
+ store.del(key).catch(() => null);
208
+ if (opt.delayedDoubleDeletion) {
209
+ asyncDelayedExecution(2000, () => {
210
+ store.del(key).catch(() => null);
211
+ });
211
212
  }
213
+ return res;
212
214
  }
213
215
  else {
214
216
  // tslint:disable-next-line: no-invalid-this
@@ -234,6 +236,24 @@ function getParamIndex(params) {
234
236
  }
235
237
  }
236
238
  return res;
239
+ }
240
+ /**
241
+ *
242
+ * @param ms
243
+ * @returns
244
+ */
245
+ function delay(ms) {
246
+ return new Promise(resolve => setTimeout(resolve, ms));
247
+ }
248
+ /**
249
+ * async delayed execution func
250
+ * @param ms
251
+ * @param fn
252
+ * @returns
253
+ */
254
+ async function asyncDelayedExecution(ms, fn) {
255
+ await delay(ms); // delay ms second
256
+ return fn();
237
257
  }
238
258
 
239
259
  export { CacheAble, CacheEvict, GetCacheStore };
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koatty_cacheable",
3
- "version": "1.4.3",
3
+ "version": "1.5.0",
4
4
  "description": "Cacheable for koatty.",
5
5
  "scripts": {
6
6
  "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koatty_cacheable",
3
- "version": "1.4.3",
3
+ "version": "1.5.0",
4
4
  "description": "Cacheable for koatty.",
5
5
  "scripts": {
6
6
  "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",