midway-fatcms 0.0.1-beta.64 → 0.0.1-beta.66

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 (108) hide show
  1. package/dist/config/config.default.js +7 -0
  2. package/dist/controller/base/BaseApiController.d.ts +5 -0
  3. package/dist/controller/base/BaseApiController.js +25 -0
  4. package/dist/controller/gateway/CrudStdGatewayController.d.ts +1 -1
  5. package/dist/controller/gateway/CrudStdGatewayController.js +36 -10
  6. package/dist/controller/gateway/FileController.js +2 -1
  7. package/dist/controller/gateway/PublicApiController.d.ts +1 -0
  8. package/dist/controller/gateway/PublicApiController.js +64 -28
  9. package/dist/index.d.ts +1 -1
  10. package/dist/index.js +1 -1
  11. package/dist/interface.d.ts +2 -0
  12. package/dist/libs/crud-pro/exceptions.d.ts +2 -0
  13. package/dist/libs/crud-pro/exceptions.js +2 -0
  14. package/dist/libs/crud-pro/models/keys.d.ts +1 -1
  15. package/dist/libs/crud-pro/models/keys.js +1 -1
  16. package/dist/libs/crud-pro/services/CrudProOriginToExecuteSql.js +6 -1
  17. package/dist/libs/crud-pro/utils/sqlConvert/convertColumnName.js +14 -0
  18. package/dist/libs/utils/AsymmetricCrypto.d.ts +76 -0
  19. package/dist/libs/utils/AsymmetricCrypto.js +261 -0
  20. package/dist/libs/utils/format-url.d.ts +2 -0
  21. package/dist/libs/utils/format-url.js +13 -0
  22. package/dist/libs/utils/render-utils.d.ts +16 -7
  23. package/dist/libs/utils/render-utils.js +27 -6
  24. package/dist/middleware/global.middleware.js +7 -0
  25. package/dist/models/SystemEntities.d.ts +34 -0
  26. package/dist/models/SystemEntities.js +22 -1
  27. package/dist/models/WorkbenchInfoTools.d.ts +7 -0
  28. package/dist/models/WorkbenchInfoTools.js +20 -0
  29. package/dist/models/bizmodels.d.ts +9 -0
  30. package/dist/models/contextLogger.d.ts +2 -0
  31. package/dist/models/contextLogger.js +8 -1
  32. package/dist/models/userSession.d.ts +2 -0
  33. package/dist/models/userSession.js +2 -0
  34. package/dist/schedule/anonymousContext.js +2 -0
  35. package/dist/service/AuthService.js +7 -0
  36. package/dist/service/EnumInfoService.d.ts +1 -1
  37. package/dist/service/EnumInfoService.js +32 -26
  38. package/dist/service/UserSessionService.d.ts +6 -1
  39. package/dist/service/UserSessionService.js +26 -17
  40. package/dist/service/WorkbenchService.d.ts +1 -0
  41. package/dist/service/WorkbenchService.js +27 -1
  42. package/dist/service/base/cache/CacheServiceFactory.d.ts +20 -0
  43. package/dist/service/base/cache/CacheServiceFactory.js +67 -0
  44. package/dist/service/base/cache/FatcmsBaseCtxCache.d.ts +19 -0
  45. package/dist/service/base/cache/FatcmsBaseCtxCache.js +38 -0
  46. package/dist/service/base/cache/FatcmsBaseDiskCache.d.ts +24 -0
  47. package/dist/service/base/cache/FatcmsBaseDiskCache.js +81 -0
  48. package/dist/service/base/cache/FatcmsBaseMemoryCache.d.ts +18 -0
  49. package/dist/service/base/cache/FatcmsBaseMemoryCache.js +66 -0
  50. package/dist/service/base/cache/FatcmsBaseNoneCache.d.ts +10 -0
  51. package/dist/service/base/cache/FatcmsBaseNoneCache.js +19 -0
  52. package/dist/service/base/{RedisCacheService.d.ts → cache/FatcmsBaseRedisCache.d.ts} +6 -6
  53. package/dist/service/base/cache/FatcmsBaseRedisCache.js +39 -0
  54. package/dist/service/crudstd/CrudStdService.d.ts +6 -4
  55. package/dist/service/crudstd/CrudStdService.js +24 -10
  56. package/dist/service/curd/CurdMixByDictService.d.ts +1 -2
  57. package/dist/service/curd/CurdMixByDictService.js +12 -8
  58. package/dist/service/curd/CurdMixByLinkToCustomService.d.ts +1 -2
  59. package/dist/service/curd/CurdMixByLinkToCustomService.js +17 -13
  60. package/dist/service/curd/CurdMixBySysConfigService.d.ts +4 -0
  61. package/dist/service/curd/CurdMixBySysConfigService.js +50 -10
  62. package/dist/service/curd/CurdMixByWorkbenchService.d.ts +2 -1
  63. package/dist/service/curd/CurdMixByWorkbenchService.js +30 -22
  64. package/package.json +1 -1
  65. package/src/config/config.default.ts +8 -0
  66. package/src/controller/base/BaseApiController.ts +39 -0
  67. package/src/controller/gateway/CrudStdGatewayController.ts +42 -10
  68. package/src/controller/gateway/FileController.ts +2 -2
  69. package/src/controller/gateway/PublicApiController.ts +72 -28
  70. package/src/controller/manage/DocLibManageApi.ts +5 -5
  71. package/src/controller/manage/FileManageApi.ts +5 -5
  72. package/src/controller/manage/MenuManageApi.ts +4 -4
  73. package/src/index.ts +1 -1
  74. package/src/interface.ts +2 -0
  75. package/src/libs/crud-pro/exceptions.ts +2 -0
  76. package/src/libs/crud-pro/models/keys.ts +6 -6
  77. package/src/libs/crud-pro/services/CrudProOriginToExecuteSql.ts +7 -1
  78. package/src/libs/crud-pro/utils/sqlConvert/convertColumnName.ts +18 -0
  79. package/src/libs/utils/AsymmetricCrypto.ts +310 -0
  80. package/src/libs/utils/format-url.ts +16 -0
  81. package/src/libs/utils/render-utils.ts +56 -15
  82. package/src/middleware/global.middleware.ts +8 -0
  83. package/src/models/SystemEntities.ts +43 -0
  84. package/src/models/WorkbenchInfoTools.ts +18 -0
  85. package/src/models/bizmodels.ts +12 -0
  86. package/src/models/contextLogger.ts +10 -1
  87. package/src/models/userSession.ts +4 -0
  88. package/src/schedule/anonymousContext.ts +2 -0
  89. package/src/service/AuthService.ts +11 -0
  90. package/src/service/EnumInfoService.ts +35 -22
  91. package/src/service/UserSessionService.ts +29 -18
  92. package/src/service/WorkbenchService.ts +42 -2
  93. package/src/service/base/cache/CacheServiceFactory.ts +66 -0
  94. package/src/service/base/cache/FatcmsBaseCtxCache.ts +47 -0
  95. package/src/service/base/cache/FatcmsBaseDiskCache.ts +87 -0
  96. package/src/service/base/cache/FatcmsBaseMemoryCache.ts +74 -0
  97. package/src/service/base/cache/FatcmsBaseNoneCache.ts +24 -0
  98. package/src/service/base/cache/FatcmsBaseRedisCache.ts +48 -0
  99. package/src/service/crudstd/CrudStdService.ts +28 -12
  100. package/src/service/curd/CurdMixByAccountService.ts +1 -0
  101. package/src/service/curd/CurdMixByDictService.ts +14 -8
  102. package/src/service/curd/CurdMixByLinkToCustomService.ts +21 -12
  103. package/src/service/curd/CurdMixBySysConfigService.ts +60 -11
  104. package/src/service/curd/CurdMixByWorkbenchService.ts +31 -24
  105. package/src/service/proxyapi/WeightedRandom.ts +1 -1
  106. package/src/service/proxyapi/WeightedRoundRobin.ts +1 -1
  107. package/dist/service/base/RedisCacheService.js +0 -57
  108. package/src/service/base/RedisCacheService.ts +0 -42
