@steedos/service-cachers-manager 2.5.3-beta.2 → 2.5.3-beta.20

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.
Files changed (2) hide show
  1. package/package.json +4 -3
  2. package/package.service.js +124 -47
package/package.json CHANGED
@@ -1,14 +1,15 @@
1
1
  {
2
2
  "name": "@steedos/service-cachers-manager",
3
- "version": "2.5.3-beta.2",
3
+ "version": "2.5.3-beta.20",
4
4
  "main": "package.service.js",
5
5
  "license": "MIT",
6
6
  "private": false,
7
7
  "dependencies": {
8
- "@steedos/cachers": "2.5.3-beta.2"
8
+ "@steedos/cachers": "2.5.3-beta.20",
9
+ "@steedos/metadata-registrar": "2.5.3-beta.20"
9
10
  },
10
11
  "publishConfig": {
11
12
  "access": "public"
12
13
  },
13
- "gitHead": "321fe88aba6ddc3380a0d71eaa2e9a18186d49f8"
14
+ "gitHead": "32606d2d07b6e53a4d6c8ba079e6356f5a2dbb75"
14
15
  }
@@ -2,7 +2,7 @@
2
2
  * @Author: baozhoutao@steedos.com
3
3
  * @Date: 2022-03-28 09:35:35
4
4
  * @LastEditors: baozhoutao@steedos.com
5
- * @LastEditTime: 2023-05-26 11:29:13
5
+ * @LastEditTime: 2023-06-09 17:34:57
6
6
  * @Description: 维护内存缓存
7
7
  */
8
8
  "use strict";
@@ -12,6 +12,7 @@ const core = require('@steedos/core');
12
12
  const cachers = require('@steedos/cachers');
13
13
  const auth = require('@steedos/auth');
14
14
  const { getObject } = require('@steedos/objectql');
15
+ const register = require('@steedos/metadata-registrar');
15
16
  /**
16
17
  * @typedef {import('moleculer').Context} Context Moleculer's Context
17
18
  * 软件包服务启动后也需要抛出事件。
@@ -35,16 +36,33 @@ module.exports = {
35
36
  // 加载mo action规则的triggers
36
37
  loadActionTriggers: async function (broker) {
37
38
  const cache = cachers.getCacher('action-triggers');
38
- broker.call('triggers.getAll').then((res)=>{
39
+ broker.call('triggers.getAll').then((res) => {
39
40
  cache.set('triggerActions', res);
40
41
  })
41
42
  },
42
43
  // 加载 steedos 规则的triggers
43
- loadTriggers: async function(broker){
44
+ loadTriggers: async function (broker) {
44
45
  const cache = cachers.getCacher('triggers');
45
- broker.call('object_triggers.getAll').then((res)=>{
46
+ broker.call('object_triggers.getAll').then((res) => {
46
47
  cache.set('triggers', res);
47
48
  })
49
+ },
50
+ loadProfiles: async function () {
51
+ const cache = cachers.getCacher('profiles');
52
+ const profileDocs = await getObject('permission_set').directFind({
53
+ filters: [
54
+ ['type', '=', 'profile'],
55
+ ],
56
+ });
57
+ for (const doc of profileDocs) {
58
+ // 库里的简档
59
+ cache.set(`${doc.space}_${doc.name}`, doc);
60
+ }
61
+ const sourceProfiles = await register.getSourceProfiles()
62
+ for (const doc of sourceProfiles) {
63
+ // 代码定义的简档
64
+ cache.set(doc.name, doc);
65
+ }
48
66
  }
49
67
  },
50
68
 
@@ -54,10 +72,10 @@ module.exports = {
54
72
  events: {
55
73
  "translations.change": {
56
74
  handler() {
57
- if(this.translationsChangeTimeoutId){
75
+ if (this.translationsChangeTimeoutId) {
58
76
  clearTimeout(this.translationsChangeTimeoutId)
59
77
  }
60
- this.translationsChangeTimeoutId = setTimeout(()=>{
78
+ this.translationsChangeTimeoutId = setTimeout(() => {
61
79
  core.loadTranslations()
62
80
  this.translationsChangeTimeoutId = null;
63
81
  }, 2000)
@@ -65,25 +83,25 @@ module.exports = {
65
83
  },
66
84
  "translations.object.change": {
67
85
  handler() {
68
- if(this.objectTranslationsChangeTimeoutId){
86
+ if (this.objectTranslationsChangeTimeoutId) {
69
87
  clearTimeout(this.objectTranslationsChangeTimeoutId)
70
88
  }
71
- this.objectTranslationsChangeTimeoutId = setTimeout(()=>{
72
- core.loadObjectTranslations().then(()=>{
89
+ this.objectTranslationsChangeTimeoutId = setTimeout(() => {
90
+ core.loadObjectTranslations().then(() => {
73
91
  cachers.getCacher('lru.translations.objects').clear();
74
92
  })
75
93
  this.objectTranslationsChangeTimeoutId = null;
76
94
  }, 2000)
77
-
95
+
78
96
  }
79
97
  },
80
98
  "triggers.change": {
81
- handler(ctx){
99
+ handler(ctx) {
82
100
  this.loadActionTriggers(ctx.broker);
83
101
  }
84
102
  },
85
103
  "metadata.object_triggers.change": {
86
- handler(ctx){
104
+ handler(ctx) {
87
105
  this.loadTriggers(ctx.broker);
88
106
  }
89
107
  },
@@ -92,9 +110,9 @@ module.exports = {
92
110
  * 当space_users属性值发生变更后清除userSession缓存
93
111
  */
