@midwayjs/cache-manager 4.0.0-beta.11 → 4.0.0-beta.13

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/factory.d.ts CHANGED
@@ -1,10 +1,15 @@
1
- import { IMidwayContainer, ServiceFactory, ServiceFactoryConfigOption } from '@midwayjs/core';
1
+ import { IMidwayContainer, ServiceFactory, ServiceFactoryConfigOption, MidwayTraceService } from '@midwayjs/core';
2
2
  import { CacheManagerOptions, MidwayCache, MidwayMultiCache, MidwayUnionCache } from './interface';
3
3
  export declare class CachingFactory extends ServiceFactory<MidwayUnionCache> {
4
4
  protected cacheManagerConfig: ServiceFactoryConfigOption<CacheManagerOptions>;
5
5
  protected applicationContext: IMidwayContainer;
6
+ protected traceService: MidwayTraceService;
7
+ protected traceMetaResolver: any;
8
+ protected traceEnabled: any;
9
+ protected traceInjector: any;
6
10
  protected init(): Promise<void>;
7
11
  protected createClient(config: CacheManagerOptions<any>, clientName: string): Promise<void | MidwayUnionCache>;
12
+ protected bindTraceContext(cache: MidwayUnionCache, clientName: string): void;
8
13
  getName(): string;
9
14
  getCaching(cacheKey: string): MidwayCache;
10
15
  getMultiCaching(cacheKey: string): MidwayMultiCache;
package/dist/factory.js CHANGED
@@ -15,6 +15,10 @@ const base_1 = require("./base");
15
15
  let CachingFactory = class CachingFactory extends core_1.ServiceFactory {
16
16
  cacheManagerConfig;
17
17
  applicationContext;
18
+ traceService;
19
+ traceMetaResolver;
20
+ traceEnabled;
21
+ traceInjector;
18
22
  async init() {
19
23
  await this.initClients(this.cacheManagerConfig);
20
24
  }
@@ -49,7 +53,9 @@ let CachingFactory = class CachingFactory extends core_1.ServiceFactory {
49
53
  throw new core_1.MidwayCommonError('invalid cache config');
50
54
  }
51
55
  }
52
- return await (0, base_1.multiCaching)(newFactory);
56
+ const cache = await (0, base_1.multiCaching)(newFactory);
57
+ this.bindTraceContext(cache, clientName);
58
+ return cache;
53
59
  }
54
60
  else {
55
61
  // single cache
@@ -59,9 +65,65 @@ let CachingFactory = class CachingFactory extends core_1.ServiceFactory {
59
65
  if (!config.store) {
60
66
  throw new core_1.MidwayCommonError(`cache instance "${clientName}" store is undefined, please check your configuration.`);
61
67
  }
62
- return await (0, base_1.caching)(config.store, config['options']);
68
+ const cache = await (0, base_1.caching)(config.store, config['options']);
69
+ this.bindTraceContext(cache, clientName);
70
+ return cache;
63
71
  }
64
72
  }
73
+ bindTraceContext(cache, clientName) {
74
+ if (!cache || !this.traceService) {
75
+ return;
76
+ }
77
+ const wrapMethod = (target, methodName) => {
78
+ const rawMethod = target?.[methodName];
79
+ if (typeof rawMethod !== 'function') {
80
+ return;
81
+ }
82
+ target[methodName] = (...args) => {
83
+ const rawCarrier = typeof this.traceInjector === 'function'
84
+ ? this.traceInjector({
85
+ request: args,
86
+ custom: {
87
+ clientName,
88
+ methodName,
89
+ },
90
+ })
91
+ : {};
92
+ const carrier = rawCarrier && typeof rawCarrier === 'object' ? rawCarrier : {};
93
+ return this.traceService.runWithExitSpan(`cache.${methodName}`, {
94
+ enable: this.traceEnabled !== false,
95
+ carrier,
96
+ attributes: {
97
+ 'midway.protocol': 'cache',
98
+ 'midway.cache.client': clientName,
99
+ 'midway.cache.method': methodName,
100
+ },
101
+ meta: this.traceMetaResolver,
102
+ metaArgs: {
103
+ carrier,
104
+ request: args,
105
+ custom: {
106
+ clientName,
107
+ methodName,
108
+ },
109
+ },
110
+ }, async () => {
111
+ return rawMethod.apply(target, args);
112
+ });
113
+ };
114
+ };
115
+ [
116
+ 'get',
117
+ 'set',
118
+ 'del',
119
+ 'wrap',
120
+ 'methodWrap',
121
+ 'mget',
122
+ 'mset',
123
+ 'mdel',
124
+ 'reset',
125
+ ].forEach(methodName => wrapMethod(cache, methodName));
126
+ }
65
127
  getName() {
66
128
  return 'cache-manager';
67
129
  }
@@ -81,6 +143,22 @@ __decorate([
81
143
  (0, core_1.ApplicationContext)(),
82
144
  __metadata("design:type", Object)
83
145
  ], CachingFactory.prototype, "applicationContext", void 0);
146
+ __decorate([
147
+ (0, core_1.Inject)(),
148
+ __metadata("design:type", core_1.MidwayTraceService)
149
+ ], CachingFactory.prototype, "traceService", void 0);
150
+ __decorate([
151
+ (0, core_1.Config)('cacheManager.tracing.meta'),
152
+ __metadata("design:type", Object)
153
+ ], CachingFactory.prototype, "traceMetaResolver", void 0);
154
+ __decorate([
155
+ (0, core_1.Config)('cacheManager.tracing.enable'),
156
+ __metadata("design:type", Object)
157
+ ], CachingFactory.prototype, "traceEnabled", void 0);
158
+ __decorate([
159
+ (0, core_1.Config)('cacheManager.tracing.injector'),
160
+ __metadata("design:type", Object)
161
+ ], CachingFactory.prototype, "traceInjector", void 0);
84
162
  __decorate([
85
163
  (0, core_1.Init)(),
86
164
  __metadata("design:type", Function),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/cache-manager",
3
- "version": "4.0.0-beta.11",
3
+ "version": "4.0.0-beta.13",
4
4
  "description": "midway cache manager",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -28,9 +28,9 @@
28
28
  "node": ">=20"
29
29
  },
30
30
  "devDependencies": {
31
- "@midwayjs/core": "^4.0.0-beta.11",
32
- "@midwayjs/mock": "^4.0.0-beta.11",
33
- "@midwayjs/redis": "^4.0.0-beta.11",
31
+ "@midwayjs/core": "^4.0.0-beta.13",
32
+ "@midwayjs/mock": "^4.0.0-beta.13",
33
+ "@midwayjs/redis": "^4.0.0-beta.13",
34
34
  "cache-manager": "6.0.0",
35
35
  "cache-manager-ioredis-yet": "2.1.2"
36
36
  },
@@ -38,5 +38,5 @@
38
38
  "lodash.clonedeep": "4.5.0",
39
39
  "lru-cache": "7.18.3"
40
40
  },
41
- "gitHead": "6ef05719ca6e900f1ec34aff7a5c5a9614358c50"
41
+ "gitHead": "9a38b66a84a6880370cac90d737f94f9c5f2f256"
42
42
  }