vona-module-a-caching 5.0.14 → 5.0.16

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.
@@ -3,6 +3,7 @@ import type { IAopMethodExecute, IDecoratorAopMethodOptions } from 'vona-module-
3
3
  import type { TypeCachingActionOptions } from '../types/caching.ts';
4
4
  import { BeanAopMethodBase } from 'vona';
5
5
  export interface IAopMethodOptionsCachingDel extends IDecoratorAopMethodOptions, TypeCachingActionOptions {
6
+ intention: 'del' | 'create';
6
7
  }
7
8
  export declare class AopMethodCachingDel extends BeanAopMethodBase implements IAopMethodExecute {
8
9
  execute(options: IAopMethodOptionsCachingDel, args: [], next: Next, receiver: any, prop: string): Promise<any>;
package/dist/index.js CHANGED
@@ -11,57 +11,82 @@ let AopMethodCachingClear = (_dec$4 = AopMethod(), _dec2$4 = BeanInfo({
11
11
  async execute(options, _args, next, receiver, prop) {
12
12
  if (!options.cacheName) throw new Error(`Should specify cacheName for caching: ${receiver.$beanFullName}#${prop}`);
13
13
  // next
14
- const value = await next();
14
+ const result = await next();
15
15
  // cache
16
16
  const cache = this.bean.summer.cache(beanFullNameFromOnionName(options.cacheName, 'summerCache'));
17
17
  await cache.clear(options);
18
18
  // ok
19
- return value;
19
+ return result;
20
20
  }
21
21
  }) || _class$4) || _class$4);
22
22
 