@@ -0,0 +1,66 @@
1
+ import { Inject, Provide, Config } from '@midwayjs/core';
2
+ import { Context } from '@midwayjs/koa';
3
+ import { ICacheServiceBase, IfatcmsCacheConfig } from '@/models/bizmodels';
4
+ import { CacheLevelEnum, CacheNameEnum } from '@/models/SystemEntities';
5
+ import { RedisService } from '@midwayjs/redis';
6
+ import { FatcmsBaseCtxCache } from './FatcmsBaseCtxCache';
7
+ import { FatcmsBaseRedisCache } from './FatcmsBaseRedisCache';
8
+ import { FatcmsBaseDiskCache } from './FatcmsBaseDiskCache';
9
+ import { FatcmsBaseMemoryCache } from './FatcmsBaseMemoryCache';
10
+ import { FatcmsBaseNoneCache } from './FatcmsBaseNoneCache';
11
+
12
+ interface IGetJsonObjectCacheParam {
13
+ cacheLevel: CacheLevelEnum;
14
+ cacheName: CacheNameEnum;
15
+ cacheKey: string;
16
+ cacheSecond: number;
17
+ refreshCache?: boolean;
18
+ getter: () => Promise<any>;
19
+ }
20
+
21
+ @Provide()
22
+ export class CacheServiceFactory {
23
+ @Inject()
24
+ protected ctx: Context;
25
+
26
+ @Config('fatcmsCache')
27
+ private fatcmsCache: IfatcmsCacheConfig;
28
+
29
+ @Inject()
30
+ protected redisService: RedisService;
31
+
32
+ public getCacheService(cacheLevelEnum: CacheLevelEnum, cacheName: CacheNameEnum): ICacheServiceBase {
33
+ switch (cacheLevelEnum) {
34
+ case CacheLevelEnum.CONTEXT:
35
+ return new FatcmsBaseCtxCache(this.ctx, cacheName);
36
+ case CacheLevelEnum.REDIS:
37
+ return new FatcmsBaseRedisCache(this.ctx, cacheName, this.redisService);
38
+ case CacheLevelEnum.DISK:
39
+ return new FatcmsBaseDiskCache(this.ctx, cacheName, this.fatcmsCache);
40
+ case CacheLevelEnum.MEMORY:
41
+ return new FatcmsBaseMemoryCache(this.ctx, cacheName);
42
+ default:
43
+ return new FatcmsBaseNoneCache(this.ctx, cacheName);
44
+ }
45
+ }
46
+
47
+ public async getJsonObjectCache(param: IGetJsonObjectCacheParam): Promise<any> {
48
+ let { cacheLevel, cacheName, cacheKey, cacheSecond, refreshCache, getter } = param;
49
+
50
+ if (!cacheSecond || typeof cacheSecond !== 'number') {
51
+ console.error('[CacheServiceFactory#getJsonObjectCache] cacheSecond is not number, use default value 60, ' + JSON.stringify(param));
52
+ cacheSecond = 60;
53
+ }
54
+
55
+ const realCacheKey = this.fatcmsCache.keyPrefix + cacheKey;
56
+
57
+ const cacheService = this.getCacheService(cacheLevel, cacheName);
58
+ let obj = cacheService.getJsonObject(realCacheKey);
59
+ if (!obj || refreshCache === true) {
60
+ obj = await getter();
61
+ cacheService.setJsonObject(realCacheKey, obj, cacheSecond);
62
+ }
63
+ return obj;
64
+ }
65
+
66
+ }
@@ -0,0 +1,47 @@
1
+ import { ICacheServiceBase } from '@/models/bizmodels';
2
+ import { CacheNameEnum } from '@/models/SystemEntities';
3
+ import { Context } from '@midwayjs/koa';
4
+
5
+
6
+
7
+ export class FatcmsBaseCtxCache implements ICacheServiceBase {
8
+ private ctx: Context;
9
+ private cacheName: CacheNameEnum;
10
+
11
+ constructor(ctx: Context, cacheName: CacheNameEnum) {
12
+ this.ctx = ctx;
13
+ this.cacheName = cacheName;
14
+ }
15
+
16
+ private getCacheMap(): Map<string, any> {
17
+ if (!this.ctx.baseCtxCacheMap) {
18
+ this.ctx.baseCtxCacheMap = new Map();
19
+ }
20
+ if (!this.ctx.baseCtxCacheMap.has(this.cacheName)) {
21
+ this.ctx.baseCtxCacheMap.set(this.cacheName, new Map());
22
+ }
23
+ return this.ctx.baseCtxCacheMap.get(this.cacheName);
24
+ }
25
+
26
+ async getJsonObject(key: string): Promise<any> {
27
+ const cacheMap = this.getCacheMap();
28
+ return cacheMap.get(key);
29
+ }
30
+
31
+ /**
32
+ * 设置缓存对象
33
+ * @param key
34
+ * @param obj
35
+ * @param expireSecond 过期时间,单位秒
36
+ * @returns
37
+ */
38
+ async setJsonObject(key: string, obj: any, expireSecond?: number): Promise<any> {
39
+ const cacheMap = this.getCacheMap();
40
+ cacheMap.set(key, obj);
41
+ }
42
+
43
+ async removeItem(key: string): Promise<any> {
44
+ const cacheMap = this.getCacheMap();
45
+ cacheMap.delete(key);
46
+ }
47
+ }
@@ -0,0 +1,87 @@
1
+ import { Context } from '@midwayjs/koa';
2
+ import * as md5 from 'md5';
3
+ import * as fs from 'fs/promises';
4
+ import * as fs2 from 'fs';
5
+ import * as path from 'path';
6
+ import { parseJsonObject } from '../../../libs/utils/functions';
7
+ import { ICacheServiceBase, IfatcmsCacheConfig } from '@/models/bizmodels';
8
+
9
+
10
+ export class FatcmsBaseDiskCache implements ICacheServiceBase {
11
+ protected ctx: Context;
12
+ protected cacheName: string;
13
+ private fatcmsCache: IfatcmsCacheConfig;
14
+
15
+ constructor(ctx: Context, cacheName: string, fatcmsCache: IfatcmsCacheConfig) {
16
+ this.ctx = ctx;
17
+ this.cacheName = cacheName;
18
+ this.fatcmsCache = fatcmsCache;
19
+ }
20
+
21
+
22
+ /**
23
+ * 获取缓存文件路径
24
+ * @param key
25
+ * @returns
26
+ */
27
+ private async getCacheFilePath(key: string, checkDirExist: boolean = true): Promise<string> {
28
+ const cacheDir0 = this.fatcmsCache.diskCacheDir;
29
+ const cacheDir = path.join(cacheDir0, this.cacheName);
30
+ if (checkDirExist) {
31
+ const isDirExist = await fs2.promises.access(cacheDir).then(() => true, () => false);
32
+ if (!isDirExist) {
33
+ await fs2.promises.mkdir(cacheDir, { recursive: true });
34
+ }
35
+ }
36
+ return path.join(cacheDir, `${md5(key)}.json`);
37
+ }
38
+
39
+ async getJsonObject(key: string): Promise<any> {
40
+ const cacheFilePath = await this.getCacheFilePath(key, false);
41
+ try {
42
+ const str = await fs.readFile(cacheFilePath, 'utf8');
43
+ const data = parseJsonObject(str);
44
+ if (data && data.key === key && data.expireAt > Date.now()) {
45
+ return data.data;
46
+ }
47
+ return null;
48
+ } catch (error) {
49
+ return null;
50
+ }
51
+ }
52
+
53
+ /**
54
+ * 设置缓存对象
55
+ * @param key
56
+ * @param obj
57
+ * @param expireSecond 过期时间,单位秒
58
+ * @returns
59
+ */
60
+ async setJsonObject(key: string, obj: any, expireSecond?: number): Promise<any> {
61
+ if (!expireSecond) {
62
+ expireSecond = 60;
63
+ }
64
+ const str = JSON.stringify({
65
+ data: obj,
66
+ key,
67
+ expireAt: Date.now() + expireSecond * 1000,
68
+ });
69
+ const cacheFilePath = await this.getCacheFilePath(key);
70
+ try {
71
+ await fs.writeFile(cacheFilePath, str, 'utf8');
72
+ return true;
73
+ } catch (error) {
74
+ return false;
75
+ }
76
+ }
77
+
78
+ async removeItem(key: string): Promise<any> {
79
+ const cacheFilePath = await this.getCacheFilePath(key);
80
+ try {
81
+ await fs.unlink(cacheFilePath);
82
+ return true;
83
+ } catch (error) {
84
+ return false;
85
+ }
86
+ }
87
+ }
@@ -0,0 +1,74 @@
1
+ import { Context } from '@midwayjs/koa';
2
+ import { LRUCache } from 'lru-cache';
3
+ import { ICacheServiceBase } from '@/models/bizmodels';
4
+
5
+
6
+
7
+ const LRU_CACHE_MAP = new Map<string, LRUCache<string, any>>();
8
+
9
+
10
+ export class FatcmsBaseMemoryCache implements ICacheServiceBase {
11
+ protected ctx: Context;
12
+ protected cacheName: string;
13
+
14
+ constructor(ctx: Context, cacheName: string) {
15
+ this.ctx = ctx;
16
+ this.cacheName = cacheName;
17
+ }
18
+
19
+ private getLruCache(): LRUCache<string, any> {
20
+ if (!LRU_CACHE_MAP.has(this.cacheName)) {
21
+ const lruCache = new LRUCache<string, any>({
22
+ max: 500,
23
+ ttl: 1000 * 60 * 60 * 24,
24
+ ttlAutopurge: true,
25
+ });
26
+ LRU_CACHE_MAP.set(this.cacheName, lruCache);
27
+ }
28
+ return LRU_CACHE_MAP.get(this.cacheName);
29
+ }
30
+
31
+ async getJsonObject(key: string): Promise<any> {
32
+ try {
33
+ const lruCache = this.getLruCache();
34
+ const saveObj = lruCache.get(key);
35
+ if (saveObj && saveObj.key === key && saveObj.expireAt > Date.now()) {
36
+ return saveObj.data;
37
+ }
38
+ return null;
39
+ } catch (error) {
40
+ return null;
41
+ }
42
+ }
43
+
44
+ /**
45
+ * 设置缓存对象
46
+ * @param key
47
+ * @param obj
48
+ * @param expireSecond 过期时间,单位秒
49
+ * @returns
50
+ */
51
+ async setJsonObject(key: string, obj: any, expireSecond?: number): Promise<any> {
52
+ const lruCache = this.getLruCache();
53
+ if (!expireSecond) {
54
+ expireSecond = 60;
55
+ }
56
+ const saveObj = {
57
+ data: obj,
58
+ key,
59
+ expireAt: Date.now() + expireSecond * 1000,
60
+ };
61
+ lruCache.set(key, saveObj);
62
+ return true;
63
+ }
64
+
65
+ async removeItem(key: string): Promise<any> {
66
+ try {
67
+ const lruCache = this.getLruCache();
68
+ lruCache.delete(key);
69
+ return true;
70
+ } catch (error) {
71
+ return false;
72
+ }
73
+ }
74
+ }
@@ -0,0 +1,24 @@
1
+ import { ICacheServiceBase } from '@/models/bizmodels';
2
+ import { Context } from '@midwayjs/koa';
3
+
4
+ export class FatcmsBaseNoneCache implements ICacheServiceBase {
5
+ protected ctx: Context;
6
+ protected cacheName: string;
7
+
8
+ constructor(ctx: Context, cacheName: string) {
9
+ this.ctx = ctx;
10
+ this.cacheName = cacheName;
11
+ }
12
+
13
+ async getJsonObject(key: string): Promise<any> {
14
+ return null;
15
+ }
16
+
17
+ async setJsonObject(key: string, obj: any, expireSecond?: number): Promise<any> {
18
+ return true;
19
+ }
20
+
21
+ async removeItem(key: string): Promise<any> {
22
+ return true;
23
+ }
24
+ }
@@ -0,0 +1,48 @@
1
+ import { Context } from '@midwayjs/koa';
2
+ import { RedisService } from '@midwayjs/redis';
3
+ import { parseJsonObject } from '../../../libs/utils/functions';
4
+ import { ICacheServiceBase } from '@/models/bizmodels';
5
+
6
+
7
+ export class FatcmsBaseRedisCache implements ICacheServiceBase {
8
+ protected ctx: Context;
9
+ protected cacheName: string;
10
+ protected redisService: RedisService;
11
+
12
+ constructor(ctx: Context, cacheName: string, redisService: RedisService) {
13
+ this.ctx = ctx;
14
+ this.cacheName = cacheName;
15
+ this.redisService = redisService;
16
+ }
17
+
18
+ private getCacheKey(key: string): string {
19
+ return `${this.cacheName}${key}`;
20
+ }
21
+
22
+ async getJsonObject(key: string): Promise<any> {
23
+ const cacheKey = this.getCacheKey(key);
24
+ const str = await this.redisService.get(cacheKey);
25
+ return parseJsonObject(str);
26
+ }
27
+
28
+ /**
29
+ * 设置缓存对象
30
+ * @param key
31
+ * @param obj
32
+ * @param expireSecond 过期时间,单位秒
33
+ * @returns
34
+ */
35
+ async setJsonObject(key: string, obj: any, expireSecond?: number): Promise<any> {
36
+ const cacheKey = this.getCacheKey(key);
37
+ if (!expireSecond) {
38
+ expireSecond = 60;
39
+ }
40
+ const str = JSON.stringify(obj);
41
+ return this.redisService.set(cacheKey, str, 'EX', expireSecond);
42
+ }
43
+
44
+ async removeItem(key: string): Promise<any> {
45
+ const cacheKey = this.getCacheKey(key);
46
+ return await this.redisService.del(cacheKey);
47
+ }
48
+ }
@@ -17,6 +17,8 @@ import { ApiBaseService } from '../base/ApiBaseService';
17
17
  import { parseDatabaseName } from '@/libs/crud-pro/utils/DatabaseName';
