blob: af6147a9f253ffe95b7ba4950d91a223622e8c06 [file] [log] [blame]
Jiarenxiang38dcb052025-03-13 16:40:09 +08001import React, { useEffect, useState, useRef } from 'react';
2import { clearCacheAll, clearCacheKey, clearCacheName, getCacheValue, listCacheKey, listCacheName } from '@/services/monitor/cachelist';
3import { Button, Card, Col, Form, FormInstance, Input, message, Row, Table } from 'antd';
4import styles from './index.less';
5import { FormattedMessage } from '@umijs/max';
6import { ReloadOutlined } from '@ant-design/icons';
7import { ProForm } from '@ant-design/pro-components';
8
9const { TextArea } = Input;
10
11
12/* *
13 *
14 * @author whiteshader@163.com
15 * @datetime 2022/06/27
16 *
17 * */
18
19
20
21const CacheList: React.FC = () => {
22 const [cacheNames, setCacheNames] = useState<any>([]);
23 const [currentCacheName, setCurrentCacheName] = useState<any>([]);
24 const [cacheKeys, setCacheKeys] = useState<any>([]);
25 const [form] = Form.useForm();
26
27 const getCacheNames = () => {
28 listCacheName().then((res) => {
29 if (res.code === 200) {
30 setCacheNames(res.data);
31 }
32 });
33 }
34
35 useEffect(() => {
36 getCacheNames();
37 }, []);
38
39 const getCacheKeys = (cacheName: string) => {
40 listCacheKey(cacheName).then(res => {
41 if (res.code === 200) {
42 let index = 0;
43 const keysData = res.data.map((item: any) => {
44 return {
45 index: index++,
46 cacheKey: item
47 }
48 })
49 setCacheKeys(keysData);
50 }
51 });
52 };
53
54 const onClearAll = async () => {
55 clearCacheAll().then(res => {
56 if(res.code === 200) {
57 message.success("清理全部缓存成功");
58 }
59 });
60 };
61
62 const onClearAllFailed = (errorInfo: any) => {
63 message.error('Failed:', errorInfo);
64 };
65
66 const refreshCacheNames = () => {
67 getCacheNames();
68 message.success("刷新缓存列表成功");
69 };
70
71 const refreshCacheKeys = () => {
72 getCacheKeys(currentCacheName);
73 message.success("刷新键名列表成功");
74 };
75
76 const columns = [
77 {
78 title: '缓存名称',
79 dataIndex: 'cacheName',
80 key: 'cacheName',
81 render: (_: any, record: any) => {
82 return record.cacheName.replace(":", "");
83 }
84 },
85 {
86 title: '备注',
87 dataIndex: 'remark',
88 key: 'remark',
89 },
90 {
91 title: <FormattedMessage id="pages.searchTable.titleOption" defaultMessage="操作" />,
92 dataIndex: 'option',
93 width: '40px',
94 valueType: 'option',
95 render: (_: any, record: API.Monitor.CacheContent) => [
96 <Button
97 type="link"
98 size="small"
99 key="remove"
100 danger
101 onClick={() => {
102 clearCacheName(record.cacheName).then(res => {
103 if(res.code === 200) {
104 message.success("清理缓存名称[" + record.cacheName + "]成功");
105 getCacheKeys(currentCacheName);
106 }
107 });
108 }}
109 >
110 <FormattedMessage id="pages.searchTable.delete" defaultMessage="删除" />
111 </Button>,
112 ]
113 }
114 ];
115
116 const cacheKeysColumns = [
117 {
118 title: '序号',
119 dataIndex: 'index',
120 key: 'index'
121 },
122 {
123 title: '缓存键名',
124 dataIndex: 'cacheKey',
125 key: 'cacheKey',
126 render: (_: any, record: any) => {
127 return record.cacheKey.replace(currentCacheName, "");
128 }
129 },
130 {
131 title: <FormattedMessage id="pages.searchTable.titleOption" defaultMessage="操作" />,
132 dataIndex: 'option',
133 width: '40px',
134 valueType: 'option',
135 render: (_: any, record: API.Monitor.CacheContent) => [
136 <Button
137 type="link"
138 size="small"
139 key="remove"
140 danger
141 onClick={() => {
142 console.log(record)
143 clearCacheKey(record.cacheKey).then(res => {
144 if(res.code === 200) {
145 message.success("清理缓存键名[" + record.cacheKey + "]成功");
146 getCacheKeys(currentCacheName);
147 }
148 });
149 }}
150 >
151 <FormattedMessage id="pages.searchTable.delete" defaultMessage="删除" />
152 </Button>,
153 ]
154 }
155 ];
156
157 return (
158 <div>
159 <Row gutter={[24, 24]}>
160 <Col span={8}>
161 <Card title="缓存列表" extra={<Button icon={<ReloadOutlined />} onClick={()=>{ refreshCacheNames()}} type="link" />} className={styles.card}>
162 <Table
163 rowKey="cacheName"
164 dataSource={cacheNames}
165 columns={columns}
166 onRow={(record: API.Monitor.CacheContent) => {
167 return {
168 onClick: () => {
169 setCurrentCacheName(record.cacheName);
170 getCacheKeys(record.cacheName);
171 },
172 };
173 }}
174 />
175 </Card>
176 </Col>
177 <Col span={8}>
178 <Card title="键名列表" extra={<Button icon={<ReloadOutlined />} onClick={()=>{ refreshCacheKeys()}} type="link" />} className={styles.card}>
179 <Table
180 rowKey="index"
181 dataSource={cacheKeys}
182 columns={cacheKeysColumns}
183 onRow={(record: any) => {
184 return {
185 onClick: () => {
186 getCacheValue(currentCacheName, record.cacheKey).then(res => {
187 if (res.code === 200) {
188 form.resetFields();
189 form.setFieldsValue({
190 cacheName: res.data.cacheName,
191 cacheKey: res.data.cacheKey,
192 cacheValue: res.data.cacheValue,
193 remark: res.data.remark,
194 });
195 }
196 });
197 },
198 };
199 }}
200 />
201 </Card>
202 </Col>
203 <Col span={8}>
204 <Card title="缓存内容" extra={<Button icon={<ReloadOutlined />} onClick={()=>{ onClearAll()}} type="link" >清理全部</Button>} className={styles.card}>
205 <ProForm
206 name="basic"
207 form={form}
208 labelCol={{ span: 8 }}
209 wrapperCol={{ span: 16 }}
210 onFinish={onClearAll}
211 onFinishFailed={onClearAllFailed}
212 autoComplete="off"
213 >
214 <Form.Item
215 label="缓存名称"
216 name="cacheName"
217 >
218 <Input />
219 </Form.Item>
220 <Form.Item
221 label="缓存键名"
222 name="cacheKey"
223 >
224 <Input />
225 </Form.Item>
226 <Form.Item
227 label="缓存内容"
228 name="cacheValue"
229 >
230 <TextArea autoSize={{ minRows: 2 }} />
231 </Form.Item>
232 </ProForm>
233 </Card>
234 </Col>
235 </Row>
236 </div>
237 );
238};
239
240export default CacheList;