midway-fatcms 0.0.16 → 0.0.17

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 (119) hide show
  1. package/dist/config/config.default.js +5 -3
  2. package/dist/configuration.d.ts +0 -3
  3. package/dist/configuration.js +0 -19
  4. package/dist/controller/gateway/ProxyApiGatewayController.d.ts +0 -2
  5. package/dist/controller/gateway/ProxyApiGatewayController.js +0 -18
  6. package/dist/controller/helpers.controller.d.ts +4 -5
  7. package/dist/controller/helpers.controller.js +56 -14
  8. package/dist/controller/manage/AccountRoleManageApi.js +3 -5
  9. package/dist/controller/manage/AnyApiMangeApi.js +4 -3
  10. package/dist/controller/manage/AppMangeApi.js +3 -4
  11. package/dist/controller/manage/AppPageMangeApi.js +4 -3
  12. package/dist/controller/manage/CrudMethodsMangeApi.js +3 -4
  13. package/dist/controller/manage/DataDictManageApi.js +6 -7
  14. package/dist/controller/manage/DeployManageApi.js +4 -5
  15. package/dist/controller/manage/FlowConfigManageApi.js +3 -5
  16. package/dist/controller/manage/MenuManageApi.js +3 -4
  17. package/dist/controller/manage/ProxyApiMangeApi.js +3 -4
  18. package/dist/controller/manage/SysConfigMangeApi.js +3 -3
  19. package/dist/controller/manage/SystemInfoManageApi.js +3 -1
  20. package/dist/controller/manage/WorkbenchMangeApi.js +3 -4
  21. package/dist/index.d.ts +3 -0
  22. package/dist/index.js +3 -0
  23. package/dist/libs/crud-pro/models/TransactionSqlServer.js +27 -4
  24. package/dist/libs/crud-pro/services/CrudProCachedCfgService.d.ts +5 -8
  25. package/dist/libs/crud-pro/services/CrudProCachedCfgService.js +25 -42
  26. package/dist/libs/crud-pro/utils/CrudMonitor.d.ts +0 -9
  27. package/dist/libs/crud-pro/utils/CrudMonitor.js +0 -21
  28. package/dist/libs/crud-pro/utils/pool/SqlServerUtils.js +26 -0
  29. package/dist/middleware/cacherefresh.middleware.d.ts +3 -46
  30. package/dist/middleware/cacherefresh.middleware.js +9 -67
  31. package/dist/models/CacheRefreshCategory.d.ts +1 -18
  32. package/dist/models/CacheRefreshCategory.js +0 -17
  33. package/dist/models/SystemEntities.d.ts +2 -0
  34. package/dist/models/bizmodels.d.ts +0 -8
  35. package/dist/models/bizmodels.js +0 -8
  36. package/dist/schedule/scheduleNames.d.ts +2 -0
  37. package/dist/schedule/scheduleNames.js +3 -1
  38. package/dist/service/SysAppService.d.ts +2 -5
  39. package/dist/service/SysAppService.js +15 -69
  40. package/dist/service/SysConfigService.d.ts +1 -6
  41. package/dist/service/SysConfigService.js +7 -46
  42. package/dist/service/SysDictDataService.d.ts +2 -5
  43. package/dist/service/SysDictDataService.js +13 -59
  44. package/dist/service/SysMenuService.d.ts +1 -4
  45. package/dist/service/SysMenuService.js +6 -42
  46. package/dist/service/WorkbenchService.d.ts +1 -5
  47. package/dist/service/WorkbenchService.js +10 -61
  48. package/dist/service/anyapi/AnyApiService.d.ts +1 -1
  49. package/dist/service/anyapi/AnyApiService.js +6 -16
  50. package/dist/service/base/bizmemcache/BizMemCacheService.d.ts +16 -0
  51. package/dist/service/base/bizmemcache/BizMemCacheService.js +239 -0
  52. package/dist/service/base/bizmemcache/BizMemCacheStore.d.ts +34 -0
  53. package/dist/service/base/bizmemcache/BizMemCacheStore.js +118 -0
  54. package/dist/service/base/bizmemcache/BizMemCacheTypes.d.ts +60 -0
  55. package/dist/service/base/bizmemcache/BizMemCacheTypes.js +18 -0
  56. package/dist/service/base/cache-refresh/CacheRefreshHub.d.ts +0 -22
  57. package/dist/service/base/cache-refresh/CacheRefreshHub.js +0 -22
  58. package/dist/service/base/cache-refresh/CacheRefreshHubScheduleService.d.ts +0 -6
  59. package/dist/service/base/cache-refresh/CacheRefreshHubScheduleService.js +0 -6
  60. package/dist/service/curd/CurdMixByDictService.d.ts +0 -4
  61. package/dist/service/curd/CurdMixByDictService.js +9 -55
  62. package/dist/service/curd/CurdMixBySysConfigService.d.ts +0 -5
  63. package/dist/service/curd/CurdMixBySysConfigService.js +9 -54
  64. package/dist/service/curd/CurdMixByWorkbenchService.d.ts +0 -1
  65. package/dist/service/curd/CurdMixByWorkbenchService.js +9 -36
  66. package/dist/service/curd/CurdProService.d.ts +0 -1
  67. package/dist/service/curd/CurdProService.js +0 -4
  68. package/dist/service/proxyapi/ProxyApiLoadService.d.ts +3 -8
  69. package/dist/service/proxyapi/ProxyApiLoadService.js +40 -68
  70. package/dist/service/proxyapi/ProxyApiService.d.ts +2 -0
  71. package/dist/service/proxyapi/ProxyApiService.js +32 -9
  72. package/dist/service/proxyapi/RouteTrie.d.ts +1 -7
  73. package/dist/service/proxyapi/RouteTrie.js +1 -17
  74. package/package.json +1 -1
  75. package/src/config/config.default.ts +4 -2
  76. package/src/configuration.ts +0 -23
  77. package/src/controller/gateway/ProxyApiGatewayController.ts +1 -9
  78. package/src/controller/helpers.controller.ts +66 -15
  79. package/src/controller/manage/AccountRoleManageApi.ts +3 -5
  80. package/src/controller/manage/AnyApiMangeApi.ts +4 -3
  81. package/src/controller/manage/AppMangeApi.ts +4 -5
  82. package/src/controller/manage/AppPageMangeApi.ts +4 -3
  83. package/src/controller/manage/CrudMethodsMangeApi.ts +4 -5
  84. package/src/controller/manage/DataDictManageApi.ts +7 -8
  85. package/src/controller/manage/DeployManageApi.ts +2 -3
  86. package/src/controller/manage/FlowConfigManageApi.ts +3 -5
  87. package/src/controller/manage/MenuManageApi.ts +4 -5
  88. package/src/controller/manage/ProxyApiMangeApi.ts +4 -5
  89. package/src/controller/manage/SysConfigMangeApi.ts +5 -5
  90. package/src/controller/manage/SystemInfoManageApi.ts +3 -1
  91. package/src/controller/manage/WorkbenchMangeApi.ts +4 -5
  92. package/src/index.ts +3 -0
  93. package/src/libs/crud-pro/models/TransactionSqlServer.ts +26 -4
  94. package/src/libs/crud-pro/services/CrudProCachedCfgService.ts +25 -43
  95. package/src/libs/crud-pro/utils/CrudMonitor.ts +0 -23
  96. package/src/libs/crud-pro/utils/pool/SqlServerUtils.ts +31 -0
  97. package/src/middleware/cacherefresh.middleware.ts +10 -71
  98. package/src/models/CacheRefreshCategory.ts +0 -17
  99. package/src/models/SystemEntities.ts +2 -0
  100. package/src/models/bizmodels.ts +0 -8
  101. package/src/schedule/scheduleNames.ts +5 -3
  102. package/src/service/SysAppService.ts +13 -66
  103. package/src/service/SysConfigService.ts +6 -39
  104. package/src/service/SysDictDataService.ts +11 -56
  105. package/src/service/SysMenuService.ts +4 -37
  106. package/src/service/WorkbenchService.ts +10 -68
  107. package/src/service/anyapi/AnyApiService.ts +5 -17
  108. package/src/service/base/bizmemcache/BizMemCacheService.ts +288 -0
  109. package/src/service/base/bizmemcache/BizMemCacheStore.ts +158 -0
  110. package/src/service/base/bizmemcache/BizMemCacheTypes.ts +76 -0
  111. package/src/service/base/cache-refresh/CacheRefreshHub.ts +0 -22
  112. package/src/service/base/cache-refresh/CacheRefreshHubScheduleService.ts +0 -6
  113. package/src/service/curd/CurdMixByDictService.ts +10 -61
  114. package/src/service/curd/CurdMixBySysConfigService.ts +9 -62
  115. package/src/service/curd/CurdMixByWorkbenchService.ts +7 -32
  116. package/src/service/curd/CurdProService.ts +0 -6
  117. package/src/service/proxyapi/ProxyApiLoadService.ts +39 -77
  118. package/src/service/proxyapi/ProxyApiService.ts +45 -9
  119. package/src/service/proxyapi/RouteTrie.ts +1 -18
