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
@@ -16,40 +16,46 @@ const CurdProService_1 = require("./curd/CurdProService");
16
16
  const CurdMixUtils_1 = require("./curd/CurdMixUtils");
17
17
  const SystemTables_1 = require("../models/SystemTables");
18
18
  const parseConfig_1 = require("../libs/utils/parseConfig");
19
- const RedisCacheService_1 = require("./base/RedisCacheService");
20
19
  const CurdMixByLinkToCustomService_1 = require("./curd/CurdMixByLinkToCustomService");
21
20
  const global_config_1 = require("../libs/global-config/global-config");
22
- const RedisKeys_1 = require("../models/RedisKeys");
21
+ const CacheServiceFactory_1 = require("./base/cache/CacheServiceFactory");
22
+ const SystemEntities_1 = require("../models/SystemEntities");
23
23
  function filterEmpty(e) {
24
24
  return e.label && typeof e.value !== 'undefined';
25
25
  }
26
+ const DEFAULT_CACHE_LEVEL = SystemEntities_1.CacheLevelEnum.MEMORY;
27
+ const DEFAULT_CACHE_SECOND = 60 * 5;
26
28
  let EnumInfoService = class EnumInfoService {
27
29
  async queryEnumInfo(codeList, refreshCache) {
28
- const expireSecond = 2 * 60;
30
+ const { queryEnumInfoCacheLevel, queryEnumInfoCacheSecond } = this.ctx.workbenchInfoTools.getWorkbenchConfig() || {};
31
+ const getEnumInfoByCode = async (type, code) => {
32
+ if (type === CurdMixUtils_1.RelatedType.linkToCustom) {
33
+ return await this.queryEnumInfoByLinkToCustom(code);
34
+ }
35
+ const cacheKey = `${type}@@${code}`;
36
+ const emums = refreshCache ? null : await this.cacheServiceFactory.getJsonObjectCache({
37
+ cacheLevel: queryEnumInfoCacheLevel || DEFAULT_CACHE_LEVEL,
38
+ cacheSecond: queryEnumInfoCacheSecond || DEFAULT_CACHE_SECOND,
39
+ cacheKey,
40
+ refreshCache,
41
+ cacheName: SystemEntities_1.CacheNameEnum.GetEnumInfoByCode,
42
+ getter: async () => {
43
+ if (type === CurdMixUtils_1.RelatedType.sysCfgEnum) {
44
+ return await this.queryEnumInfoBySysCfgEnum(code);
45
+ }
46
+ else if (type === CurdMixUtils_1.RelatedType.dict) {
47
+ return await this.queryEnumInfoItemByDict(code);
48
+ }
49
+ return [];
50
+ },
51
+ });
52
+ return emums;
53
+ };
29
54
  const result = [];
30
55
  for (let i = 0; i < codeList.length; i++) {
31
56
  const { code, type } = codeList[i];
32
- const cacheKey = `${RedisKeys_1.RedisKeys.QUERY_ENUM_INFO_PREFIX}${type}@@${code}`;
33
- const emums = refreshCache ? null : await this.redisCacheService.getJsonObject(cacheKey);
34
- if (!Array.isArray(emums) || emums.length === 0) {
35
- let values;
36
- if (type === CurdMixUtils_1.RelatedType.sysCfgEnum) {
37
- values = await this.queryEnumInfoBySysCfgEnum(code);
38
- await this.redisCacheService.setJsonObject(cacheKey, values, expireSecond);
39
- }
40
- else if (type === CurdMixUtils_1.RelatedType.dict) {
41
- values = await this.queryEnumInfoItemByDict(code);
42
- await this.redisCacheService.setJsonObject(cacheKey, values, expireSecond);
43
- }
44
- else if (type === CurdMixUtils_1.RelatedType.linkToCustom) {
45
- values = await this.queryEnumInfoByLinkToCustom(code);
46
- // await this.redisCacheService.setJsonObject(cacheKey, values, expireSecond);
47
- }
48
- result.push({ type, code, values });
49
- }
50
- else {
51
- result.push({ type, code, values: emums });
52
- }
57
+ const emums = await getEnumInfoByCode(type, code);
58
+ result.push({ type, code, values: emums });
53
59
  }
54
60
  return result;
55
61
  }
@@ -119,8 +125,8 @@ __decorate([
119
125
  ], EnumInfoService.prototype, "curdMixByLinkToCustomService", void 0);
120
126
  __decorate([
121
127
  (0, core_1.Inject)(),
122
- __metadata("design:type", RedisCacheService_1.RedisCacheService)
123
- ], EnumInfoService.prototype, "redisCacheService", void 0);
128
+ __metadata("design:type", CacheServiceFactory_1.CacheServiceFactory)
129
+ ], EnumInfoService.prototype, "cacheServiceFactory", void 0);
124
130
  EnumInfoService = __decorate([
125
131
  (0, core_1.Provide)()
126
132
  ], EnumInfoService);