23
- function combineCachingKey(options, args, receiver, prop) {
23
+ function combineCachingKey(info, options) {
24
+ const {
25
+ result,
26
+ args,
27
+ prop,
28
+ receiver,
29
+ intention
30
+ } = info;
24
31
  // cacheKeyFn
25
32
  if (options.cacheKeyFn) {
26
33
  if (typeof options.cacheKeyFn === 'string') {
27
34
  if (!receiver[options.cacheKeyFn]) {
28
35
  throw new Error(`cacheKeyFn not found: ${cast(receiver).$beanFullName}#${options.cacheKeyFn}`);
29
36
  }
30
- return receiver[options.cacheKeyFn](args, prop, options, receiver);
37
+ return _hashKey(receiver[options.cacheKeyFn](info, options));
31
38
  }
32
- return options.cacheKeyFn.call(receiver, args, prop, options, receiver);
39
+ return _hashKey(options.cacheKeyFn.call(receiver, info, options));
33
40
  }
34
41
  // cacheKey
35
42
  if (options.cacheKey) {
36
- return evaluateExpressions(options.cacheKey, {
43
+ return _hashKey(evaluateExpressions(options.cacheKey, {
37
44
  args,
38
45
  prop,
46
+ intention,
39
47
  options,
40
48
  self: receiver,
41
49
  app: cast(receiver).app,
42
50
  ctx: cast(receiver).ctx
43
- });
51
+ }));
44
52
  }
45
- // default
46
- return `${cast(receiver).$beanFullName}_${options.cacheProp ?? prop}_${getKeyHash(args)}`;
53
+ // default: only use first arg
54
+ let argsPick;
55
+ if (intention === 'create') {
56
+ argsPick = [result?.id];
57
+ } else if (intention === 'set') {
58
+ argsPick = args.slice(0, args.length - 1);
59
+ } else {
60
+ argsPick = args;
61
+ }
62
+ const argFirst = argsPick.length === 1 ? argsPick[0] : argsPick;
63
+ return _hashKey(argFirst);
47
64
  }
48
- function combineCachingValue(options, args, receiver, prop, value) {
65
+ function combineCachingValue(info, options) {
66
+ const {
67
+ result,
68
+ args,
69
+ prop,
70
+ receiver,
71
+ intention
72
+ } = info;
49
73
  // cacheValueFn
50
74
  if (!isNil(options.cacheValueFn)) {
51
75
  if (typeof options.cacheValueFn === 'string') {
52
76
  if (!receiver[options.cacheValueFn]) {
53
77
  throw new Error(`cacheValueFn not found: ${cast(receiver).$beanFullName}#${options.cacheValueFn}`);
54
78
  }
55
- return receiver[options.cacheValueFn](value, args, prop, options, receiver);
79
+ return receiver[options.cacheValueFn](info, options);
56
80
  }
57
- return options.cacheValueFn.call(receiver, value, args, prop, options, receiver);
81
+ return options.cacheValueFn.call(receiver, info, options);
58
82
  }
59
83
  // cacheValue
60
84
  if (!isNil(options.cacheValue)) {
61
85
  return evaluateExpressions(options.cacheValue, {
62
- value,
86
+ result,
63
87
  args,
64
88
  prop,
89
+ intention,
65
90
  options,
66
91
  self: receiver,
67
92
  app: cast(receiver).app,
@@ -69,10 +94,18 @@ function combineCachingValue(options, args, receiver, prop, value) {
69
94
  });
70
95
  }
71
96
  // default
72
- return value;
97
+ if (intention === 'set') {
98
+ return args[args.length - 1];
99
+ } else {
100
+ return result;
101
+ }
73
102
  }
74
103
  function isCachingKeyValid(key) {
75
- return !isNil(key) && key !== false && key !== '';
104
+ return !isNil(key);
105
+ // return !isNil(key) && key !== false && key !== '';
106
+ }
107
+ function _hashKey(arg) {
108
+ return isCachingKeyValid(arg) ? getKeyHash(arg) : arg;
76
109
  }
77
110
 
78
111
  var _dec$3, _dec2$3, _class$3;
@@ -82,15 +115,21 @@ let AopMethodCachingDel = (_dec$3 = AopMethod(), _dec2$3 = BeanInfo({
82
115
  async execute(options, args, next, receiver, prop) {
83
116
  if (!options.cacheName) throw new Error(`Should specify cacheName for caching: ${receiver.$beanFullName}#${prop}`);
84
117
  // next
85
- const value = await next();
118
+ const result = await next();
86
119
  // key
87
- const key = combineCachingKey(options, args, receiver, prop);
88
- if (!isCachingKeyValid(key)) return value;
120
+ const key = combineCachingKey({
121
+ args,
122
+ receiver,
123
+ prop,
124
+ result,
125
+ intention: options.intention ?? 'del'
126
+ }, options);
127
+ if (!isCachingKeyValid(key)) return result;
89
128
  // cache
90
129
  const cache = this.bean.summer.cache(beanFullNameFromOnionName(options.cacheName, 'summerCache'));
91
130
  await cache.del(key, options);
92
131
  // ok
93
- return value;
132
+ return result;
94
133
  }
95
134
  }) || _class$3) || _class$3);
96
135
 
@@ -101,7 +140,12 @@ let AopMethodCachingGet = (_dec$2 = AopMethod(), _dec2$2 = BeanInfo({
101
140
  async execute(options, args, next, receiver, prop) {
102
141
  if (!options.cacheName) throw new Error(`Should specify cacheName for caching: ${receiver.$beanFullName}#${prop}`);
103
142
  // key
104
- const key = combineCachingKey(options, args, receiver, prop);
143
+ const key = combineCachingKey({
144
+ args,
145
+ receiver,
146
+ prop,
147
+ intention: 'get'
148
+ }, options);
105
149
  if (!isCachingKeyValid(key)) return next();
106
150
  // cache
107
151
  const cache = this.bean.summer.cache(beanFullNameFromOnionName(options.cacheName, 'summerCache'));
@@ -120,17 +164,28 @@ let AopMethodCachingSet = (_dec$1 = AopMethod(), _dec2$1 = BeanInfo({
120
164
  async execute(options, args, next, receiver, prop) {
121
165
  if (!options.cacheName) throw new Error(`Should specify cacheName for caching: ${receiver.$beanFullName}#${prop}`);
122
166
  // next
123
- const value = await next();
167
+ const result = await next();
124
168
  // key
125
- const key = combineCachingKey(options, args, receiver, prop);
126
- if (!isCachingKeyValid(key)) return value;
169
+ const key = combineCachingKey({
170
+ args,
171
+ receiver,
172
+ prop,
173
+ intention: 'set'
174
+ }, options);
175
+ if (!isCachingKeyValid(key)) return result;
127
176
  // value
128
- const cacheValue = combineCachingValue(options, args, receiver, prop, value);
177
+ const cacheValue = combineCachingValue({
178
+ args,
179
+ receiver,
180
+ prop,
181
+ result,
182
+ intention: 'set'
183
+ }, options);
129
184
  // cache
130
185
  const cache = this.bean.summer.cache(beanFullNameFromOnionName(options.cacheName, 'summerCache'));
131
186
  await cache.set(cacheValue, key, options);
132
187
  // ok
133
- return value;
188
+ return result;
134
189
  }
135
190
  }) || _class$1) || _class$1);
136
191
 
@@ -1,6 +1,5 @@
1
- import type { BeanBase } from 'vona';
2
1
  import type { IAopMethodOptionsCachingSet } from '../bean/aopMethod.cachingSet.ts';
3
- import type { TypeCachingActionOptions } from '../types/caching.ts';
4
- export declare function combineCachingKey(options: TypeCachingActionOptions, args: [], receiver: BeanBase, prop: string): any;
5
- export declare function combineCachingValue(options: IAopMethodOptionsCachingSet, args: [], receiver: BeanBase, prop: string, value: any): any;
2
+ import type { ICachingActionKeyInfo, ICachingActionValueInfo, TypeCachingActionOptions } from '../types/caching.ts';
3
+ export declare function combineCachingKey(info: ICachingActionKeyInfo, options: TypeCachingActionOptions): any;
4
+ export declare function combineCachingValue(info: ICachingActionValueInfo, options: IAopMethodOptionsCachingSet): any;
6
5
  export declare function isCachingKeyValid(key: any): boolean;
@@ -1,11 +1,25 @@
1
1
  import type { BeanBase } from 'vona';
2
2
  import type { ISummerCacheRecord, TSummerCacheActionOptions } from 'vona-module-a-summer';
3
- export type TypeCacheKeyFn = (args: [], prop: string, options: TypeCachingActionOptions, receiver: BeanBase) => any;
4
- export type TypeCacheValueFn = (value: any, args: [], prop: string, options: TypeCachingActionOptions, receiver: BeanBase) => any;
3
+ export type TypeCachingActionIntention = 'get' | 'set' | 'del' | 'create';
4
+ export interface ICachingActionKeyInfo {
5
+ result?: any;
6
+ args: any[];
7
+ prop: string;
8
+ receiver: BeanBase;
9
+ intention: TypeCachingActionIntention;
10
+ }
11
+ export interface ICachingActionValueInfo {
12
+ result: any;
13
+ args: any[];
14
+ prop: string;
15
+ receiver: BeanBase;
16
+ intention: TypeCachingActionIntention;
17
+ }
18
+ export type TypeCacheKeyFn = (info: ICachingActionKeyInfo, options: TypeCachingActionOptions) => any;
19
+ export type TypeCacheValueFn = (info: ICachingActionValueInfo, options: TypeCachingActionOptions) => any;
5
20
  export type TypeCachingActionOptions = Pick<TSummerCacheActionOptions<unknown, unknown>, 'mode' | 'ignoreNull' | 'ttl' | 'updateAgeOnGet' | 'broadcastOnSet'> & {
6
21
  cacheName: keyof ISummerCacheRecord;
7
22
  cacheKey?: any;
8
23
  cacheKeyFn?: TypeCacheKeyFn | string;
9
- cacheProp?: string;
10
24
  };
11
- export type TypeCachingActionClearOptions = Omit<TypeCachingActionOptions, 'cacheKey' | 'cacheKeyFn' | 'cacheProp'>;
25
+ export type TypeCachingActionClearOptions = Omit<TypeCachingActionOptions, 'cacheKey' | 'cacheKeyFn'>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vona-module-a-caching",
3
3
  "type": "module",
4
- "version": "5.0.14",
4
+ "version": "5.0.16",
5
5
  "title": "a-caching",
6
6
  "vonaModule": {
7
7
  "dependencies": {}