@@ -1,57 +1,24 @@
1
- import { Inject, Provide } from '@midwayjs/core';
2
- import { Context } from '@midwayjs/koa';
3
- import { SystemTables } from '@/models/SystemTables';
1
+ import { Provide } from '@midwayjs/core';
4
2
  import { BaseService } from './base/BaseService';
5
- import { CurdProService } from './curd/CurdProService';
6
- import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
7
- import { CacheNameEnum } from "@/models/bizmodels";
8
- import { CacheServiceFactory } from "@/service/base/cache/CacheServiceFactory";
3
+ import { bizMemCacheStore } from '@/service/base/bizmemcache/BizMemCacheStore';
9
4
 
10
5
  @Provide()
11
6
  export class SysConfigService extends BaseService {
12
- @Inject()
13
- ctx: Context;
14
-
15
- @Inject()
16
- private curdProService: CurdProService;
17
-
18
- @Inject()
19
- private cacheServiceFactory: CacheServiceFactory;
20
-
21
- private get sysConfigsDao(){
22
- const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
23
- return this.curdProService.getQuickCrud({
24
- sqlDatabase: SystemDbName,
25
- sqlDbType: SystemDbType,
26
- sqlTable: SystemTables.sys_configs,
27
- })
28
- }
29
7
 
30
8
  async getSysConfigOne(config_code: string): Promise<Record<string, any>> {
31
9
  if (!config_code || typeof config_code !== 'string') {
32
10
  throw new Error('[getSysAppOne] config_code required');
33
11
  }
34
- return await this.cacheServiceFactory.getJsonObjectCache({
35
- cacheKey: config_code,
36
- cacheName: CacheNameEnum.GetSysConfigOne,
37
- getter: async (ctx) => {
38
- const service: SysConfigService = await ctx.requestContext.getAsync(SysConfigService);
39
- const res = await service.sysConfigsDao.findOne({
40
- condition: {config_code, deleted_at: 0}
41
- });
42
- return res.row;
43
- },
44
- });
12
+ const sysConfigCodeMap = bizMemCacheStore.getSysConfig()?.sysConfigCodeMap || {};
13
+ return sysConfigCodeMap[config_code];
45
14
  }
46
15
 
16
+
47
17
  async removeSysConfigOneCache(config_code: string) {
48
18
  if (!config_code || typeof config_code !== 'string') {
49
19
  throw new Error('[removeSysConfigOneCache] config_code required');
50
20
  }
51
21
 
52
- return await this.cacheServiceFactory.removeItemCache({
53
- cacheKey: config_code,
54
- cacheName: CacheNameEnum.GetSysConfigOne,
55
- });
22
+ await bizMemCacheStore.markDirty('sysConfgCacheObj', this.redisService);
56
23
  }
57
24
  }
