@bit-sun/business-component 4.0.13-alpha.9 → 4.2.0-aiwei-alpha.1
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.
- package/.umirc.ts +21 -10
- package/dist/components/Business/AddSelectBusiness/index.d.ts +3 -4
- package/dist/components/Business/BsLayouts/Components/AllFunc/drawContent.d.ts +1 -2
- package/dist/components/Business/BsLayouts/Components/ChooseStore/index.d.ts +1 -2
- package/dist/components/Business/BsLayouts/Components/CustomerMenu/MenuSetting/index.d.ts +1 -1
- package/dist/components/Business/BsLayouts/Components/CustomerMenu/MenuSetting/leftTree.d.ts +1 -1
- package/dist/components/Business/BsLayouts/Components/CustomerMenu/MenuSetting/rightTree.d.ts +2 -2
- package/dist/components/Business/BsLayouts/Components/CustomerMenu/globalMenu/DrawContent.d.ts +1 -2
- package/dist/components/Business/BsLayouts/Components/CustomerMenu/globalMenu/customMenuHeader.d.ts +1 -2
- package/dist/components/Business/BsLayouts/Components/CustomerMenu/index.d.ts +1 -1
- package/dist/components/Business/BsLayouts/Components/GlobalHeader/index.d.ts +1 -2
- package/dist/components/Business/BsLayouts/Components/RightContent/LoginModal.d.ts +1 -2
- package/dist/components/Business/BsLayouts/index.d.ts +1 -1
- package/dist/components/Business/BsSulaQueryTable/SearchItemSetting.d.ts +5 -5
- package/dist/components/Business/BsSulaQueryTable/index.d.ts +1 -2
- package/dist/components/Business/BsSulaQueryTable/setting.d.ts +11 -8
- package/dist/components/Business/BsSulaQueryTable/utils.d.ts +14 -15
- package/dist/components/Business/CommodityEntry/index.d.ts +1 -2
- package/dist/components/Business/CommonAlert/index.d.ts +1 -2
- package/dist/components/Business/CommonGuideWrapper/index.d.ts +3 -3
- package/dist/components/Business/DetailPageWrapper/index.d.ts +11 -6
- package/dist/components/Business/HomePageWrapper/index.d.ts +1 -2
- package/dist/components/Business/ItemPropertySelector/index.d.ts +2 -0
- package/dist/components/Business/JsonQueryTable/components/FieldsModifyModal.d.ts +1 -2
- package/dist/components/Business/JsonQueryTable/components/FieldsSettingsTable.d.ts +1 -2
- package/dist/components/Business/JsonQueryTable/components/Formula.d.ts +1 -2
- package/dist/components/Business/JsonQueryTable/components/MaintainOptions.d.ts +1 -2
- package/dist/components/Business/JsonQueryTable/drawer/index.d.ts +1 -2
- package/dist/components/Business/ModalUtils/index.d.ts +8 -0
- package/dist/components/Business/PropertyModal/index.d.ts +1 -2
- package/dist/components/Business/PropertyModal/propertyGroup.d.ts +1 -1
- package/dist/components/Business/SearchSelect/index.d.ts +1 -1
- package/dist/components/Business/SearchSelect/utils.d.ts +4 -2
- package/dist/components/Business/StateFlow/index.d.ts +1 -2
- package/dist/components/Business/SystemLog/index.d.ts +78 -0
- package/dist/components/Business/TreeSearchSelect/index.d.ts +1 -1
- package/dist/components/Business/columnSettingTable/columnSetting.d.ts +9 -8
- package/dist/components/Business/columnSettingTable/components/TableSumComponent.d.ts +1 -2
- package/dist/components/Business/columnSettingTable/index.d.ts +21 -4
- package/dist/components/Business/columnSettingTable/sulaSettingTable.d.ts +22 -5
- package/dist/components/Business/columnSettingTable/utils.d.ts +1 -2
- package/dist/components/Business/moreTreeTable/FixedScrollBar.d.ts +1 -1
- package/dist/components/Common/ParagraphCopier/index.d.ts +1 -1
- package/dist/components/Common/Section/index.d.ts +1 -1
- package/dist/components/Functional/AddSelect/index.d.ts +1 -2
- package/dist/components/Functional/AuthButton/index.d.ts +1 -2
- package/dist/components/Functional/DataImport/index.d.ts +3 -3
- package/dist/components/Functional/DataValidation/index.d.ts +15 -5
- package/dist/components/Functional/ExportFunctions/ExportIcon/index.d.ts +1 -2
- package/dist/components/Functional/QueryMutipleInput/index.d.ts +3 -3
- package/dist/components/Functional/QueryMutipleSelect/index.d.ts +1 -2
- package/dist/components/Functional/SearchSelect/index.d.ts +1 -1
- package/dist/components/Functional/SearchSelect/utils.d.ts +4 -4
- package/dist/components/Functional/TreeSearchSelect/index.d.ts +1 -2
- package/dist/components/Solution/RuleComponent/CustomPlugin/CustomSelector/CustomSelectorModal.d.ts +1 -1
- package/dist/components/Solution/RuleComponent/CustomPlugin/CustomSelector/index.d.ts +1 -2
- package/dist/components/Solution/RuleComponent/Formula.d.ts +1 -2
- package/dist/components/Solution/RuleComponent/InnerSelect.d.ts +1 -2
- package/dist/components/Solution/RuleComponent/RenderCompItem.d.ts +1 -2
- package/dist/components/Solution/RuleSetter/RuleInstance.d.ts +1 -2
- package/dist/components/Solution/RuleSetter/baseRule.d.ts +1 -1
- package/dist/components/Solution/RuleSetter/index.d.ts +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.esm.js +4238 -2124
- package/dist/index.js +4247 -2122
- package/dist/plugin/TableColumnSetting/index.d.ts +6 -5
- package/dist/utils/RequestCacheManager.d.ts +82 -0
- package/dist/utils/TableUtils.d.ts +18 -19
- package/dist/utils/cachedRequest.d.ts +76 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/utils.d.ts +41 -0
- package/package.json +16 -14
- package/src/assets/btn-cancel.svg +3 -0
- package/src/assets/btn-copy.svg +3 -0
- package/src/assets/btn-delete.svg +2 -29
- package/src/assets/btn-edit.svg +2 -19
- package/src/assets/btn-hangUp.svg +3 -0
- package/src/assets/btn-print.svg +3 -0
- package/src/assets/btn-refresh.svg +3 -0
- package/src/assets/btn-unhook.svg +3 -0
- package/src/components/Business/AddSelectBusiness/index.md +8 -2
- package/src/components/Business/AddSelectBusiness/index.tsx +4 -1
- package/src/components/Business/BsLayouts/Components/ChooseStore/services.ts +1 -1
- package/src/components/Business/BsLayouts/Components/GlobalHeader/index.less +1 -1
- package/src/components/Business/BsLayouts/Components/RightContent/LoginModal.tsx +1 -1
- package/src/components/Business/BsLayouts/Components/RightContent/home.less +1 -1
- package/src/components/Business/BsLayouts/Components/SearchFunc/index.less +2 -2
- package/src/components/Business/BsLayouts/service.ts +1 -1
- package/src/components/Business/BsSulaQueryTable/SearchItemSetting.tsx +6 -5
- package/src/components/Business/BsSulaQueryTable/bssulaquerytable.less +0 -4
- package/src/components/Business/BsSulaQueryTable/index.less +5 -3
- package/src/components/Business/BsSulaQueryTable/index.md +45 -32
- package/src/components/Business/BsSulaQueryTable/index.tsx +154 -76
- package/src/components/Business/BsSulaQueryTable/setting.tsx +47 -11
- package/src/components/Business/BsSulaQueryTable/utils.less +1 -1
- package/src/components/Business/BsSulaQueryTable/utils.tsx +15 -12
- package/src/components/Business/CommodityEntry/index.md +1 -1
- package/src/components/Business/CommodityEntry/index.tsx +11 -9
- package/src/components/Business/DetailPageWrapper/index.less +1 -1
- package/src/components/Business/DetailPageWrapper/index.tsx +24 -5
- package/src/components/Business/DetailPageWrapper/utils.tsx +7 -2
- package/src/components/Business/HomePageWrapper/index.less +1 -1
- package/src/components/Business/ItemPropertySelector/index.tsx +88 -0
- package/src/components/Business/JsonQueryTable/function.ts +1 -1
- package/src/components/Business/JsonQueryTable/index.tsx +1 -1
- package/src/components/Business/ModalUtils/index.tsx +45 -0
- package/src/components/Business/PropertyModal/index.tsx +1 -1
- package/src/components/Business/SearchSelect/BusinessUtils.tsx +625 -76
- package/src/components/Business/SearchSelect/index.md +4 -4
- package/src/components/Business/SearchSelect/utils.ts +27 -5
- package/src/components/Business/SystemLog/index.md +37 -0
- package/src/components/Business/SystemLog/index.tsx +87 -0
- package/src/components/Business/columnSettingTable/columnSetting.tsx +28 -12
- package/src/components/Business/columnSettingTable/index.less +5 -3
- package/src/components/Business/columnSettingTable/index.md +200 -136
- package/src/components/Business/columnSettingTable/index.tsx +168 -39
- package/src/components/Business/columnSettingTable/sulaSettingTable.tsx +188 -56
- package/src/components/Functional/AddSelect/helps.ts +1 -1
- package/src/components/Functional/AddSelect/index.tsx +132 -13
- package/src/components/Functional/BillEntry/index.less +3 -0
- package/src/components/Functional/BillEntry/index.tsx +1 -1
- package/src/components/Functional/DataImport/index.tsx +1 -1
- package/src/components/Functional/DataValidation/index.md +1 -0
- package/src/components/Functional/DataValidation/index.tsx +161 -27
- package/src/components/Functional/ExportFunctions/ExportIcon/index.tsx +1 -1
- package/src/components/Functional/QueryMutipleInput/index.tsx +12 -3
- package/src/components/Functional/QueryMutipleSelect/index.tsx +3 -2
- package/src/components/Functional/SearchSelect/index.less +4 -0
- package/src/components/Functional/SearchSelect/index.tsx +68 -24
- package/src/components/Functional/SearchSelect/utils.tsx +17 -11
- package/src/components/Functional/TreeSearchSelect/index.tsx +3 -2
- package/src/components/Solution/RuleComponent/InnerSelect.tsx +1 -0
- package/src/components/Solution/RuleComponent/RenderCompItem.tsx +11 -2
- package/src/components/Solution/RuleComponent/index.js +75 -4
- package/src/components/Solution/RuleComponent/ruleFiled.js +26 -2
- package/src/components/Solution/RuleComponent/services.ts +1 -1
- package/src/components/Solution/RuleSetter/service.js +1 -1
- package/src/index.ts +3 -0
- package/src/plugin/TableColumnSetting/index.less +5 -3
- package/src/plugin/TableColumnSetting/index.tsx +80 -13
- package/src/styles/bsDefault.less +9 -3
- package/src/utils/RequestCacheManager.ts +213 -0
- package/src/utils/RequestCache_Examples.md +137 -0
- package/src/utils/RequestCache_README.md +170 -0
- package/src/utils/TableUtils.less +1 -1
- package/src/utils/cachedRequest.ts +288 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/request.ts +8 -4
- package/src/utils/requestUtils.ts +1 -1
- package/src/utils/utils.ts +52 -16
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 请求缓存管理器
|
|
3
|
+
* 用于业务组件库接口请求的缓存机制
|
|
4
|
+
* 规则:1秒内相同请求直接返回缓存数据并更新起止时间
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
interface CacheItem {
|
|
8
|
+
data: any;
|
|
9
|
+
timestamp: number;
|
|
10
|
+
startTime: number;
|
|
11
|
+
endTime: number;
|
|
12
|
+
requestKey: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface CacheConfig {
|
|
16
|
+
ttl: number; // 缓存有效期(毫秒)
|
|
17
|
+
maxSize: number; // 最大缓存数量
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class RequestCacheManager {
|
|
21
|
+
private cache: Map<string, CacheItem>;
|
|
22
|
+
private config: CacheConfig;
|
|
23
|
+
private static instance: RequestCacheManager;
|
|
24
|
+
// 新增:进行中请求追踪
|
|
25
|
+
private pendingMap: Map<string, Promise<any>>;
|
|
26
|
+
|
|
27
|
+
private constructor(config: CacheConfig = { ttl: 1000, maxSize: 100 }) {
|
|
28
|
+
this.cache = new Map();
|
|
29
|
+
this.config = config;
|
|
30
|
+
this.pendingMap = new Map();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 获取单例实例
|
|
35
|
+
*/
|
|
36
|
+
public static getInstance(config?: CacheConfig): RequestCacheManager {
|
|
37
|
+
if (!RequestCacheManager.instance) {
|
|
38
|
+
RequestCacheManager.instance = new RequestCacheManager(config);
|
|
39
|
+
}
|
|
40
|
+
return RequestCacheManager.instance;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 生成请求的唯一标识符
|
|
45
|
+
*/
|
|
46
|
+
private generateRequestKey(url: string, params?: any, method: string = 'GET'): string {
|
|
47
|
+
const sortedParams = this.sortParams(params);
|
|
48
|
+
return `${method.toUpperCase()}_${url}_${JSON.stringify(sortedParams)}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 对参数进行排序,确保相同参数不同顺序的请求被视为相同
|
|
53
|
+
*/
|
|
54
|
+
private sortParams(params: any): any {
|
|
55
|
+
if (!params || typeof params !== 'object') {
|
|
56
|
+
return params;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (Array.isArray(params)) {
|
|
60
|
+
return params.map(item => this.sortParams(item));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const sortedKeys = Object.keys(params).sort();
|
|
64
|
+
const sortedParams: any = {};
|
|
65
|
+
sortedKeys.forEach(key => {
|
|
66
|
+
sortedParams[key] = this.sortParams(params[key]);
|
|
67
|
+
});
|
|
68
|
+
return sortedParams;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 检查缓存是否有效
|
|
73
|
+
*/
|
|
74
|
+
private isCacheValid(cacheItem: CacheItem): boolean {
|
|
75
|
+
const now = Date.now();
|
|
76
|
+
return now - cacheItem.timestamp < this.config.ttl;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 清理过期缓存
|
|
81
|
+
*/
|
|
82
|
+
private cleanExpiredCache(): void {
|
|
83
|
+
const now = Date.now();
|
|
84
|
+
for (const [key, item] of this.cache.entries()) {
|
|
85
|
+
if (now - item.timestamp >= this.config.ttl) {
|
|
86
|
+
this.cache.delete(key);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 清理最旧的缓存
|
|
93
|
+
*/
|
|
94
|
+
private cleanOldestCache(): void {
|
|
95
|
+
if (this.cache.size >= this.config.maxSize) {
|
|
96
|
+
let oldestKey = '';
|
|
97
|
+
let oldestTime = Infinity;
|
|
98
|
+
|
|
99
|
+
for (const [key, item] of this.cache.entries()) {
|
|
100
|
+
if (item.timestamp < oldestTime) {
|
|
101
|
+
oldestTime = item.timestamp;
|
|
102
|
+
oldestKey = key;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (oldestKey) {
|
|
107
|
+
this.cache.delete(oldestKey);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 获取缓存数据
|
|
114
|
+
*/
|
|
115
|
+
public getCache(url: string, params?: any, method?: string): CacheItem | null {
|
|
116
|
+
const requestKey = this.generateRequestKey(url, params, method);
|
|
117
|
+
const cacheItem = this.cache.get(requestKey);
|
|
118
|
+
|
|
119
|
+
if (cacheItem && this.isCacheValid(cacheItem)) {
|
|
120
|
+
// 更新起止时间
|
|
121
|
+
const now = Date.now();
|
|
122
|
+
cacheItem.startTime = now;
|
|
123
|
+
cacheItem.endTime = now;
|
|
124
|
+
cacheItem.timestamp = now;
|
|
125
|
+
|
|
126
|
+
return { ...cacheItem };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 设置缓存数据
|
|
134
|
+
*/
|
|
135
|
+
public setCache(url: string, params: any, method: string, data: any, startTime: number, endTime: number): void {
|
|
136
|
+
const requestKey = this.generateRequestKey(url, params, method);
|
|
137
|
+
|
|
138
|
+
// 清理过期缓存
|
|
139
|
+
this.cleanExpiredCache();
|
|
140
|
+
|
|
141
|
+
// 清理最旧的缓存
|
|
142
|
+
this.cleanOldestCache();
|
|
143
|
+
|
|
144
|
+
const cacheItem: CacheItem = {
|
|
145
|
+
data,
|
|
146
|
+
timestamp: Date.now(),
|
|
147
|
+
startTime,
|
|
148
|
+
endTime,
|
|
149
|
+
requestKey
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
this.cache.set(requestKey, cacheItem);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* 新增:获取进行中请求
|
|
157
|
+
*/
|
|
158
|
+
public getPending(url: string, params?: any, method?: string): Promise<any> | undefined {
|
|
159
|
+
const requestKey = this.generateRequestKey(url, params, method);
|
|
160
|
+
return this.pendingMap.get(requestKey);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* 新增:设置进行中请求
|
|
165
|
+
*/
|
|
166
|
+
public setPending(url: string, params: any, method: string, promise: Promise<any>): void {
|
|
167
|
+
const requestKey = this.generateRequestKey(url, params, method);
|
|
168
|
+
this.pendingMap.set(requestKey, promise);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 新增:清理进行中请求
|
|
173
|
+
*/
|
|
174
|
+
public clearPending(url: string, params?: any, method?: string): void {
|
|
175
|
+
const requestKey = this.generateRequestKey(url, params, method);
|
|
176
|
+
this.pendingMap.delete(requestKey);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 清除指定缓存
|
|
181
|
+
*/
|
|
182
|
+
public clearCache(url?: string, params?: any, method?: string): void {
|
|
183
|
+
if (url) {
|
|
184
|
+
const requestKey = this.generateRequestKey(url, params, method);
|
|
185
|
+
this.cache.delete(requestKey);
|
|
186
|
+
this.pendingMap.delete(requestKey);
|
|
187
|
+
} else {
|
|
188
|
+
// 清除所有缓存
|
|
189
|
+
this.cache.clear();
|
|
190
|
+
this.pendingMap.clear();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* 获取缓存统计信息
|
|
196
|
+
*/
|
|
197
|
+
public getCacheStats(): {
|
|
198
|
+
size: number;
|
|
199
|
+
maxSize: number;
|
|
200
|
+
ttl: number;
|
|
201
|
+
pendingSize: number;
|
|
202
|
+
} {
|
|
203
|
+
return {
|
|
204
|
+
size: this.cache.size,
|
|
205
|
+
maxSize: this.config.maxSize,
|
|
206
|
+
ttl: this.config.ttl,
|
|
207
|
+
pendingSize: this.pendingMap.size,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export default RequestCacheManager;
|
|
213
|
+
export { RequestCacheManager };
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# 接口缓存系统使用示例
|
|
2
|
+
|
|
3
|
+
## 示例1: 基础替换使用
|
|
4
|
+
|
|
5
|
+
### 原始代码
|
|
6
|
+
```typescript
|
|
7
|
+
import request from "umi-request";
|
|
8
|
+
|
|
9
|
+
// 在组件中使用
|
|
10
|
+
const loadData = async () => {
|
|
11
|
+
const response = await request.get('/api/users', {
|
|
12
|
+
params: { page: 1, size: 10 }
|
|
13
|
+
});
|
|
14
|
+
return response.data;
|
|
15
|
+
};
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### 使用缓存后的代码
|
|
19
|
+
```typescript
|
|
20
|
+
import request from '@/utils/cachedRequest';
|
|
21
|
+
|
|
22
|
+
// 在组件中使用 - 完全相同的API
|
|
23
|
+
const loadData = async () => {
|
|
24
|
+
const response = await request.get('/api/users', {
|
|
25
|
+
params: { page: 1, size: 10 }
|
|
26
|
+
});
|
|
27
|
+
return response.data;
|
|
28
|
+
};
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## 示例2: SearchSelect组件集成
|
|
32
|
+
|
|
33
|
+
### 原始代码
|
|
34
|
+
```typescript
|
|
35
|
+
// src/components/Functional/SearchSelect/index.tsx
|
|
36
|
+
import request from "umi-request";
|
|
37
|
+
|
|
38
|
+
const getData = async (params) => {
|
|
39
|
+
let getRequest;
|
|
40
|
+
const methodName = method?.toLocaleLowerCase?.();
|
|
41
|
+
|
|
42
|
+
if(['post','patch','put'].includes(methodName)) {
|
|
43
|
+
getRequest = request[methodName](`${url}${convertUrlQueryParams(queryParams)}`,convertBodyParams(queryParams), { headers: { ...extralHeaders }})
|
|
44
|
+
} else {
|
|
45
|
+
getRequest = request.get( `${url}${convertUrlQueryParams(queryParams)}`,{headers: { ...extralHeaders }})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return getRequest.then((result: any) => {
|
|
49
|
+
setFetching(false)
|
|
50
|
+
result = result.data;
|
|
51
|
+
if (judgeIsRequestError(result)) {
|
|
52
|
+
message.error(result.msg);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const res = result.data;
|
|
56
|
+
// 处理数据...
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### 使用缓存后的代码
|
|
62
|
+
```typescript
|
|
63
|
+
// src/components/Functional/SearchSelect/index.tsx
|
|
64
|
+
import request from '@/utils/cachedRequest';
|
|
65
|
+
|
|
66
|
+
const getData = async (params) => {
|
|
67
|
+
let getRequest;
|
|
68
|
+
const methodName = method?.toLocaleLowerCase?.();
|
|
69
|
+
|
|
70
|
+
if(['post','patch','put'].includes(methodName)) {
|
|
71
|
+
getRequest = request[methodName](`${url}${convertUrlQueryParams(queryParams)}`,convertBodyParams(queryParams), { headers: { ...extralHeaders }})
|
|
72
|
+
} else {
|
|
73
|
+
getRequest = request.get( `${url}${convertUrlQueryParams(queryParams)}`,{headers: { ...extralHeaders }})
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return getRequest.then((result: any) => {
|
|
77
|
+
setFetching(false)
|
|
78
|
+
result = result.data;
|
|
79
|
+
if (judgeIsRequestError(result)) {
|
|
80
|
+
message.error(result.msg);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const res = result.data;
|
|
84
|
+
// 处理数据...
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## 示例3: 业务工具函数集成
|
|
90
|
+
|
|
91
|
+
### 原始代码
|
|
92
|
+
```typescript
|
|
93
|
+
// src/components/Business/SearchSelect/BusinessUtils.tsx
|
|
94
|
+
import { request } from 'bssula';
|
|
95
|
+
|
|
96
|
+
export const loadSelectSource = (url: string, params?: any, headers?: any) => {
|
|
97
|
+
return request(url, {
|
|
98
|
+
method: 'GET',
|
|
99
|
+
params,
|
|
100
|
+
headers
|
|
101
|
+
}).then(response => {
|
|
102
|
+
if (handleError(response)) {
|
|
103
|
+
return response.data;
|
|
104
|
+
}
|
|
105
|
+
return [];
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### 使用缓存后的代码
|
|
111
|
+
```typescript
|
|
112
|
+
// src/components/Business/SearchSelect/BusinessUtils.tsx
|
|
113
|
+
import request from '@/utils/cachedRequest';
|
|
114
|
+
|
|
115
|
+
export const loadSelectSource = (url: string, params?: any, headers?: any) => {
|
|
116
|
+
return request.get(url, {
|
|
117
|
+
params,
|
|
118
|
+
headers
|
|
119
|
+
}).then(response => {
|
|
120
|
+
if (handleError(response)) {
|
|
121
|
+
return response.data;
|
|
122
|
+
}
|
|
123
|
+
return [];
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## 示例4: 缓存管理
|
|
129
|
+
|
|
130
|
+
### 清除特定缓存
|
|
131
|
+
```typescript
|
|
132
|
+
import { clearRequestCache } from '@/utils/cachedRequest';
|
|
133
|
+
|
|
134
|
+
// 在数据变更操作后清除相关缓存
|
|
135
|
+
const handleDataChange = async () => {
|
|
136
|
+
// 执行数据变更操作
|
|
137
|
+
await updateUserData(newData
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# 业务组件库接口缓存系统
|
|
2
|
+
|
|
3
|
+
## 概述
|
|
4
|
+
|
|
5
|
+
本缓存系统为业务组件库的接口请求提供智能缓存机制,主要解决以下问题:
|
|
6
|
+
- 减少重复请求,提升性能
|
|
7
|
+
- 1秒内相同请求直接返回缓存数据
|
|
8
|
+
- 自动更新请求起止时间
|
|
9
|
+
- 不影响现有业务逻辑
|
|
10
|
+
|
|
11
|
+
## 核心特性
|
|
12
|
+
|
|
13
|
+
### 1. 智能缓存策略
|
|
14
|
+
- **缓存时间**: 1秒(可配置)
|
|
15
|
+
- **缓存容量**: 最大100个请求(可配置)
|
|
16
|
+
- **缓存键**: 基于URL、参数、请求方法生成
|
|
17
|
+
- **参数排序**: 自动排序参数,确保相同参数不同顺序的请求被视为相同
|
|
18
|
+
|
|
19
|
+
### 2. 自动时间更新
|
|
20
|
+
- 命中缓存时自动更新起止时间为当前时间
|
|
21
|
+
- 保持与真实请求相同的时间格式
|
|
22
|
+
|
|
23
|
+
### 3. 零侵入集成
|
|
24
|
+
- 完全兼容现有请求代码
|
|
25
|
+
- 支持umi-request的所有功能
|
|
26
|
+
- 提供清晰的缓存统计信息
|
|
27
|
+
|
|
28
|
+
## 使用方法
|
|
29
|
+
|
|
30
|
+
### 1. 基础使用
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
// 替换原有的request导入
|
|
34
|
+
import request from '@/utils/cachedRequest';
|
|
35
|
+
|
|
36
|
+
// 使用方式与原来完全一致
|
|
37
|
+
const response = await request.get('/api/data', { params: { id: 1 } });
|
|
38
|
+
const response2 = await request.post('/api/data', { data: { name: 'test' } });
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### 2. 高级使用
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import {
|
|
45
|
+
cachedRequest,
|
|
46
|
+
cachedGet,
|
|
47
|
+
cachedPost,
|
|
48
|
+
clearRequestCache,
|
|
49
|
+
getCacheStats
|
|
50
|
+
} from '@/utils/cachedRequest';
|
|
51
|
+
|
|
52
|
+
// 清除特定缓存
|
|
53
|
+
clearRequestCache('/api/data', { id: 1 }, 'GET');
|
|
54
|
+
|
|
55
|
+
// 清除所有缓存
|
|
56
|
+
clearRequestCache();
|
|
57
|
+
|
|
58
|
+
// 获取缓存统计信息
|
|
59
|
+
const stats = getCacheStats();
|
|
60
|
+
console.log(`缓存数量: ${stats.size}/${stats.maxSize}`);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 3. 自定义缓存配置
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import RequestCacheManager from '@/utils/RequestCacheManager';
|
|
67
|
+
|
|
68
|
+
// 创建自定义缓存管理器
|
|
69
|
+
const customCache = RequestCacheManager.getInstance({
|
|
70
|
+
ttl: 2000, // 2秒缓存时间
|
|
71
|
+
maxSize: 200 // 最大200个缓存
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 缓存机制详解
|
|
76
|
+
|
|
77
|
+
### 缓存键生成
|
|
78
|
+
```
|
|
79
|
+
缓存键 = METHOD_URL_排序后的参数JSON
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
例如:
|
|
83
|
+
```typescript
|
|
84
|
+
// 以下两个请求会被视为相同,生成相同的缓存键
|
|
85
|
+
request.get('/api/users', { params: { page: 1, size: 10 } })
|
|
86
|
+
request.get('/api/users', { params: { size: 10, page: 1 } })
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### 缓存生命周期
|
|
90
|
+
1. **请求发起**: 检查是否存在有效缓存
|
|
91
|
+
2. **缓存命中**: 直接返回缓存数据,更新时间戳
|
|
92
|
+
3. **缓存未命中**: 发送真实请求,缓存响应数据
|
|
93
|
+
4. **缓存过期**: 自动清理过期缓存(超过1秒)
|
|
94
|
+
5. **容量控制**: 超出最大容量时清理最旧的缓存
|
|
95
|
+
|
|
96
|
+
### 时间更新机制
|
|
97
|
+
当缓存命中时,系统会自动:
|
|
98
|
+
- 更新`startTime`为当前时间
|
|
99
|
+
- 更新`endTime`为当前时间
|
|
100
|
+
- 更新`timestamp`为当前时间
|
|
101
|
+
|
|
102
|
+
## 性能优化
|
|
103
|
+
|
|
104
|
+
### 1. 减少网络请求
|
|
105
|
+
- 1秒内相同请求复用缓存数据
|
|
106
|
+
- 特别适用于频繁请求的场景(如搜索、筛选)
|
|
107
|
+
|
|
108
|
+
### 2. 内存管理
|
|
109
|
+
- 自动清理过期缓存
|
|
110
|
+
- LRU算法清理最旧缓存
|
|
111
|
+
- 可配置最大缓存数量
|
|
112
|
+
|
|
113
|
+
### 3. 调试支持
|
|
114
|
+
- 控制台日志显示缓存命中/未命中
|
|
115
|
+
- 缓存统计信息实时监控
|
|
116
|
+
- 详细的请求时间记录
|
|
117
|
+
|
|
118
|
+
## 注意事项
|
|
119
|
+
|
|
120
|
+
### 1. 缓存适用场景
|
|
121
|
+
✅ **适合缓存**:
|
|
122
|
+
- 查询类接口(GET请求)
|
|
123
|
+
- 数据变化频率低的接口
|
|
124
|
+
- 参数相同的重复请求
|
|
125
|
+
|
|
126
|
+
❌ **不适合缓存**:
|
|
127
|
+
- 修改类接口(POST/PUT/DELETE)
|
|
128
|
+
- 实时性要求高的接口
|
|
129
|
+
- 用户特定数据的接口
|
|
130
|
+
|
|
131
|
+
### 2. 缓存配置建议
|
|
132
|
+
- **开发环境**: 可设置较长缓存时间便于调试
|
|
133
|
+
- **生产环境**: 建议保持1秒默认配置
|
|
134
|
+
- **高频请求**: 可适当增加最大缓存数量
|
|
135
|
+
|
|
136
|
+
### 3. 缓存清除时机
|
|
137
|
+
- 用户主动刷新页面时
|
|
138
|
+
- 数据变更操作后
|
|
139
|
+
- 特定业务场景需要时
|
|
140
|
+
|
|
141
|
+
## 监控与调试
|
|
142
|
+
|
|
143
|
+
### 控制台日志
|
|
144
|
+
```
|
|
145
|
+
[RequestCache] 命中缓存: /api/users {原始起止时间: 1234567890123 - 1234567890123, 更新时间: 1234567890123}
|
|
146
|
+
[RequestCache] 缓存请求: /api/users {起止时间: 1234567890123 - 1234567890123, 耗时: 45}
|
|
147
|
+
[RequestCache] 清除缓存: /api/users
|
|
148
|
+
[RequestCache] 缓存统计: {size: 5, maxSize: 100, ttl: 1000}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### 性能指标
|
|
152
|
+
- **缓存命中率**: 命中缓存的请求数 / 总请求数
|
|
153
|
+
- **平均响应时间**: 缓存命中时的响应时间(接近0ms)
|
|
154
|
+
- **缓存数量**: 当前缓存的请求数量
|
|
155
|
+
- **内存使用**: 缓存数据占用的内存大小
|
|
156
|
+
|
|
157
|
+
## 错误处理
|
|
158
|
+
|
|
159
|
+
缓存系统不会影响原有的错误处理机制:
|
|
160
|
+
- 请求失败时不会缓存错误响应
|
|
161
|
+
- 缓存系统异常不会影响真实请求
|
|
162
|
+
- 保持原有的错误提示和重试机制
|
|
163
|
+
|
|
164
|
+
## 扩展性
|
|
165
|
+
|
|
166
|
+
缓存系统设计考虑了未来的扩展需求:
|
|
167
|
+
- 支持自定义缓存策略
|
|
168
|
+
- 可扩展的缓存存储后端
|
|
169
|
+
- 灵活的配置选项
|
|
170
|
+
- 插件化的架构设计
|