@steedos/service-cachers-manager 2.5.3-beta.8 → 2.5.4

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 +108 -35
package/package.json CHANGED
@@ -1,14 +1,15 @@
1
1
  {
2
2
  "name": "@steedos/service-cachers-manager",
3
- "version": "2.5.3-beta.8",
3
+ "version": "2.5.4",
4
4
  "main": "package.service.js",
5
5
  "license": "MIT",
6
6
  "private": false,
7
7
  "dependencies": {
8
- "@steedos/cachers": "2.5.3-beta.8"
8
+ "@steedos/cachers": "2.5.4",
9
+ "@steedos/metadata-registrar": "2.5.4"
9
10
  },
10
11
  "publishConfig": {
11
12
  "access": "public"
12
13
  },
13
- "gitHead": "7ac3c90fc78437f905b268c700d5ab414ca2c5a1"
14
+ "gitHead": "0b1b93c80e8448e54b77064d976b9e20bfe39727"
14
15
  }
@@ -1,8 +1,8 @@
1
1
  /*
2
2
  * @Author: baozhoutao@steedos.com
3
3
  * @Date: 2022-03-28 09:35:35
4
- * @LastEditors: baozhoutao@steedos.com
5
- * @LastEditTime: 2023-05-26 11:29:13
4
+ * @LastEditors: sunhaolin@hotoa.com
5
+ * @LastEditTime: 2023-06-09 11:44:06
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,39 +122,39 @@ 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) {
@@ -151,13 +169,13 @@ module.exports = {
151
169
  }
152
170
 
153
171
  const obj = getObject(objectName);
154
- if(!objects[objectName]){
172
+ if (!objects[objectName]) {
155
173
  objects[objectName] = {}
156
174
  }
157
175
 
158
176
  //TODO 确认 delete\directDelete 功能
159
- _.each(['find', 'count', 'findOne', 'insert', 'update', 'delete', 'directFind', 'directInsert', 'directUpdate', 'directDelete'], (funKey)=>{
160
- objects[objectName][funKey] = function(...args){
177
+ _.each(['find', 'count', 'findOne', 'insert', 'update', 'delete', 'directFind', 'directInsert', 'directUpdate', 'directDelete'], (funKey) => {
178
+ objects[objectName][funKey] = function (...args) {
161
179
  return obj[funKey].apply(obj, args) // 重写this为obj, 防止this异常
162
180
  }
163
181
  })
@@ -166,6 +184,61 @@ module.exports = {
166
184
  // console.log(objects)
167
185
  global.objects = objects;
168
186
  }
187
+ },
188
+ "steedos-server.started": {
189
+ async handler(ctx) {
190
+ // 初始化缓存
191
+ await this.loadProfiles();
192
+ }
193
+ },
194
+ "@permission_set.*": {
195
+ async handler(ctx) {
196
+ // 数据库数据变化后,重新加载缓存
197
+ const params = ctx.params
198
+ const { isUpdate, isAfter, isInsert, isDelete, doc, previousDoc } = params;
199
+ if (isAfter && (isUpdate || isInsert || isDelete) && ('profile' === (doc || {}).type || 'profile' === (previousDoc || {}).type)) {
200
+ // 先清理缓存
201
+ cachers.clearCacher('profiles');
202
+ // 重新添加缓存
203
+ await this.loadProfiles();
204
+ }
205
+ }
206
+ },
207
+ "$METADATA.profiles.*": {
208
+ async handler(ctx) {
209
+ if (this.profilesChangeTimeoutId) {
210
+ clearTimeout(this.profilesChangeTimeoutId)
211
+ }
212
+ this.profilesChangeTimeoutId = setTimeout(() => {
213
+ // 先清理缓存
214
+ cachers.clearCacher('profiles');
215
+ // 重新添加缓存
216
+ this.loadProfiles();
217
+ this.profilesChangeTimeoutId = null;
218
+ }, 2000)
219
+ }
220
+ }
221
+ },
222
+
223
+ actions: {
224
+ getProfile: {
225
+ params: {
226
+ name: { type: "string" },
227
+ spaceId: { type: "string", optional: true }
228
+ },
229
+ async handler(ctx) {
230
+ const { name, spaceId } = ctx.params;
231
+
232
+ const cache = cachers.getCacher('profiles');
233
+ let profile;
234
+ if (spaceId) {
235
+ profile = cache.get(`${spaceId}_${name}`);
236
+ }
237
+ if (!profile) {
238
+ profile = cache.get(name);
239
+ }
240
+ return profile;
241
+ }
169
242
  }
170
243
  },
171
244