@@ -1,74 +1,29 @@
1
- import { Inject, Provide } from '@midwayjs/core';
2
- import { SystemTables } from '@/models/SystemTables';
1
+ import { Provide } from '@midwayjs/core';
3
2
  import { BaseService } from './base/BaseService';
4
- import { CurdProService } from './curd/CurdProService';
5
- import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
6
- import { CacheServiceFactory } from "@/service/base/cache/CacheServiceFactory";
7
- import { CacheNameEnum } from "@/models/bizmodels";
3
+ import { bizMemCacheStore } from '@/service/base/bizmemcache/BizMemCacheStore';
8
4
 
9
5
  @Provide()
10
6
  export class SysDictDataService extends BaseService {
11
7
 
12
- @Inject()
13
- private curdProService: CurdProService;
14
-
15
- @Inject()
16
- private cacheServiceFactory: CacheServiceFactory;
17
-
18
- private get sys_data_dict_item() {
19
- const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
20
- return this.curdProService.getQuickCrud({
21
- sqlDatabase: SystemDbName,
22
- sqlDbType: SystemDbType,
23
- sqlTable: SystemTables.sys_data_dict_item,
24
- })
25
- }
26
-
27
- // private get sys_data_dict(){
28
- // const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
29
- // return this.curdProService.getQuickCrud({
30
- // sqlDatabase: SystemDbName,
31
- // sqlDbType: SystemDbType,
32
- // sqlTable: SystemTables.sys_data_dict,
33
- // })
34
- // }
35
-
36
-
37
8
  public async getDataDictItemsByDictCode(dict_code: string): Promise<any[]> {
38
9
  if (!dict_code || typeof dict_code !== 'string') {
39
- throw new Error('[getDataDictItemsByDictCode] page_path required');
10
+ throw new Error('[getDataDictItemsByDictCode] dict_code required');
40
11
  }
41
- return await this.cacheServiceFactory.getJsonObjectCache({
42
- cacheKey: dict_code,
43
- cacheName: CacheNameEnum.GetDataDictItemsByDictCode,
44
- getter: async (ctx) => {
45
- const service: SysDictDataService = await ctx.requestContext.getAsync(SysDictDataService);
46
- const res = await service.sys_data_dict_item.findList({
47
- condition: { dict_code, status: 1, deleted_at: 0 }
48
- });
49
- return res.rows;
50
- },
51
- });
12
+ const dictCodeMap = bizMemCacheStore.getDataDictItem()?.dictCodeMap || {};
13
+ return dictCodeMap[dict_code] || [];
52
14
  }
53
15
 
54
16
  public async removeCacheByDictItemId(dict_item_id: string) {
55
- const res = await this.sys_data_dict_item.findOne({ condition: { id: dict_item_id } });
56
- const dictItemObj = res.row;
57
- return this.removeCacheByDictCode(dictItemObj['dict_code']);
17
+ if (!dict_item_id) {
18
+ throw new Error('[removeCacheByDictItemId] dict_item_id required');
19
+ }
20
+ await bizMemCacheStore.markDirty('dataDictItemCacheObj', this.redisService);
58
21
  }
59
22
 
60
- // public async removeCacheByDictId(dict_id: string) {
61
- // const dictObj = await this.sys_data_dict.getUniqueOne({condition: {id: dict_id}});
62
- // return this.removeCacheByDictCode(dictObj['dict_code']);
63
- // }
64
-
65
23
  public async removeCacheByDictCode(dict_code: string) {
66
24
  if (!dict_code || typeof dict_code !== 'string') {
67
- throw new Error('[removeSysAppOneCache] app_code required');
25
+ throw new Error('[removeCacheByDictCode] dict_code required');
68
26
  }
69
- return await this.cacheServiceFactory.removeItemCache({
70
- cacheKey: dict_code,
71
- cacheName: CacheNameEnum.GetDataDictItemsByDictCode
72
- });
27
+ await bizMemCacheStore.markDirty('dataDictItemCacheObj', this.redisService);
73
28
  }
74
29
  }