18
18
  import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
19
19
  import { MixinUtils } from '@/libs/crud-pro/utils/MixinUtils';
20
+ import { CacheServiceFactory } from '../base/cache/CacheServiceFactory';
21
+ import { CacheLevelEnum, CacheNameEnum } from '@/models/SystemEntities';
20
22
 
21
23
 
22
24
  export const SPECIAL_SETTING_KEY = {
@@ -31,12 +33,16 @@ export interface ICrudStdActionParams {
31
33
  buttonSettingKey?: string;
32
34
  }
33
35
 
34
-
35
36
  export interface IRequestModelCrudProExt extends IRequestModel {
36
37
  filterValue: string | number;
37
38
  }
38
39
 
39
40
 
41
+
42
+ const DEFAULT_CACHE_LEVEL = CacheLevelEnum.MEMORY;
43
+ const DEFAULT_CACHE_SECOND = 60 * 5;
44
+
45
+
40
46
  function isNotEmptyStr(str: any): boolean {
41
47
  return typeof str === 'string' && str.trim().length > 0;
42
48
  }
@@ -85,6 +91,9 @@ export class CrudStdService extends ApiBaseService {
85
91
  @Inject()
86
92
  protected crudStdRelationService: CrudStdRelationService;
87
93
 
94
+ @Inject()
95
+ protected cacheServiceFactory: CacheServiceFactory;
96
+
88
97
  /**
89
98
  * 执行普通CRUD
90
99
  */
@@ -236,7 +245,7 @@ export class CrudStdService extends ApiBaseService {
236
245
  const hasOperationPerm = this.ctx.userSession.isAuthPass(update_auth_type as any, authConfig);
237
246
  appInfo.settingKeyActionCfg = { settingKey, hasOperationPerm };
238
247
 
239
- }
248
+ }
240
249
  // 列表查询或详情查询
241
250
  else if (settingKey === SPECIAL_SETTING_KEY.QUERY_LIST || settingKey === SPECIAL_SETTING_KEY.QUERY_ONE) {
242
251
  const authSettingValues = _.get(appSchema, 'authSetting.values');
@@ -301,17 +310,24 @@ export class CrudStdService extends ApiBaseService {
301
310
 
302
311
 
303
312
  /**
304
- * 查询APP信息
305
- * @param appCode
306
- * @private
307
- */
313
+ * 查询APP信息
314
+ * @param appCode
315
+ * @private
316
+ */
308
317
  public async getParsedCrudStdAppInfo(appCode: string): Promise<ICrudStdAppInfo> {
309
- const cacheKey = 'getParsedCrudStdAppInfo:' + appCode;
310
- let appInfo: ICrudStdAppInfo = this.getFromCtxCache(cacheKey) as any;
311
- if (!appInfo) {
312
- appInfo = await this.getParsedCrudStdAppInfoPrivate(appCode);
313
- this.setToCtxCache(cacheKey, appInfo);
314
- }
318
+ const { crudStdAppInfoCacheLevel, crudStdAppInfoCacheSecond } = this.ctx.workbenchInfoTools.getWorkbenchConfig();
319
+ const cacheSecond = crudStdAppInfoCacheSecond || DEFAULT_CACHE_SECOND;
320
+ const cacheLevel = crudStdAppInfoCacheLevel || DEFAULT_CACHE_LEVEL;
321
+
322
+ let appInfo = await this.cacheServiceFactory.getJsonObjectCache({
323
+ cacheLevel,
324
+ cacheKey: appCode,
325
+ cacheSecond,
326
+ cacheName: CacheNameEnum.GetParsedCrudStdAppInfo,
327
+ getter: async () => {
328
+ return await this.getParsedCrudStdAppInfoPrivate(appCode);
329
+ },
330
+ });
315
331
  return appInfo;
316
332
  }
317
333
 
@@ -19,6 +19,7 @@ const TMP_CTX_KEY2 = _.uniqueId('CurdMixByAccountService2');
19
19
 
20
20
  @Provide()
21
21
  export class CurdMixByAccountService implements IExecuteContextHandler {
22
+
22
23
  @Inject()
23
24
  protected ctx: Context;
24
25
 
@@ -8,14 +8,17 @@ import { SystemTables } from '@/models/SystemTables';
8
8
  import { KeysOfSimpleSQL } from '@/libs/crud-pro/models/keys';
9
9
  import { MultiKeyMap } from '@/libs/crud-pro/utils/MultiKeyMap';
10
10
  import { ColumnRelation } from '@/libs/crud-pro/interfaces';
11
- import { RedisCacheService } from '../base/RedisCacheService';
12
11
  import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
13
- import { RedisKeys } from '@/models/RedisKeys';
12
+ import { CacheServiceFactory } from '../base/cache/CacheServiceFactory';
13
+ import { CacheLevelEnum, CacheNameEnum } from '@/models/SystemEntities';
14
14
 
15
15
  const TMP_CTX_KEY = _.uniqueId('CurdMixByDictService');
16
-
17
16
  const dictMixUtils = new CrudMixUtils(RelatedType.dict);
18
17
 
18
+
19
+ const DEFAULT_CACHE_LEVEL = CacheLevelEnum.MEMORY;
20
+ const DEFAULT_CACHE_SECOND = 60 * 5;
21
+
19
22
  @Provide()
20
23
  export class CurdMixByDictService implements IExecuteContextHandler {
21
24
  @Inject()
@@ -25,15 +28,18 @@ export class CurdMixByDictService implements IExecuteContextHandler {
25
28
  private curdProService: CurdProService;
26
29
 
27
30
  @Inject()
28
- protected redisCacheService: RedisCacheService;
31
+ private cacheServiceFactory: CacheServiceFactory;
29
32
 
30
33
  private async getFromCache(code: string): Promise<any> {
31
- const cacheKey = `${RedisKeys.CURD_MIX_BY_DICT_PREFIX}${code}`;
32
- return this.redisCacheService.getJsonObject(cacheKey);
34
+ const { curdMixByCommonCacheLevel } = this.ctx.workbenchInfoTools.getWorkbenchConfig();
35
+ const cacheService = this.cacheServiceFactory.getCacheService(curdMixByCommonCacheLevel || DEFAULT_CACHE_LEVEL, CacheNameEnum.CurdMixByDict);
36
+ return cacheService.getJsonObject(code);
33
37
  }
38
+
34
39
  private async setToCache(code: string, value: any): Promise<any> {
35
- const cacheKey = `${RedisKeys.CURD_MIX_BY_DICT_PREFIX}${code}`;
36
- return this.redisCacheService.setJsonObject(cacheKey, value, 60 * 60);
40
+ const { curdMixByCommonCacheLevel, curdMixByCommonCacheSecond } = this.ctx.workbenchInfoTools.getWorkbenchConfig();
41
+ const cacheService = this.cacheServiceFactory.getCacheService(curdMixByCommonCacheLevel || DEFAULT_CACHE_LEVEL, CacheNameEnum.CurdMixByDict);
42
+ return cacheService.setJsonObject(code, value, curdMixByCommonCacheSecond || DEFAULT_CACHE_SECOND);
37
43
  }
38
44
 
39
45
  async handleExecuteContextPrepare(executeContext: HandleExecuteContextType): Promise<void> {
@@ -8,15 +8,21 @@ import { KeysOfSimpleSQL } from '@/libs/crud-pro/models/keys';
8
8
  import { ColumnRelation } from '@/libs/crud-pro/interfaces';
9
9
  import { BizException } from '@/models/devops';
10
10
  import { Exceptions } from '@/libs/crud-pro/exceptions';
11
- import { RedisCacheService } from '../base/RedisCacheService';
12
11
  import { parseDatabaseName } from '@/libs/crud-pro/utils/DatabaseName';
13
12
  import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
14
13
  import { IRequestCfgModel2 } from '@/models/bizmodels';
15
- import { RedisKeys } from '@/models/RedisKeys';
14
+ import { CacheLevelEnum, CacheNameEnum } from '@/models/SystemEntities';
15
+ import { CacheServiceFactory } from '../base/cache/CacheServiceFactory';
16
+ import * as md5 from 'md5';
16
17
 
17
18
  const linkToCustomMixUtils = new CrudMixUtils(RelatedType.linkToCustom);
18
19
  const TMP_CTX_KEY = _.uniqueId('CurdMixByLinkToCustomService');
19
20
 
21
+
22
+ const DEFAULT_CACHE_LEVEL = CacheLevelEnum.MEMORY;
23
+ const DEFAULT_CACHE_SECOND = 60 * 5;
24
+
25
+
20
26
  function toMapObject(rows: any[], fieldsArr: string[]): Map<string, any> {
21
27
  const valueKey = fieldsArr[0];
22
28
  const labelKey = fieldsArr[1];
@@ -52,7 +58,8 @@ export class CurdMixByLinkToCustomService implements IExecuteContextHandler {
52
58
  protected curdProService: CurdProService;
53
59
 
54
60
  @Inject()
55
- protected redisCacheService: RedisCacheService;
61
+ private cacheServiceFactory: CacheServiceFactory;
62
+
56
63
 
57
64
  async handleExecuteContextPrepare(executeContext: HandleExecuteContextType) {
58
65
  const relations = linkToCustomMixUtils.pickColumnRelations(executeContext);
@@ -91,17 +98,19 @@ export class CurdMixByLinkToCustomService implements IExecuteContextHandler {
91
98
  }
92
99
 
93
100
  public async queryInfoByLinkToCustom(code: string): Promise<Map<string, any>> {
94
- const cacheKey = RedisKeys.LINK_TO_CUSTOM_PREFIX + code;
95
- let values;
96
- const expireSecond = 2 * 60; // 2分钟
97
- const enumMap = await this.redisCacheService.getJsonObject(cacheKey);
98
- if (!enumMap || Object.keys(enumMap).length === 0) {
99
- values = await this.queryInfoByLinkToCustomImpl(code);
100
- await this.redisCacheService.setJsonObject(cacheKey, values, expireSecond);
101
- }
102
- return values;
101
+ const { curdMixByCommonCacheLevel, curdMixByCommonCacheSecond } = this.ctx.workbenchInfoTools.getWorkbenchConfig();
102
+ return this.cacheServiceFactory.getJsonObjectCache({
103
+ cacheLevel: curdMixByCommonCacheLevel || DEFAULT_CACHE_LEVEL,
104
+ cacheKey: md5(code),
105
+ cacheSecond: curdMixByCommonCacheSecond || DEFAULT_CACHE_SECOND,
106
+ cacheName: CacheNameEnum.CurdMixByLinkToCustom,
107
+ getter: async () => {
108
+ return await this.queryInfoByLinkToCustomImpl(code);
109
+ },
110
+ });
103
111
  }
104
112
 
113
+
105
114
  /**
106
115
  * code形如:mysql_________fatcms~~~sys_perm_role~~~role_code,role_name,xxx_name
107
116
  * code形如:fatcms~~~sys_perm_role~~~role_code,role_name,xxx_name
@@ -10,11 +10,19 @@ import { MultiKeyMap } from '@/libs/crud-pro/utils/MultiKeyMap';
10
10
  import { ColumnRelation } from '@/libs/crud-pro/interfaces';
11
11
  import { parseConfigContentToEnumInfo } from '@/libs/utils/parseConfig';
12
12
  import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
13
+ import { CacheServiceFactory } from '../base/cache/CacheServiceFactory';
14
+ import { CacheLevelEnum, CacheNameEnum } from '@/models/SystemEntities';
13
15
 
14
16
  const dictMixUtils = new CrudMixUtils(RelatedType.sysCfgEnum);
15
17
 
16
18
  const TMP_CTX_KEY = _.uniqueId('CurdMixBySysConfigService');
17
19
 
20
+ const DEFAULT_CACHE_LEVEL = CacheLevelEnum.MEMORY;
21
+ const DEFAULT_CACHE_SECOND = 60 * 5;
22
+
23
+
24
+
25
+
18
26
  @Provide()
19
27
  export class CurdMixBySysConfigService implements IExecuteContextHandler {
20
28
  @Inject()
@@ -22,18 +30,43 @@ export class CurdMixBySysConfigService implements IExecuteContextHandler {
22
30
 
23
31
  @Inject()
24
32
  protected curdProService: CurdProService;
33
+ @Inject()
34
+ private cacheServiceFactory: CacheServiceFactory;
25
35
 
26
- async handleExecuteContextPrepare(executeContext: HandleExecuteContextType) {
27
- const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
28
36
 
29
- const codes = dictMixUtils.pickColumnRelationCodes(executeContext);
30
- if (!codes || codes.length === 0) {
31
- return;
37
+ private async getFromCache(code: string): Promise<any> {
38
+ const { curdMixByCommonCacheLevel } = this.ctx.workbenchInfoTools.getWorkbenchConfig();
39
+ const cacheService = this.cacheServiceFactory.getCacheService(curdMixByCommonCacheLevel || DEFAULT_CACHE_LEVEL, CacheNameEnum.CurdMixBySysConfig);
40
+ return cacheService.getJsonObject(code);
41
+ }
42
+
43
+ private async setToCache(code: string, value: any): Promise<any> {
44
+ const { curdMixByCommonCacheLevel, curdMixByCommonCacheSecond } = this.ctx.workbenchInfoTools.getWorkbenchConfig();
45
+ const cacheService = this.cacheServiceFactory.getCacheService(curdMixByCommonCacheLevel || DEFAULT_CACHE_LEVEL, CacheNameEnum.CurdMixBySysConfig);
46
+ return cacheService.setJsonObject(code, value, curdMixByCommonCacheSecond || DEFAULT_CACHE_SECOND);
47
+ }
48
+
49
+
50
+ private async queryConfigRowsUseCache(codes: string[]): Promise<any[]> {
51
+
52
+ const resultRowMap = {} as Record<string, any>;
53
+ for (let i = 0; i < codes.length; i++) {
54
+ const code = codes[i];
55
+ const row = await this.getFromCache(code);
56
+ if (row) {
57
+ resultRowMap[code] = row;
58
+ }
32
59
  }
33
60
 
61
+ const notCachedCodes = codes.filter((code) => !resultRowMap[code]);
62
+ if (notCachedCodes.length === 0) {
63
+ return Object.values(resultRowMap);
64
+ }
65
+
66
+ const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
34
67
  const res1 = await this.curdProService.executeCrudByCfg(
35
68
  {
36
- condition: { config_code: { $in: codes } },
69
+ condition: { config_code: { $in: notCachedCodes } },
37
70
  },
38
71
  {
39
72
  sqlTable: SystemTables.sys_configs,
@@ -43,16 +76,32 @@ export class CurdMixBySysConfigService implements IExecuteContextHandler {
43
76
  }
44
77
  );
45
78
  const rows = res1.getResRows();
46
- const multiKeyMap = new MultiKeyMap();
47
-
48
- if (Array.isArray(rows)) {
79
+ if (Array.isArray(rows) && rows.length > 0) {
49
80
  for (let i = 0; i < rows.length; i++) {
50
81
  const row = rows[i];
51
82
  const { config_code } = row;
52
- const config_content_rows = parseConfigContentToEnumInfo(row); // 数组元素必须有value字段
53
- multiKeyMap.addRowsValue1(config_content_rows, config_code, 'value');
83
+ await this.setToCache(config_code, row);
84
+ resultRowMap[config_code] = row;
54
85
  }
55
86
  }
87
+ return Object.values(resultRowMap)
88
+ }
89
+
90
+ async handleExecuteContextPrepare(executeContext: HandleExecuteContextType) {
91
+ const codes = dictMixUtils.pickColumnRelationCodes(executeContext);
92
+ if (!codes || codes.length === 0) {
93
+ return;
94
+ }
95
+
96
+ const multiKeyMap = new MultiKeyMap();
97
+ const rows = await this.queryConfigRowsUseCache(codes);
98
+
99
+ for (let i = 0; i < rows.length; i++) {
100
+ const row = rows[i];
101
+ const { config_code } = row;
102
+ const config_content_rows = parseConfigContentToEnumInfo(row); // 数组元素必须有value字段
103
+ multiKeyMap.addRowsValue1(config_content_rows, config_code, 'value');
104
+ }
56
105
 
57
106
  executeContext[TMP_CTX_KEY] = multiKeyMap;
58
107
  }