94
112
  "@space_users.updated": {
95
- handler(ctx){
113
+ handler(ctx) {
96
114
  const params = ctx.params
97
- const { isUpdate, isAfter} = params;
115
+ const { isUpdate, isAfter } = params;
98
116
  if (isAfter && isUpdate) {
99
117
  auth.deleteSpaceUserSessionCacheByChangedProp(params.doc, params.previousDoc)
100
118
  }
@@ -104,68 +122,127 @@ module.exports = {
104
122
  * userSession支持实时更新
105
123
  * 当spaces属性值发生变更后清除spaces缓存
106
124
  */
107
- "@spaces.updated": {
108
- handler(ctx){
125
+ "@spaces.updated": {
126
+ handler(ctx) {
109
127
  const params = ctx.params
110
- const { isUpdate, isAfter} = params;
128
+ const { isUpdate, isAfter } = params;
111
129
  if (isAfter && isUpdate) {
112
130
  auth.deleteSpaceCacheByChangedProp(params.doc, params.previousDoc)
113
131
  }
114
132
  }
115
133
  },
116
134
  "$services.changed": {
117
- async handler(ctx) {
118
- const { broker } = ctx
119
- const _services = broker.registry.getServiceList({ skipInternal: true, withActions: true });
135
+ async handler(ctx) {
136
+ const { broker } = ctx
137
+ const _services = broker.registry.getServiceList({ skipInternal: true, withActions: true });
120
138
  const globalServicesVars = {};
121
- for (const service of _services) {
122
- const { name: serviceName, actions } = service
123
- if(!globalServicesVars[serviceName]){
139
+ for (const service of _services) {
140
+ const { name: serviceName, actions } = service
141
+ if (!globalServicesVars[serviceName]) {
124
142
  globalServicesVars[serviceName] = {};
125
143
  }
126
- for (const key in actions) {
127
- if (Object.prototype.hasOwnProperty.call(actions, key)) {
144
+ for (const key in actions) {
145
+ if (Object.prototype.hasOwnProperty.call(actions, key)) {
128
146
  const rawName = actions[key].rawName;
129
- globalServicesVars[serviceName][rawName] = async function(params, opts){
147
+ globalServicesVars[serviceName][rawName] = async function (params, opts) {
130
148
  return await broker.call(key, params, opts)
131
149
  }
132
- }
133
- }
134
- }
150
+ }
151
+ }
152
+ }
135
153
  global.services = globalServicesVars;
136
154
  // console.log('===========global.services===========');
137
155
  // console.log(global.services)
138
- }
139
- },
156
+ }
157
+ },
140
158
  "$packages.changed": {
141
159
  params: {},
142
160
  async handler(ctx) {
143
161
  const objects = {}
144
162
  const objectConfigs = await this.broker.call("objects.getAll");
145
163
  for (const object of objectConfigs) {
146
- const objectConfig = object.metadata
147
- const objectName = objectConfig.name
148
- // 排除 __MONGO_BASE_OBJECT __SQL_BASE_OBJECT
149
- if (['__MONGO_BASE_OBJECT', '__SQL_BASE_OBJECT'].includes(objectName)) {
150
- continue
151
- }
152
-
153
- const obj = getObject(objectName);
154
- if(!objects[objectName]){
155
- objects[objectName] = {}
156
- }
164
+ try {
165
+ const objectConfig = object.metadata
166
+ const objectName = objectConfig.name
167
+ // 排除 __MONGO_BASE_OBJECT __SQL_BASE_OBJECT
168
+ if (['__MONGO_BASE_OBJECT', '__SQL_BASE_OBJECT'].includes(objectName)) {
169
+ continue
170
+ }
157
171
 
158
- //TODO 确认 delete\directDelete 功能
159
- _.each(['find', 'count', 'findOne', 'insert', 'update', 'delete', 'directFind', 'directInsert', 'directUpdate', 'directDelete'], (funKey)=>{
160
- objects[objectName][funKey] = function(...args){
161
- return obj[funKey].apply(obj, args) // 重写this为obj, 防止this异常
172
+ const obj = getObject(objectName);
173
+ if (!objects[objectName]) {
174
+ objects[objectName] = {}
162
175
  }
163
- })
176
+
177
+ //TODO 确认 delete\directDelete 功能
178
+ _.each(['find', 'count', 'findOne', 'insert', 'update', 'delete', 'directFind', 'directInsert', 'directUpdate', 'directDelete'], (funKey) => {
179
+ objects[objectName][funKey] = function (...args) {
180
+ return obj[funKey].apply(obj, args) // 重写this为obj, 防止this异常
181
+ }
182
+ })
183
+ } catch (ex) {
184
+ console.error(`====>`,ex)
185
+ }
164
186
  };
165
187
  // console.log('===========global.objects===========');
166
188
  // console.log(objects)
167
189
  global.objects = objects;
168
190
  }
191
+ },
192
+ "steedos-server.started": {
193
+ async handler(ctx) {
194
+ // 初始化缓存
195
+ await this.loadProfiles();
196
+ }
197
+ },
198
+ "@permission_set.*": {
199
+ async handler(ctx) {
200
+ // 数据库数据变化后,重新加载缓存
201
+ const params = ctx.params
202
+ const { isUpdate, isAfter, isInsert, isDelete, doc, previousDoc } = params;
203
+ if (isAfter && (isUpdate || isInsert || isDelete) && ('profile' === (doc || {}).type || 'profile' === (previousDoc || {}).type)) {
204
+ // 先清理缓存
205
+ cachers.clearCacher('profiles');
206
+ // 重新添加缓存
207
+ await this.loadProfiles();
208
+ }
209
+ }
210
+ },
211
+ "$METADATA.profiles.*": {
212
+ async handler(ctx) {
213
+ if (this.profilesChangeTimeoutId) {
214
+ clearTimeout(this.profilesChangeTimeoutId)
215
+ }
216
+ this.profilesChangeTimeoutId = setTimeout(() => {
217
+ // 先清理缓存
218
+ cachers.clearCacher('profiles');
219
+ // 重新添加缓存
220
+ this.loadProfiles();
221
+ this.profilesChangeTimeoutId = null;
222
+ }, 2000)
223
+ }
224
+ }
225
+ },
226
+
227
+ actions: {
228
+ getProfile: {
229
+ params: {
230
+ name: { type: "string" },
231
+ spaceId: { type: "string", optional: true }
232
+ },
233
+ async handler(ctx) {
234
+ const { name, spaceId } = ctx.params;
235
+
236
+ const cache = cachers.getCacher('profiles');
237
+ let profile;
238
+ if (spaceId) {
239
+ profile = cache.get(`${spaceId}_${name}`);
240
+ }
241
+ if (!profile) {
242
+ profile = cache.get(name);
243
+ }
244
+ return profile;
245
+ }
169
246
  }
170
247
  },
171
248