@@ -1,7 +1,7 @@
1
1
  import { ISessionInfo } from '../models/userSession';
2
2
  export declare class UserSessionService {
3
3
  private ctx;
4
- private redisCacheService;
4
+ private cacheServiceFactory;
5
5
  private curdProService;
6
6
  private fatcmsUserSessionKeepTimeSecond;
7
7
  /**
@@ -9,6 +9,11 @@ export declare class UserSessionService {
9
9
  * @returns
10
10
  */
11
11
  private getSessionKeepTimeSecond;
12
+ /**
13
+ * 获取用户会话缓存服务
14
+ * @returns
15
+ */
16
+ private getUserSessionCacheService;
12
17
  /**
13
18
  * 保存用户会话信息。
14
19
  * 用户登录后,在业务的项目中,可以调用此函数,更新bizExt字段
@@ -13,13 +13,13 @@ exports.UserSessionService = void 0;
13
13
  const core_1 = require("@midwayjs/core");
14
14
  const functions_1 = require("../libs/utils/functions");
15
15
  const userSession_1 = require("../models/userSession");
16
- const RedisCacheService_1 = require("./base/RedisCacheService");
17
- const RedisKeys_1 = require("../models/RedisKeys");
18
16
  const AsyncTaskModel_1 = require("../models/AsyncTaskModel");
19
17
  const CurdProService_1 = require("./curd/CurdProService");
20
18
  const keys_1 = require("../libs/crud-pro/models/keys");
21
19
  const SystemTables_1 = require("../models/SystemTables");
22
20
  const global_config_1 = require("../libs/global-config/global-config");
21
+ const CacheServiceFactory_1 = require("./base/cache/CacheServiceFactory");
22
+ const SystemEntities_1 = require("../models/SystemEntities");
23
23
  function pickUserAvatar(avatar) {
24
24
  if (!avatar) {
25
25
  return null;
@@ -45,10 +45,7 @@ function pickUserAvatar(avatar) {
45
45
  }
46
46
  return null;
47
47
  }
48
- const SESSION_KEEP_TIME_SECOND = 3600 * 24 * 30;
49
- const toCacheKey = (sessionId) => {
50
- return `${RedisKeys_1.RedisKeys.USER_SESSION_PREFIX}${sessionId}`;
51
- };
48
+ const SESSION_KEEP_TIME_SECOND = 3600 * 24 * 7;
52
49
  let UserSessionService = class UserSessionService {
53
50
  /**
54
51
  * 获取用户会话保持时间: 单位秒
@@ -60,6 +57,13 @@ let UserSessionService = class UserSessionService {
60
57
  }
61
58
  return SESSION_KEEP_TIME_SECOND;
62
59
  }
60
+ /**
61
+ * 获取用户会话缓存服务
62
+ * @returns
63
+ */
64
+ getUserSessionCacheService() {
65
+ return this.cacheServiceFactory.getCacheService(SystemEntities_1.CacheLevelEnum.REDIS, SystemEntities_1.CacheNameEnum.UserSessionBySessionId);
66
+ }
63
67
  /**
64
68
  * 保存用户会话信息。
65
69
  * 用户登录后,在业务的项目中,可以调用此函数,更新bizExt字段
@@ -69,14 +73,16 @@ let UserSessionService = class UserSessionService {
69
73
  sessionInfo.avatar = pickUserAvatar(sessionInfo.avatar);
70
74
  const sessionId = sessionInfo.sessionId;
71
75
  const keepTime = this.getSessionKeepTimeSecond();
72
- await this.redisCacheService.setJsonObject(toCacheKey(sessionId), sessionInfo, keepTime);
76
+ const userSessionCache = this.getUserSessionCacheService();
77
+ await userSessionCache.setJsonObject(sessionId, sessionInfo, keepTime);
73
78
  }
74
79
  /**
75
80
  * 移除Session
76
81
  * @param sessionId
77
82
  */
78
83
  async removeUserSession(sessionId) {
79
- return await this.redisCacheService.removeItem(toCacheKey(sessionId));
84
+ const userSessionCache = this.getUserSessionCacheService();
85
+ return await userSessionCache.removeItem(sessionId);
80
86
  }
81
87
  /**
82
88
  * 获取用户会话信息
@@ -87,14 +93,15 @@ let UserSessionService = class UserSessionService {
87
93
  if (!sessionId) {
88
94
  return null;
89
95
  }
90
- return await this.redisCacheService.getJsonObject(toCacheKey(sessionId));
96
+ const userSessionCache = this.getUserSessionCacheService();
97
+ return await userSessionCache.getJsonObject(sessionId);
91
98
  }
92
99
  /**
93
100
  * 从异步任务队列中获取当时用户的Session信息
94
101
  * @param asyncTaskId
95
102
  */
96
103
  async getCurrentUserSessionByAsyncTaskId(asyncTaskId) {
97
- const taskSessionCacheKey = `${RedisKeys_1.RedisKeys.USER_SESSION_ASYNC_TASK_ID_PREFIX}${asyncTaskId}`;
104
+ const taskSessionCacheKey = `${asyncTaskId}`;
98
105
  const loadSessionInfo = async () => {
99
106
  const { SystemDbName, SystemDbType } = global_config_1.GLOBAL_STATIC_CONFIG.getConfig();
100
107
  const asyncTaskRes = await this.curdProService.executeCrudByCfg({
@@ -114,11 +121,13 @@ let UserSessionService = class UserSessionService {
114
121
  }
115
122
  return (0, functions_1.parseJsonObject)(asyncTaskObj.created_user_session);
116
123
  };
117
- let sessionInfo1 = await this.redisCacheService.getJsonObject(taskSessionCacheKey);
118
- if (!sessionInfo1) {
119
- sessionInfo1 = await loadSessionInfo();
120
- await this.redisCacheService.setJsonObject(taskSessionCacheKey, sessionInfo1, 3600); // 只缓存一小时即可
121
- }
124
+ const sessionInfo1 = await this.cacheServiceFactory.getJsonObjectCache({
125
+ cacheLevel: SystemEntities_1.CacheLevelEnum.REDIS,
126
+ cacheName: SystemEntities_1.CacheNameEnum.UserSessionByAsyncTaskId,
127
+ cacheKey: taskSessionCacheKey,
128
+ cacheSecond: 3600,
129
+ getter: loadSessionInfo,
130
+ });
122
131
  return sessionInfo1;
123
132
  }
124
133
  /**
@@ -141,8 +150,8 @@ __decorate([
141
150
  ], UserSessionService.prototype, "ctx", void 0);
142
151
  __decorate([
143
152
  (0, core_1.Inject)(),
144
- __metadata("design:type", RedisCacheService_1.RedisCacheService)
145
- ], UserSessionService.prototype, "redisCacheService", void 0);
153
+ __metadata("design:type", CacheServiceFactory_1.CacheServiceFactory)
154
+ ], UserSessionService.prototype, "cacheServiceFactory", void 0);
146
155
  __decorate([
147
156
  (0, core_1.Inject)(),
148
157
  __metadata("design:type", CurdProService_1.CurdProService)
@@ -50,4 +50,5 @@ export declare class WorkbenchService extends BaseService implements IScheduleSe
50
50
  private getCurrentHost;
51
51
  isSupportCurrentWorkbench(workbench_code_array: string | string[]): Promise<boolean>;
52
52
  isSupportCurrentHostForForMiddleware(): Promise<string | boolean>;
53
+ isSupportTheNodePathByWorkbenchInfo(workbenchInfo: IWorkbenchEntity, path: string): boolean;
53
54
  }
@@ -22,6 +22,7 @@ const fatcms_request_1 = require("../libs/utils/fatcms-request");
22
22
  const MixinUtils_1 = require("../libs/crud-pro/utils/MixinUtils");
23
23
  const global_config_1 = require("../libs/global-config/global-config");
24
24
  const AsyncTaskModel_1 = require("../models/AsyncTaskModel");
25
+ const format_url_1 = require("../libs/utils/format-url");
25
26
  //
26
27
  // const lruCache = new LRUCache<string, any>({
27
28
  // max: 500,
@@ -153,10 +154,11 @@ let WorkbenchService = class WorkbenchService extends BaseService_1.BaseService
153
154
  * @returns
154
155
  */
155
156
  getCurrentHost() {
156
- const host = this.ctx.request.host;
157
+ let host = this.ctx.request.host;
157
158
  if (!host) {
158
159
  throw new exceptions_1.CommonException('GET_CURRENT_HOST_ERROR', '获取当前域名失败');
159
160
  }
161
+ host = (0, format_url_1.formatHost)(host);
160
162
  return host;
161
163
  }
162
164
  async isSupportCurrentWorkbench(workbench_code_array) {
@@ -192,6 +194,30 @@ let WorkbenchService = class WorkbenchService extends BaseService_1.BaseService
192
194
  }
193
195
  return true;
194
196
  }
197
+ isSupportTheNodePathByWorkbenchInfo(workbenchInfo, path) {
198
+ // 非node api路径,直接返回true
199
+ if (!path.startsWith('/ns/')) {
200
+ return true;
201
+ }
202
+ const configObj = (0, functions_1.parseJsonObject)(workbenchInfo === null || workbenchInfo === void 0 ? void 0 : workbenchInfo.config_content);
203
+ // 为开启校验开关。开关为true时,才校验。
204
+ if (!configObj || !configObj.nodeApiWhiteEnable) {
205
+ return true;
206
+ }
207
+ let isOkByList = false;
208
+ const { nodeApiWhiteList, nodeApiWhitePrefix } = configObj;
209
+ if (Array.isArray(nodeApiWhiteList) && nodeApiWhiteList.length > 0) {
210
+ isOkByList = nodeApiWhiteList.includes(path);
211
+ }
212
+ if (isOkByList) {
213
+ return true;
214
+ }
215
+ let isOkByPrefix = false;
216
+ if (Array.isArray(nodeApiWhitePrefix) && nodeApiWhitePrefix.length > 0) {
217
+ isOkByPrefix = nodeApiWhitePrefix.some((prefix) => path.startsWith(prefix));
218
+ }
219
+ return isOkByPrefix;
220
+ }
195
221
  };
196
222
  __decorate([
197
223
  (0, core_1.Inject)(),
@@ -0,0 +1,20 @@
1
+ import { Context } from '@midwayjs/koa';
2
+ import { ICacheServiceBase } from '../../../models/bizmodels';
3
+ import { CacheLevelEnum, CacheNameEnum } from '../../../models/SystemEntities';
4
+ import { RedisService } from '@midwayjs/redis';
5
+ interface IGetJsonObjectCacheParam {
6
+ cacheLevel: CacheLevelEnum;
7
+ cacheName: CacheNameEnum;
8
+ cacheKey: string;
9
+ cacheSecond: number;
10
+ refreshCache?: boolean;
11
+ getter: () => Promise<any>;
12
+ }
13
+ export declare class CacheServiceFactory {
14
+ protected ctx: Context;
15
+ private fatcmsCache;
16
+ protected redisService: RedisService;
17
+ getCacheService(cacheLevelEnum: CacheLevelEnum, cacheName: CacheNameEnum): ICacheServiceBase;
18
+ getJsonObjectCache(param: IGetJsonObjectCacheParam): Promise<any>;
19
+ }
20
+ export {};
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.CacheServiceFactory = void 0;
13
+ const core_1 = require("@midwayjs/core");
14
+ const SystemEntities_1 = require("../../../models/SystemEntities");
15
+ const redis_1 = require("@midwayjs/redis");
16
+ const FatcmsBaseCtxCache_1 = require("./FatcmsBaseCtxCache");
17
+ const FatcmsBaseRedisCache_1 = require("./FatcmsBaseRedisCache");
18
+ const FatcmsBaseDiskCache_1 = require("./FatcmsBaseDiskCache");
19
+ const FatcmsBaseMemoryCache_1 = require("./FatcmsBaseMemoryCache");
20
+ const FatcmsBaseNoneCache_1 = require("./FatcmsBaseNoneCache");
21
+ let CacheServiceFactory = class CacheServiceFactory {
22
+ getCacheService(cacheLevelEnum, cacheName) {
23
+ switch (cacheLevelEnum) {
24
+ case SystemEntities_1.CacheLevelEnum.CONTEXT:
25
+ return new FatcmsBaseCtxCache_1.FatcmsBaseCtxCache(this.ctx, cacheName);
26
+ case SystemEntities_1.CacheLevelEnum.REDIS:
27
+ return new FatcmsBaseRedisCache_1.FatcmsBaseRedisCache(this.ctx, cacheName, this.redisService);
28
+ case SystemEntities_1.CacheLevelEnum.DISK:
29
+ return new FatcmsBaseDiskCache_1.FatcmsBaseDiskCache(this.ctx, cacheName, this.fatcmsCache);
30
+ case SystemEntities_1.CacheLevelEnum.MEMORY:
31
+ return new FatcmsBaseMemoryCache_1.FatcmsBaseMemoryCache(this.ctx, cacheName);
32
+ default:
33
+ return new FatcmsBaseNoneCache_1.FatcmsBaseNoneCache(this.ctx, cacheName);
34
+ }
35
+ }
36
+ async getJsonObjectCache(param) {
37
+ let { cacheLevel, cacheName, cacheKey, cacheSecond, refreshCache, getter } = param;
38
+ if (!cacheSecond || typeof cacheSecond !== 'number') {
39
+ console.error('[CacheServiceFactory#getJsonObjectCache] cacheSecond is not number, use default value 60, ' + JSON.stringify(param));
40
+ cacheSecond = 60;
41
+ }
42
+ const realCacheKey = this.fatcmsCache.keyPrefix + cacheKey;
43
+ const cacheService = this.getCacheService(cacheLevel, cacheName);
44
+ let obj = cacheService.getJsonObject(realCacheKey);
45
+ if (!obj || refreshCache === true) {
46
+ obj = await getter();
47
+ cacheService.setJsonObject(realCacheKey, obj, cacheSecond);
48
+ }
49
+ return obj;
50
+ }
51
+ };
52
+ __decorate([
53
+ (0, core_1.Inject)(),
54
+ __metadata("design:type", Object)
55
+ ], CacheServiceFactory.prototype, "ctx", void 0);
56
+ __decorate([
57
+ (0, core_1.Config)('fatcmsCache'),
58
+ __metadata("design:type", Object)
59
+ ], CacheServiceFactory.prototype, "fatcmsCache", void 0);
60
+ __decorate([
61
+ (0, core_1.Inject)(),
62
+ __metadata("design:type", redis_1.RedisService)
63
+ ], CacheServiceFactory.prototype, "redisService", void 0);
64
+ CacheServiceFactory = __decorate([
65
+ (0, core_1.Provide)()
66
+ ], CacheServiceFactory);
67
+ exports.CacheServiceFactory = CacheServiceFactory;
@@ -0,0 +1,19 @@
1
+ import { ICacheServiceBase } from '../../../models/bizmodels';
2
+ import { CacheNameEnum } from '../../../models/SystemEntities';
3
+ import { Context } from '@midwayjs/koa';
4
+ export declare class FatcmsBaseCtxCache implements ICacheServiceBase {
5
+ private ctx;
6
+ private cacheName;
7
+ constructor(ctx: Context, cacheName: CacheNameEnum);
8
+ private getCacheMap;
9
+ getJsonObject(key: string): Promise<any>;
10
+ /**
11
+ * 设置缓存对象
12
+ * @param key
13
+ * @param obj
14
+ * @param expireSecond 过期时间,单位秒
15
+ * @returns
16
+ */
17
+ setJsonObject(key: string, obj: any, expireSecond?: number): Promise<any>;
18
+ removeItem(key: string): Promise<any>;
19
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FatcmsBaseCtxCache = void 0;
4
+ class FatcmsBaseCtxCache {
5
+ constructor(ctx, cacheName) {
6
+ this.ctx = ctx;
7
+ this.cacheName = cacheName;
8
+ }
9
+ getCacheMap() {
10
+ if (!this.ctx.baseCtxCacheMap) {
11
+ this.ctx.baseCtxCacheMap = new Map();
12
+ }
13
+ if (!this.ctx.baseCtxCacheMap.has(this.cacheName)) {
14
+ this.ctx.baseCtxCacheMap.set(this.cacheName, new Map());
15
+ }
16
+ return this.ctx.baseCtxCacheMap.get(this.cacheName);
17
+ }
18
+ async getJsonObject(key) {
19
+ const cacheMap = this.getCacheMap();
20
+ return cacheMap.get(key);
21
+ }
22
+ /**
23
+ * 设置缓存对象
24
+ * @param key
25
+ * @param obj
26
+ * @param expireSecond 过期时间,单位秒
27
+ * @returns
28
+ */
29
+ async setJsonObject(key, obj, expireSecond) {
30
+ const cacheMap = this.getCacheMap();
31
+ cacheMap.set(key, obj);
32
+ }
33
+ async removeItem(key) {
34
+ const cacheMap = this.getCacheMap();
35
+ cacheMap.delete(key);
36
+ }
37
+ }
38
+ exports.FatcmsBaseCtxCache = FatcmsBaseCtxCache;
@@ -0,0 +1,24 @@
1
+ import { Context } from '@midwayjs/koa';
2
+ import { ICacheServiceBase, IfatcmsCacheConfig } from '../../../models/bizmodels';
3
+ export declare class FatcmsBaseDiskCache implements ICacheServiceBase {
4
+ protected ctx: Context;
5
+ protected cacheName: string;
6
+ private fatcmsCache;
7
+ constructor(ctx: Context, cacheName: string, fatcmsCache: IfatcmsCacheConfig);
8
+ /**
9
+ * 获取缓存文件路径
10
+ * @param key
11
+ * @returns
12
+ */
13
+ private getCacheFilePath;
14
+ getJsonObject(key: string): Promise<any>;
15
+ /**
16
+ * 设置缓存对象
17
+ * @param key
18
+ * @param obj
19
+ * @param expireSecond 过期时间,单位秒
20
+ * @returns
21
+ */
22
+ setJsonObject(key: string, obj: any, expireSecond?: number): Promise<any>;
23
+ removeItem(key: string): Promise<any>;
24
+ }
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FatcmsBaseDiskCache = void 0;
4
+ const md5 = require("md5");
5
+ const fs = require("fs/promises");
6
+ const fs2 = require("fs");
7
+ const path = require("path");
8
+ const functions_1 = require("../../../libs/utils/functions");
9
+ class FatcmsBaseDiskCache {
10
+ constructor(ctx, cacheName, fatcmsCache) {
11
+ this.ctx = ctx;
12
+ this.cacheName = cacheName;
13
+ this.fatcmsCache = fatcmsCache;
14
+ }
15
+ /**
16
+ * 获取缓存文件路径
17
+ * @param key
18
+ * @returns
19
+ */
20
+ async getCacheFilePath(key, checkDirExist = true) {
21
+ const cacheDir0 = this.fatcmsCache.diskCacheDir;
22
+ const cacheDir = path.join(cacheDir0, this.cacheName);
23
+ if (checkDirExist) {
24
+ const isDirExist = await fs2.promises.access(cacheDir).then(() => true, () => false);
25
+ if (!isDirExist) {
26
+ await fs2.promises.mkdir(cacheDir, { recursive: true });
27
+ }
28
+ }
29
+ return path.join(cacheDir, `${md5(key)}.json`);
30
+ }
31
+ async getJsonObject(key) {
32
+ const cacheFilePath = await this.getCacheFilePath(key, false);
33
+ try {
34
+ const str = await fs.readFile(cacheFilePath, 'utf8');
35
+ const data = (0, functions_1.parseJsonObject)(str);
36
+ if (data && data.key === key && data.expireAt > Date.now()) {
37
+ return data.data;
38
+ }
39
+ return null;
40
+ }
41
+ catch (error) {
42
+ return null;
43
+ }
44
+ }
45
+ /**
46
+ * 设置缓存对象
47
+ * @param key
48
+ * @param obj
49
+ * @param expireSecond 过期时间,单位秒
50
+ * @returns
51
+ */
52
+ async setJsonObject(key, obj, expireSecond) {
53
+ if (!expireSecond) {
54
+ expireSecond = 60;
55
+ }
56
+ const str = JSON.stringify({
57
+ data: obj,
58
+ key,
59
+ expireAt: Date.now() + expireSecond * 1000,
60
+ });
61
+ const cacheFilePath = await this.getCacheFilePath(key);
62
+ try {
63
+ await fs.writeFile(cacheFilePath, str, 'utf8');
64
+ return true;
65
+ }
66
+ catch (error) {
67
+ return false;
68
+ }
69
+ }
70
+ async removeItem(key) {
71
+ const cacheFilePath = await this.getCacheFilePath(key);
72
+ try {
73
+ await fs.unlink(cacheFilePath);
74
+ return true;
75
+ }
76
+ catch (error) {
77
+ return false;
78
+ }
79
+ }
80
+ }
81
+ exports.FatcmsBaseDiskCache = FatcmsBaseDiskCache;
@@ -0,0 +1,18 @@
1
+ import { Context } from '@midwayjs/koa';
2
+ import { ICacheServiceBase } from '../../../models/bizmodels';
3
+ export declare class FatcmsBaseMemoryCache implements ICacheServiceBase {
4
+ protected ctx: Context;
5
+ protected cacheName: string;
6
+ constructor(ctx: Context, cacheName: string);
7
+ private getLruCache;
8
+ getJsonObject(key: string): Promise<any>;
9
+ /**
10
+ * 设置缓存对象
11
+ * @param key
12
+ * @param obj
13
+ * @param expireSecond 过期时间,单位秒
14
+ * @returns
15
+ */
16
+ setJsonObject(key: string, obj: any, expireSecond?: number): Promise<any>;
17
+ removeItem(key: string): Promise<any>;
18
+ }
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FatcmsBaseMemoryCache = void 0;
4
+ const lru_cache_1 = require("lru-cache");
5
+ const LRU_CACHE_MAP = new Map();
6
+ class FatcmsBaseMemoryCache {
7
+ constructor(ctx, cacheName) {
8
+ this.ctx = ctx;
9
+ this.cacheName = cacheName;
10
+ }
11
+ getLruCache() {
12
+ if (!LRU_CACHE_MAP.has(this.cacheName)) {
13
+ const lruCache = new lru_cache_1.LRUCache({
14
+ max: 500,
15
+ ttl: 1000 * 60 * 60 * 24,
16
+ ttlAutopurge: true,
17
+ });
18
+ LRU_CACHE_MAP.set(this.cacheName, lruCache);
19
+ }
20
+ return LRU_CACHE_MAP.get(this.cacheName);
21
+ }
22
+ async getJsonObject(key) {
23
+ try {
24
+ const lruCache = this.getLruCache();
25
+ const saveObj = lruCache.get(key);
26
+ if (saveObj && saveObj.key === key && saveObj.expireAt > Date.now()) {
27
+ return saveObj.data;
28
+ }
29
+ return null;
30
+ }
31
+ catch (error) {
32
+ return null;
33
+ }
34
+ }
35
+ /**
36
+ * 设置缓存对象
37
+ * @param key
38
+ * @param obj
39
+ * @param expireSecond 过期时间,单位秒
40
+ * @returns
41
+ */
42
+ async setJsonObject(key, obj, expireSecond) {
43
+ const lruCache = this.getLruCache();
44
+ if (!expireSecond) {
45
+ expireSecond = 60;
46
+ }
47
+ const saveObj = {
48
+ data: obj,
49
+ key,
50
+ expireAt: Date.now() + expireSecond * 1000,
51
+ };
52
+ lruCache.set(key, saveObj);
53
+ return true;
54
+ }
55
+ async removeItem(key) {
56
+ try {
57
+ const lruCache = this.getLruCache();
58
+ lruCache.delete(key);
59
+ return true;
60
+ }
61
+ catch (error) {
62
+ return false;
63
+ }
64
+ }
65
+ }
66
+ exports.FatcmsBaseMemoryCache = FatcmsBaseMemoryCache;
@@ -0,0 +1,10 @@
1
+ import { ICacheServiceBase } from '../../../models/bizmodels';
2
+ import { Context } from '@midwayjs/koa';
3
+ export declare class FatcmsBaseNoneCache implements ICacheServiceBase {
4
+ protected ctx: Context;
5
+ protected cacheName: string;
6
+ constructor(ctx: Context, cacheName: string);
7
+ getJsonObject(key: string): Promise<any>;
8
+ setJsonObject(key: string, obj: any, expireSecond?: number): Promise<any>;
9
+ removeItem(key: string): Promise<any>;
10
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FatcmsBaseNoneCache = void 0;
4
+ class FatcmsBaseNoneCache {
5
+ constructor(ctx, cacheName) {
6
+ this.ctx = ctx;
7
+ this.cacheName = cacheName;
8
+ }
9
+ async getJsonObject(key) {
10
+ return null;
11
+ }
12
+ async setJsonObject(key, obj, expireSecond) {
13
+ return true;
14
+ }
15
+ async removeItem(key) {
16
+ return true;
17
+ }
18
+ }
19
+ exports.FatcmsBaseNoneCache = FatcmsBaseNoneCache;
@@ -1,12 +1,12 @@
1
1
  import { Context } from '@midwayjs/koa';
2
2
  import { RedisService } from '@midwayjs/redis';
3
- import { OSSServiceFactory } from '@midwayjs/oss';
4
- import * as koa from '@midwayjs/koa';
5
- export declare class RedisCacheService {
3
+ import { ICacheServiceBase } from '../../../models/bizmodels';
4
+ export declare class FatcmsBaseRedisCache implements ICacheServiceBase {
6
5
  protected ctx: Context;
7
- protected app: koa.Application;
6
+ protected cacheName: string;
8
7
  protected redisService: RedisService;
9
- protected ossServiceFactory: OSSServiceFactory;
8
+ constructor(ctx: Context, cacheName: string, redisService: RedisService);
9
+ private getCacheKey;
10
10
  getJsonObject(key: string): Promise<any>;
11
11
  /**
12
12
  * 设置缓存对象
@@ -15,6 +15,6 @@ export declare class RedisCacheService {
15
15
  * @param expireSecond 过期时间,单位秒
16
16
  * @returns
17
17
  */
18
- setJsonObject(key: string, obj: any, expireSecond: number): Promise<any>;
18
+ setJsonObject(key: string, obj: any, expireSecond?: number): Promise<any>;
19
19
  removeItem(key: string): Promise<any>;
20
20
  }