@@ -1,10 +1,6 @@
1
- import { Inject, Provide } from '@midwayjs/core';
2
- import { SystemTables } from '@/models/SystemTables';
1
+ import { Provide } from '@midwayjs/core';
3
2
  import { BaseService } from './base/BaseService';
4
- import { CurdProService } from './curd/CurdProService';
5
- import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
6
- import { CacheServiceFactory } from "@/service/base/cache/CacheServiceFactory";
7
- import { CacheNameEnum } from "@/models/bizmodels";
3
+ import { bizMemCacheStore } from '@/service/base/bizmemcache/BizMemCacheStore';
8
4
 
9
5
  /**
10
6
  * 菜单表接口定义
@@ -49,45 +45,16 @@ interface SysMenus {
49
45
  @Provide()
50
46
  export class SysMenuService extends BaseService {
51
47
 
52
- @Inject()
53
- private curdProService: CurdProService;
54
-
55
- @Inject()
56
- private cacheServiceFactory: CacheServiceFactory;
57
-
58
- private get sys_menus_table() {
59
- const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
60
- return this.curdProService.getQuickCrud({
61
- sqlDatabase: SystemDbName,
62
- sqlDbType: SystemDbType,
63
- sqlTable: SystemTables.sys_menus,
64
- })
65
- }
66
-
67
-
68
48
  /**
69
49
  * 获取所有菜单列表
70
50
  * @returns
71
51
  */
72
52
  async getCachedMenusList(): Promise<SysMenus[]> {
73
- return await this.cacheServiceFactory.getJsonObjectCache({
74
- cacheKey: CacheNameEnum.GetSysMenusList,
75
- cacheName: CacheNameEnum.GetSysMenusList,
76
- getter: async (ctx) => {
77
- const service: SysMenuService = await ctx.requestContext.getAsync(SysMenuService);
78
- const res = await service.sys_menus_table.findList({
79
- condition: { deleted_at: 0 }
80
- });
81
- return res.rows;
82
- },
83
- });
53
+ return bizMemCacheStore.getSysMenu()?.sysMenuList || [];
84
54
  }
85
55
 
86
56
 
87
57
  async removeSysMenuCache() {
88
- return await this.cacheServiceFactory.removeItemCache({
89
- cacheKey: CacheNameEnum.GetSysMenusList,
90
- cacheName: CacheNameEnum.GetSysMenusList,
91
- });
58
+ await bizMemCacheStore.markDirty('sysMenuCacheObj', this.redisService);
92
59
  }
93
60
  }
@@ -1,89 +1,31 @@
1
1
  import { Config, Inject, Provide } from '@midwayjs/core';
2
2
  import { Context } from '@midwayjs/koa';
3
- import { KeysOfSimpleSQL } from '@/libs/crud-pro/models/keys';
4
- import { SystemTables } from '@/models/SystemTables';
5
3
  import { BaseService } from './base/BaseService';
6
- import { CurdProService } from './curd/CurdProService';
7
4
  import { CommonException } from '@/libs/crud-pro/exceptions';
8
5
  import { parseJsonObject } from '@/libs/utils/functions';
9
6
  import { IWorkbenchConfig, IWorkbenchEntity } from '@/models/SystemEntities';
10
- import { IScheduleService } from '@/interface';
11
7
  import { getDebugWorkbenchCode } from '@/libs/utils/fatcms-request';
12
- import { MixinUtils } from '@/libs/crud-pro/utils/MixinUtils';
13
- import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
14
8
  import { HEADER_KEY_RUN_BY_ASYNC_TASK_WORKBENCH_CODE } from '@/models/AsyncTaskModel';
15
9
  import { formatHost } from '@/libs/utils/format-url';
