@abtnode/rbac 1.16.43-beta-20250424-125523-08a65a5c → 1.16.43-beta-20250427-132304-6da95c55

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.
@@ -1,4 +1,6 @@
1
1
  const pick = require('lodash/pick');
2
+ const SingleFlightLRUCache = require('@abtnode/util/lib/single-flight-lru-cache');
3
+ const { RBAC_CACHE_TTL } = require('@abtnode/constant');
2
4
 
3
5
  const Storage = require('../core/storage');
4
6
  const Permission = require('../core/permission');
@@ -15,6 +17,11 @@ class SequelizeStorage extends Storage {
15
17
  this.db = db;
16
18
  }
17
19
 
20
+ cache = new SingleFlightLRUCache({
21
+ max: 1000,
22
+ ttl: RBAC_CACHE_TTL,
23
+ });
24
+
18
25
  async add(item) {
19
26
  const { name } = item;
20
27
 
@@ -39,6 +46,7 @@ class SequelizeStorage extends Storage {
39
46
  }
40
47
 
41
48
  await this.db.insert(doc);
49
+ this.cache.clear();
42
50
 
43
51
  return true;
44
52
  }
@@ -55,6 +63,7 @@ class SequelizeStorage extends Storage {
55
63
  await Promise.all(
56
64
  docs.map((doc) => this.db.update({ id: doc.id }, { $set: { grants: doc.grants.filter((x) => x !== name) } }))
57
65
  );
66
+ this.cache.clear();
58
67
  return true;
59
68
  }
60
69
 
@@ -66,6 +75,7 @@ class SequelizeStorage extends Storage {
66
75
  }
67
76
 
68
77
  await this.db.update({ name }, pick({ title, description, extra }, 'title', 'description', 'extra'));
78
+ this.cache.clear();
69
79
  return this.get(name);
70
80
  }
71
81
 
@@ -96,6 +106,8 @@ class SequelizeStorage extends Storage {
96
106
  )
97
107
  );
98
108
 
109
+ this.cache.clear();
110
+
99
111
  return true;
100
112
  }
101
113
 
@@ -112,48 +124,61 @@ class SequelizeStorage extends Storage {
112
124
  docs.map((doc) => this.db.update({ id: doc.id }, { grants: doc.grants.filter((x) => x !== childName) }))
113
125
  );
114
126
 
127
+ this.cache.clear();
128
+
115
129
  return true;
116
130
  }
117
131
 
118
- async get(name) {
119
- const item = await this.db.findOne({ name });
132
+ get(name) {
133
+ const cacheKey = JSON.stringify({ type: 'get', name });
134
+ return this.cache.autoCache(cacheKey, async () => {
135
+ const item = await this.db.findOne({ name });
120
136
 
121
- if (!item) {
122
- return undefined;
123
- }
137
+ if (!item) {
138
+ return undefined;
139
+ }
124
140
 
125
- if (item.type === ItemTypes.Role) {
126
- return this.resolveRole(item);
127
- }
141
+ if (item.type === ItemTypes.Role) {
142
+ return this.resolveRole(item);
143
+ }
128
144
 
129
- if (item.type === ItemTypes.Permission) {
130
- return this.resolvePermission(item);
131
- }
145
+ if (item.type === ItemTypes.Permission) {
146
+ return this.resolvePermission(item);
147
+ }
132
148
 
133
- return undefined;
149
+ return undefined;
150
+ });
134
151
  }
135
152
 
136
- async getRoles() {
137
- const roles = await this.db.find({ type: ItemTypes.Role }, {}, { createdAt: -1 });
138
- return roles.map((item) => this.resolveRole(item));
153
+ getRoles() {
154
+ const cacheKey = JSON.stringify({ type: 'getRoles' });
155
+ return this.cache.autoCache(cacheKey, async () => {
156
+ const roles = await this.db.find({ type: ItemTypes.Role }, {}, { createdAt: -1 });
157
+ return roles.map((item) => this.resolveRole(item));
158
+ });
139
159
  }
140
160
 
141
- async getPermissions() {
142
- const permissions = await this.db.find({ type: ItemTypes.Permission }, {}, { createdAt: -1 });
143
- return permissions.map((item) => this.resolvePermission(item));
161
+ getPermissions() {
162
+ const cacheKey = JSON.stringify({ type: 'getPermissions' });
163
+ return this.cache.autoCache(cacheKey, async () => {
164
+ const permissions = await this.db.find({ type: ItemTypes.Permission }, {}, { createdAt: -1 });
165
+ return permissions.map((item) => this.resolvePermission(item));
166
+ });
144
167
  }
145
168
 
146
- async getGrants(name) {
147
- const item = await this.db.findOne({ name });
169
+ getGrants(name) {
170
+ const cacheKey = JSON.stringify({ type: 'getGrants', name });
171
+ return this.cache.autoCache(cacheKey, async () => {
172
+ const item = await this.db.findOne({ name });
148
173
 
149
- if (item) {
150
- const grants = await Promise.all((item.grants || []).map((x) => this.get(x)));
151
- return grants;
152
- }
174
+ if (item) {
175
+ const grants = await Promise.all((item.grants || []).map((x) => this.get(x)));
176
+ return grants;
177
+ }
153
178
 
154
- return [];
179
+ return [];
180
+ });
155
181
  }
156
-
157
182
  // custom
158
183
 
159
184
  async updateGrants(roleName, grantNames = []) {
@@ -184,6 +209,7 @@ class SequelizeStorage extends Storage {
184
209
  });
185
210
 
186
211
  await this.db.update({ name: roleName }, { grants: grantNames });
212
+ this.cache.clear();
187
213
  return this.get(roleName);
188
214
  }
189
215
 
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.43-beta-20250424-125523-08a65a5c",
6
+ "version": "1.16.43-beta-20250427-132304-6da95c55",
7
7
  "description": "Simple lib to manage access controls in ABT Node",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,12 +19,14 @@
19
19
  "author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
20
20
  "license": "Apache-2.0",
21
21
  "dependencies": {
22
- "@abtnode/db": "1.16.43-beta-20250424-125523-08a65a5c",
22
+ "@abtnode/constant": "1.16.43-beta-20250427-132304-6da95c55",
23
+ "@abtnode/db": "1.16.43-beta-20250427-132304-6da95c55",
24
+ "@abtnode/util": "1.16.43-beta-20250427-132304-6da95c55",
23
25
  "fs-extra": "^11.2.0",
24
26
  "lodash": "^4.17.21"
25
27
  },
26
28
  "devDependencies": {
27
29
  "jest": "^29.7.0"
28
30
  },
29
- "gitHead": "499de9f28770298a819122ba16a7f2b3cc78c7ee"
31
+ "gitHead": "9df10dcb3135a528912241e7fc1ed54171bfeb03"
30
32
  }