page.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. "use client";
  2. import { useEffect, useState } from "react";
  3. import { useRouter } from "next/navigation";
  4. import { ReloadOutlined, ClearOutlined } from "@ant-design/icons";
  5. import {
  6. Row,
  7. Col,
  8. Tooltip,
  9. Table,
  10. Button,
  11. Form,
  12. Input,
  13. Flex,
  14. message,
  15. } from "antd";
  16. import { DeleteOutlined } from "@ant-design/icons";
  17. import { PageContainer, ProCard } from "@ant-design/pro-components";
  18. import {
  19. faFloppyDisk,
  20. faKey,
  21. faFileLines,
  22. } from "@fortawesome/free-solid-svg-icons";
  23. import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
  24. import { fetchApi } from "@/app/_modules/func";
  25. const { TextArea } = Input;
  26. //查询缓存API
  27. const queryCacheAPI = "/api/monitor/cache/getNames";
  28. //删除指定的缓存API
  29. const deleteCacheAPI = "/api/monitor/cache/clearCacheName";
  30. //查询缓存Key API
  31. const queryCacheKeyAPI = "/api/monitor/cache/getKeys";
  32. //查询缓存值API
  33. const queryValueAPI = "/api/monitor/cache/getValue";
  34. //删除指定的Key API
  35. const deleteKeyAPI = "/api/monitor/cache/clearCacheKey";
  36. //清空所有缓存API
  37. const clearAPI = "/api/monitor/cache/clearCacheAll";
  38. export default function CacheList() {
  39. const { push } = useRouter();
  40. //缓存列表加载状态
  41. const [isCacheLoading, setIsCacheLoading] = useState(false);
  42. //缓存数据
  43. const [cacheData, setCacheData] = useState([]);
  44. //查询缓存
  45. const queryCache = async () => {
  46. setIsCacheLoading(true);
  47. const body = await fetchApi(queryCacheAPI, push);
  48. if (body !== undefined) {
  49. if (body.code == 200) {
  50. setCacheData(body.data);
  51. }
  52. }
  53. setIsCacheLoading(false);
  54. };
  55. useEffect(() => {
  56. queryCache();
  57. }, []);
  58. //缓存列定义
  59. const cacheColumns = [
  60. {
  61. title: "序号",
  62. key: "index",
  63. width: "60px",
  64. render: (text: any, record: any, index: number) => `${index + 1}`,
  65. },
  66. {
  67. title: "缓存名称",
  68. dataIndex: "cacheName",
  69. ellipsis: true,
  70. },
  71. {
  72. title: "备注",
  73. dataIndex: "remark",
  74. },
  75. {
  76. title: "操作",
  77. key: "action",
  78. render: (text: any, record: any) => [
  79. <Button
  80. key="deleteBtn"
  81. type="link"
  82. danger
  83. icon={<DeleteOutlined />}
  84. onClick={() => deleteCache(record.cacheName)}
  85. ></Button>,
  86. ],
  87. },
  88. ];
  89. //缓存列表行点击
  90. const cacheRowClick = (record: any, index: any) => {
  91. return {
  92. onMouseDown: (event: any) => {
  93. setSelectedCache(record.cacheName);
  94. queryKeys(record.cacheName);
  95. },
  96. };
  97. };
  98. //删除指定的缓存
  99. const deleteCache = async (cacheName: string) => {
  100. const body = await fetchApi(`${deleteCacheAPI}/${cacheName}}`, push, {
  101. method: "DELETE",
  102. });
  103. if (body != undefined) {
  104. if (body.code == 200) {
  105. message.success(`清理缓存[${cacheName}]成功`);
  106. queryCache();
  107. } else {
  108. message.error(body.msg);
  109. }
  110. }
  111. };
  112. //选中的缓存
  113. const [selectedCache, setSelectedCache] = useState("");
  114. //key列表加载状态
  115. const [isKeyLoading, setIsKeyLoading] = useState(false);
  116. //key 数据
  117. const [keyData, setKeyData] = useState([]);
  118. //查询缓存对应的key
  119. const queryKeys = async (cacheName?: string) => {
  120. if (cacheName === undefined) {
  121. if (selectedCache === "") {
  122. return;
  123. }
  124. cacheName = selectedCache;
  125. }
  126. setIsKeyLoading(true);
  127. const body = await fetchApi(`${queryCacheKeyAPI}/${cacheName}`, push);
  128. if (body !== undefined) {
  129. if (body.code == 200) {
  130. setKeyData(body.data);
  131. }
  132. }
  133. setIsKeyLoading(false);
  134. };
  135. //key列定义
  136. const keyColumns = [
  137. {
  138. title: "序号",
  139. key: "index",
  140. width: "60px",
  141. render: (text: any, record: any, index: number) => `${index + 1}`,
  142. },
  143. {
  144. title: "缓存键名",
  145. key: "key",
  146. ellipsis: true,
  147. render: (text: any, record: string) => record.split(":")[1],
  148. },
  149. {
  150. title: "操作",
  151. key: "action",
  152. render: (text: any, record: any) => [
  153. <Button
  154. key="deleteBtn"
  155. type="link"
  156. danger
  157. icon={<DeleteOutlined />}
  158. onClick={() => deleteKey(record)}
  159. ></Button>,
  160. ],
  161. },
  162. ];
  163. //key列表行点击
  164. const keyRowClick = (record: any, index: any) => {
  165. return {
  166. onClick: (event: any) => {
  167. queryValue(record);
  168. },
  169. };
  170. };
  171. //删除指定的key
  172. const deleteKey = async (key: string) => {
  173. const body = await fetchApi(`${deleteKeyAPI}/${key}`, push, {
  174. method: "DELETE",
  175. });
  176. if (body != undefined) {
  177. if (body.code == 200) {
  178. message.success(`清理缓存键名[${key}]成功`);
  179. queryKeys();
  180. } else {
  181. message.error(body.msg);
  182. }
  183. }
  184. };
  185. //缓存值展示表单
  186. const [valueForm] = Form.useForm();
  187. //查询值
  188. const queryValue = async (key: any) => {
  189. const body = await fetchApi(
  190. `${queryValueAPI}/${selectedCache}/${key}`,
  191. push
  192. );
  193. if (body !== undefined) {
  194. if (body.code == 200) {
  195. valueForm?.setFieldsValue({
  196. cacheName: body.data.cacheName,
  197. cacheKey: body.data.cacheKey,
  198. cacheValue: body.data.cacheValue,
  199. });
  200. }
  201. }
  202. };
  203. //清空全部缓存
  204. const clearCache = async () => {
  205. const body = await fetchApi(clearAPI, push, {
  206. method: "DELETE",
  207. });
  208. if (body !== undefined) {
  209. if (body.code == 200) {
  210. message.success("清空全部缓存成功");
  211. } else {
  212. message.error(body.msg);
  213. }
  214. } else {
  215. message.error("清空全部缓存异常");
  216. }
  217. };
  218. return (
  219. <PageContainer title={false}>
  220. <Flex justify="flex-end" style={{ marginBottom: 16 }}>
  221. <Tooltip title="清空全部缓存">
  222. <Button
  223. icon={<ClearOutlined />}
  224. type="primary"
  225. onClick={clearCache}
  226. />
  227. </Tooltip>
  228. </Flex>
  229. <Row gutter={8}>
  230. <Col span={8}>
  231. <ProCard
  232. title={
  233. <>
  234. <FontAwesomeIcon icon={faFloppyDisk} />
  235. <span style={{ marginLeft: 6 }}>缓存列表</span>
  236. </>
  237. }
  238. extra={
  239. <Tooltip title="刷新">
  240. <a onClick={queryCache}>
  241. <ReloadOutlined />
  242. </a>
  243. </Tooltip>
  244. }
  245. style={{ height: "100%" }}
  246. headerBordered
  247. bordered
  248. hoverable
  249. >
  250. <div style={{ overflowX: "auto" }}>
  251. <Table
  252. dataSource={cacheData}
  253. columns={cacheColumns}
  254. loading={isCacheLoading}
  255. onRow={cacheRowClick}
  256. />
  257. </div>
  258. </ProCard>
  259. </Col>
  260. <Col span={8}>
  261. <ProCard
  262. title={
  263. <>
  264. <FontAwesomeIcon icon={faKey} />
  265. <span style={{ marginLeft: 6 }}>键名列表</span>
  266. </>
  267. }
  268. extra={
  269. <Tooltip title="刷新">
  270. <a onClick={() => queryKeys()}>
  271. <ReloadOutlined />
  272. </a>
  273. </Tooltip>
  274. }
  275. style={{ height: "100%" }}
  276. headerBordered
  277. bordered
  278. hoverable
  279. >
  280. <div style={{ overflowX: "auto" }}>
  281. <Table
  282. dataSource={keyData}
  283. columns={keyColumns}
  284. loading={isKeyLoading}
  285. onRow={keyRowClick}
  286. />
  287. </div>
  288. </ProCard>
  289. </Col>
  290. <Col span={8}>
  291. <ProCard
  292. title={
  293. <>
  294. <FontAwesomeIcon icon={faFileLines} />
  295. <span style={{ marginLeft: 6 }}>缓存内容</span>
  296. </>
  297. }
  298. style={{ height: "100%" }}
  299. headerBordered
  300. bordered
  301. hoverable
  302. >
  303. <Form layout="vertical" form={valueForm}>
  304. <Form.Item label="缓存名称" name="cacheName">
  305. <Input readOnly />
  306. </Form.Item>
  307. <Form.Item label="缓存键名" name="cacheKey">
  308. <Input readOnly />
  309. </Form.Item>
  310. <Form.Item label="缓存内容" name="cacheValue">
  311. <TextArea readOnly rows={8} />
  312. </Form.Item>
  313. </Form>
  314. </ProCard>
  315. </Col>
  316. </Row>
  317. </PageContainer>
  318. );
  319. }