16
- import { CacheServiceFactory } from "@/service/base/cache/CacheServiceFactory";
17
- import { CacheNameEnum } from "@/models/bizmodels";
10
+ import { bizMemCacheStore } from '@/service/base/bizmemcache/BizMemCacheStore';
11
+ import { IWorkbenchCachedObj } from '@/service/base/bizmemcache/BizMemCacheTypes';
18
12
 
19
13
 
20
- interface IWorkbenchCachedObj {
21
- workbenchList: IWorkbenchEntity[];
22
- workbenchDomainMap: Record<string, IWorkbenchEntity>; // key是domain
23
- }
24
-
25
14
  @Provide()
26
- export class WorkbenchService extends BaseService implements IScheduleService {
15
+ export class WorkbenchService extends BaseService {
27
16
  @Inject()
28
17
  ctx: Context;
29
18
 
30
- @Inject()
31
- private curdProService: CurdProService;
32
-
33
- @Inject()
34
- private cacheServiceFactory: CacheServiceFactory;
35
-
36
19
  @Config('fatcmsTargetWorkbenchCode')
37
20
  private fatcmsTargetWorkbenchCode: string;
38
21
 
39
22
 
40
23
  public async clearCache() {
41
-
42
- await this.cacheServiceFactory.removeItemCache({
43
- cacheKey: CacheNameEnum.CurdMixByWorkbench,
44
- cacheName: CacheNameEnum.CurdMixByWorkbench,
45
- });
46
-
47
- await this.cacheServiceFactory.removeItemCache({
48
- cacheKey: CacheNameEnum.GetWorkbenchCachedObj,
49
- cacheName: CacheNameEnum.GetWorkbenchCachedObj,
50
- });
51
-
52
- await this.getWorkbenchCachedObj(true);
53
- }
54
-
55
- public async runBySchedule() {
56
- await this.getWorkbenchCachedObj(true);
24
+ await bizMemCacheStore.markDirty('workbenchCachedObj', this.redisService);
57
25
  }
58
26
 
59
- private async getWorkbenchCachedObj(isForceRefresh?: boolean): Promise<IWorkbenchCachedObj> {
60
- return this.cacheServiceFactory.getJsonObjectCache({
61
- cacheKey: CacheNameEnum.GetWorkbenchCachedObj,
62
- cacheName: CacheNameEnum.GetWorkbenchCachedObj,
63
- refreshCache: isForceRefresh,
64
- getter: async (ctx) => {
65
- const service: WorkbenchService = await ctx.requestContext.getAsync(WorkbenchService);
66
- const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
67
- const reqJson = { condition: { deleted_at: 0 }, pageSize: 1000, limit: 1000 };
68
- const res = await service.curdProService.executeCrudByCfg(reqJson, {
69
- sqlTable: SystemTables.sys_workbench,
70
- sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
71
- sqlDatabase: SystemDbName,
72
- sqlDbType: SystemDbType,
73
- });
74
- const listData = res.getResRows() as IWorkbenchEntity[];
75
-
76
- const domainMap1 = MixinUtils.toMap(listData, (e: any) => e.workbench_domain); // 主域名。其他两个域名。
77
- const domainMap2 = MixinUtils.toMap(listData, (e: any) => e.workbench_domain2);
78
- const domainMap3 = MixinUtils.toMap(listData, (e: any) => e.workbench_domain3);
79
- const domainMap = { ...domainMap1, ...domainMap2, ...domainMap3 };
80
- const cacheObj: IWorkbenchCachedObj = {
81
- workbenchList: listData,
82
- workbenchDomainMap: domainMap
83
- }
84
- return cacheObj;
85
- },
86
- });
27
+ private getWorkbenchCachedObj(): IWorkbenchCachedObj {
28
+ return bizMemCacheStore.getWorkbench();
87
29
  }
88
30
 
89
31
  /**
@@ -91,8 +33,8 @@ export class WorkbenchService extends BaseService implements IScheduleService {
91
33
  * @returns
92
34
  */
93
35
  public async getAllWorkbenchInfoList(): Promise<IWorkbenchEntity[]> {
94
- const cachedObj = await this.getWorkbenchCachedObj();
95
- return cachedObj.workbenchList;
36
+ const cachedObj = this.getWorkbenchCachedObj();
37
+ return cachedObj?.workbenchList || [];
96
38
  }
97
39
 
98
40
  /**
@@ -100,8 +42,8 @@ export class WorkbenchService extends BaseService implements IScheduleService {
100
42
  * @returns
101
43
  */
102
44
  public async getAllWorkbenchInfoMap(): Promise<Record<string, IWorkbenchEntity>> {
103
- const cachedObj = await this.getWorkbenchCachedObj();
104
- return cachedObj.workbenchDomainMap;
45
+ const cachedObj = this.getWorkbenchCachedObj();
46
+ return cachedObj?.workbenchDomainMap || {};
105
47
  }
106
48
 
107
49
  /**
@@ -2,7 +2,6 @@ import { Inject, Provide } from '@midwayjs/core';
2
2
  import { Context } from '@midwayjs/koa';
3
3
  import { KeysOfSimpleSQL } from '@/libs/crud-pro/models/keys';
4
4
  import { SystemTables } from '@/models/SystemTables';
5
- import { LRUCache } from 'lru-cache';
6
5
  import { BizException } from '@/models/devops';
7
6
  import { CurdMixService } from '../curd/CurdMixService';
8
7
  import { ISysAnyApiEntity } from '@/models/SystemEntities';
@@ -14,12 +13,7 @@ import { WorkbenchService } from '../WorkbenchService';
14
13
  import { validateByCfgString } from '@/libs/crud-pro/utils/ValidateUtils';
15
14
  import { API_BASE_TYPE, ApiBaseService } from '../base/ApiBaseService';
16
15
  import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
17
-
18
- const lruCache = new LRUCache<string, any>({
19
- max: 500,
20
- ttl: 1000 * 60,
21
- ttlAutopurge: true,
22
- });
16
+ import { bizMemCacheStore } from '@/service/base/bizmemcache/BizMemCacheStore';
23
17
 
24
18
  @Provide()
25
19
  export class AnyApiService extends ApiBaseService {
@@ -65,21 +59,15 @@ export class AnyApiService extends ApiBaseService {
65
59
  }
66
60
 
67
61
  private async getAnyApiMethod(methodCode: string): Promise<ISysAnyApiEntity> {
68
- // 开发环境,不使用缓存
69
62
  if (this.isEnableDebug()) {
70
- return this._getAnyApiMethod(methodCode);
63
+ return this.loadAnyApiMethodFromDb(methodCode);
71
64
  }
72
65
 
73
- // 线上环境可以使用缓存
74
- let anyApi = lruCache.get(methodCode);
75
- if (!anyApi) {
76
- anyApi = await this._getAnyApiMethod(methodCode);
77
- lruCache.set(methodCode, anyApi);
78
- }
79
- return anyApi;
66
+ const sysAnyApiMethodMap = bizMemCacheStore.getSysAnyApi()?.sysAnyApiMethodMap || {};
67
+ return sysAnyApiMethodMap[methodCode] || null;
80
68
  }
81
69
 
82
- private async _getAnyApiMethod(methodCode: string): Promise<ISysAnyApiEntity> {
70
+ private async loadAnyApiMethodFromDb(methodCode: string): Promise<ISysAnyApiEntity> {
83
71
  const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
84
72
 
85
73
  const res = await this.curdMixService.executeCrudByCfg(
@@ -0,0 +1,288 @@
1
+ import * as _ from 'lodash';
2
+ import { Inject, Provide } from '@midwayjs/core';
3
+ import { IScheduleService } from '@/interface';
4
+ import { SystemTables } from '@/models/SystemTables';
5
+ import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
6
+ import { BaseService } from "@/service/base/BaseService";
7
+ import { CurdProService } from '../../curd/CurdProService';
8
+ import { KeysOfSimpleSQL } from '@/libs/crud-pro/models/keys';
9
+ import { MixinUtils } from '@/libs/crud-pro/utils/MixinUtils';
10
+ import { IWorkbenchEntity } from '@/models/SystemEntities';
11
+ import { bizMemCacheStore } from './BizMemCacheStore';
12
+ import { BizMemCacheKey, buildSysAppPageCacheKey, ICrudMethodCacheObj, IDataDictItemCacheObj, IWorkbenchCachedObj, ISysConfgCacheObj, ISysMenuCacheObj, ISysAppCacheObj, ISysAppPageCacheObj, ISysAnyApiCacheObj, ISysProxyApiCacheObj } from './BizMemCacheTypes';
13
+ import { parseJsonObject } from '@/libs/utils/functions';
14
+ import { parseCrudMethodInfo, setMethodCache } from '@/libs/crud-pro/services/CrudProCachedCfgService';
15
+
16
+
17
+
18
+
19
+ @Provide()
20
+ export class BizMemCacheService extends BaseService implements IScheduleService {
21
+
22
+ @Inject()
23
+ private curdProService: CurdProService;
24
+
25
+ public async runBySchedule(): Promise<void> {
26
+ await this.reloadIfRedisChanged('workbenchCachedObj', () => this.loadWorkbenchCachedObj());
27
+ await this.reloadIfRedisChanged('sysConfgCacheObj', () => this.loadSysConfigObj());
28
+ await this.reloadIfRedisChanged('dataDictItemCacheObj', () => this.loadDataDictItemCacheObj());
29
+ await this.reloadIfRedisChanged('sysMenuCacheObj', () => this.loadSysMenuCacheObj());
30
+ await this.reloadIfRedisChanged('sysAppCacheObj', () => this.loadSysAppCacheObj());
31
+ await this.reloadIfRedisChanged('sysAppPageCacheObj', () => this.loadSysAppPageCacheObj());
32
+ await this.reloadIfRedisChanged('sysAnyApiCacheObj', () => this.loadSysAnyApiCacheObj());
33
+ await this.reloadIfRedisChanged('sysProxyApiCacheObj', () => this.loadSysProxyApiCacheObj());
34
+ await this.reloadIfRedisChanged('crudMethodCacheObj', () => this.loadCrudMethodCacheObj());
35
+ }
36
+
37
+ private async reloadIfRedisChanged(key: BizMemCacheKey, loader: () => Promise<void>) {
38
+ const redisTime = Number(await this.redisService.get(bizMemCacheStore.getRedisKey(key))) || 0;
39
+ if (redisTime !== bizMemCacheStore.getLastTime(key)) {
40
+ await loader();
41
+ bizMemCacheStore.setLastTime(key, redisTime);
42
+ }
43
+ }
44
+
45
+ private async loadSysMenuCacheObj() {
46
+
47
+ const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
48
+
49
+ const res1 = await this.curdProService.executeCrudByCfg(
50
+ {
51
+ condition: { deleted_at: 0, status: 1 },
52
+ },
53
+ {
54
+ sqlTable: SystemTables.sys_menus,
55
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
56
+ sqlDatabase: SystemDbName,
57
+ sqlDbType: SystemDbType,
58
+ maxLimit: 10 * 10000
59
+ }
60
+ );
61
+
62
+ const rows = res1.getResRows();
63
+ const cacheObj: ISysMenuCacheObj = {
64
+ sysMenuList: rows,
65
+ sysMenuCodeMap: _.keyBy(rows, 'menu_code')
66
+ }
67
+ bizMemCacheStore.setSysMenu(cacheObj);
68
+
69
+ }
70
+
71
+
72
+ private async loadDataDictItemCacheObj() {
73
+ const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
74
+ const res1 = await this.curdProService.executeCrudByCfg(
75
+ {
76
+ columns: ['dict_code', 'label', 'value', 'style', 'code'],
77
+ condition: { deleted_at: 0, status: 1 },
78
+ },
79
+ {
80
+ sqlTable: SystemTables.sys_data_dict_item,
81
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
82
+ sqlDatabase: SystemDbName,
83
+ sqlDbType: SystemDbType,
84
+ maxLimit: 10 * 10000
85
+ }
86
+ );
87
+
88
+ const rows = res1.getResRows();
89
+
90
+ const dictCodeMap: Record<string, any[]> = _.groupBy(rows, 'dict_code');
91
+
92
+ const cacheObj: IDataDictItemCacheObj = {
93
+ dictCodeMap: dictCodeMap
94
+ }
95
+ bizMemCacheStore.setDataDictItem(cacheObj);
96
+
97
+ }
98
+
99
+
100
+
101
+
102
+
103
+ private async loadSysConfigObj() {
104
+ const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
105
+ const res1 = await this.curdProService.executeCrudByCfg(
106
+ {
107
+ condition: { deleted_at: 0, status: 1 },
108
+ },
109
+ {
110
+ sqlTable: SystemTables.sys_configs,
111
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
112
+ sqlDatabase: SystemDbName,
113
+ sqlDbType: SystemDbType,
114
+ }
115
+ );
116
+ const sysConfigCodeMap = {};
117
+ const rows = res1.getResRows();
118
+ if (Array.isArray(rows) && rows.length > 0) {
119
+ for (let i = 0; i < rows.length; i++) {
120
+ const row = rows[i];
121
+ const { config_code } = row;
122
+ sysConfigCodeMap[config_code] = row;
123
+ }
124
+ }
125
+
126
+ const cacheObj: ISysConfgCacheObj = {
127
+ sysConfigList: rows,
128
+ sysConfigCodeMap: sysConfigCodeMap
129
+ }
130
+ bizMemCacheStore.setSysConfig(cacheObj);
131
+
132
+ }
133
+
134
+
135
+
136
+
137
+
138
+ private async loadWorkbenchCachedObj(): Promise<any> {
139
+ const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
140
+ const reqJson = { condition: { deleted_at: 0 }, pageSize: 1000, limit: 1000 };
141
+ const res = await this.curdProService.executeCrudByCfg(reqJson, {
142
+ sqlTable: SystemTables.sys_workbench,
143
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
144
+ sqlDatabase: SystemDbName,
145
+ sqlDbType: SystemDbType,
146
+ });
147
+ const listData = res.getResRows() as IWorkbenchEntity[];
148
+
149
+ const domainMap1 = MixinUtils.toMap(listData, (e: any) => e.workbench_domain); // 主域名。其他两个域名。
150
+ const domainMap2 = MixinUtils.toMap(listData, (e: any) => e.workbench_domain2);
151
+ const domainMap3 = MixinUtils.toMap(listData, (e: any) => e.workbench_domain3);
152
+ const domainMap = { ...domainMap1, ...domainMap2, ...domainMap3 };
153
+ const cacheObj: IWorkbenchCachedObj = {
154
+ workbenchList: listData,
155
+ workbenchDomainMap: domainMap
156
+ }
157
+ bizMemCacheStore.setWorkbench(cacheObj);
158
+ }
159
+
160
+ private async loadSysAppCacheObj() {
161
+ const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
162
+ const res = await this.curdProService.executeCrudByCfg(
163
+ {
164
+ condition: { deleted_at: 0 },
165
+ },
166
+ {
167
+ sqlTable: SystemTables.sys_app,
168
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
169
+ sqlDatabase: SystemDbName,
170
+ sqlDbType: SystemDbType,
171
+ maxLimit: 10 * 10000,
172
+ }
173
+ );
174
+ const rows = res.getResRows();
175
+ const cacheObj: ISysAppCacheObj = {
176
+ sysAppList: rows,
177
+ sysAppCodeMap: _.keyBy(rows, 'app_code'),
178
+ };
179
+ bizMemCacheStore.setSysApp(cacheObj);
180
+ }
181
+
182
+ private async loadSysAppPageCacheObj() {
183
+ const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
184
+ const res = await this.curdProService.executeCrudByCfg(
185
+ {
186
+ condition: { deleted_at: 0 },
187
+ },
188
+ {
189
+ sqlTable: SystemTables.sys_app_page,
190
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
191
+ sqlDatabase: SystemDbName,
192
+ sqlDbType: SystemDbType,
193
+ maxLimit: 10 * 10000,
194
+ }
195
+ );
196
+ const rows = res.getResRows();
197
+ const sysAppPageMap: Record<string, any> = {};
198
+ for (let i = 0; i < rows.length; i++) {
199
+ const row = rows[i];
200
+ const cacheKey = buildSysAppPageCacheKey(row.workbench_code, row.page_path);
201
+ sysAppPageMap[cacheKey] = row;
202
+ }
203
+ const cacheObj: ISysAppPageCacheObj = {
204
+ sysAppPageList: rows,
205
+ sysAppPageMap,
206
+ };
207
+ bizMemCacheStore.setSysAppPage(cacheObj);
208
+ }
209
+
210
+ private async loadSysAnyApiCacheObj() {
211
+ const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
212
+ const res = await this.curdProService.executeCrudByCfg(
213
+ {
214
+ condition: { deleted_at: 0 },
215
+ },
216
+ {
217
+ sqlTable: SystemTables.sys_anyapi,
218
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
219
+ sqlDatabase: SystemDbName,
220
+ sqlDbType: SystemDbType,
221
+ maxLimit: 10 * 10000,
222
+ }
223
+ );
224
+ const rows = res.getResRows();
225
+ const sysAnyApiMethodMap: Record<string, any> = {};
226
+ for (let i = 0; i < rows.length; i++) {
227
+ const row = rows[i];
228
+ const obj = { ...row };
229
+ obj.parsedOthers = parseJsonObject(obj.others);
230
+ sysAnyApiMethodMap[row.method] = obj;
231
+ }
232
+ const cacheObj: ISysAnyApiCacheObj = {
233
+ sysAnyApiList: rows,
234
+ sysAnyApiMethodMap,
235
+ };
236
+ bizMemCacheStore.setSysAnyApi(cacheObj);
237
+ }
238
+
239
+ private async loadSysProxyApiCacheObj() {
240
+ const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
241
+ const res = await this.curdProService.executeCrudByCfg(
242
+ {
243
+ condition: { deleted_at: 0 },
244
+ },
245
+ {
246
+ sqlTable: SystemTables.sys_proxyapi,
247
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
248
+ sqlDatabase: SystemDbName,
249
+ sqlDbType: SystemDbType,
250
+ maxLimit: 10 * 10000,
251
+ }
252
+ );
253
+ const rows = res.getResRows();
254
+ const cacheObj: ISysProxyApiCacheObj = {
255
+ sysProxyApiList: rows,
256
+ routeTrieMap: null,
257
+ };
258
+ bizMemCacheStore.setSysProxyApi(cacheObj);
259
+ }
260
+
261
+ private async loadCrudMethodCacheObj() {
262
+ const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
263
+ const res = await this.curdProService.executeCrudByCfg(
264
+ {
265
+ condition: { deleted_at: 0 },
266
+ },
267
+ {
268
+ sqlTable: SystemTables.sys_crud_methods,
269
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
270
+ sqlDatabase: SystemDbName,
271
+ sqlDbType: SystemDbType,
272
+ maxLimit: 10 * 10000,
273
+ }
274
+ );
275
+ const rows = res.getResRows();
276
+ const crudMethodList = rows.map(row => parseCrudMethodInfo(row));
277
+ const cacheObj: ICrudMethodCacheObj = {
278
+ crudMethodList,
279
+ crudMethodMap: _.keyBy(crudMethodList, 'method'),
280
+ };
281
+ bizMemCacheStore.setCrudMethod(cacheObj);
282
+ setMethodCache(crudMethodList);
283
+ }
284
+
285
+
286
+
287
+
288
+ }