소스 검색

添加根据父键批量删除的接口,避免重复调用删除接口触发IP限流

skywolf627 3 년 전
부모
커밋
9e46ae23a9

+ 2 - 0
Admin.NET/Admin.NET.Core/Service/Cache/ISysCacheService.cs

@@ -37,4 +37,6 @@ public interface ISysCacheService
     Task<int?> GetMaxDataScopeType(long userId);
 
     Task SetMaxDataScopeType(long userId, int dataScopeType);
+    
+    Task DelByParentKeyAsync(string key);
 }

+ 21 - 0
Admin.NET/Admin.NET.Core/Service/Cache/SysCacheService.cs

@@ -300,4 +300,25 @@ public class SysCacheService : ISysCacheService, IDynamicApiController, ISinglet
 
         await AddCacheKey(cacheKey);
     }
+
+    /// <summary>
+    ///  根据父键清空   
+    /// </summary>
+    /// <param name="key"></param>
+    /// <returns></returns>
+    [HttpGet("/sysCache/delByParentKey")]
+    public async Task DelByParentKeyAsync(string key)
+    {
+        var allkeys = await GetAllCacheKeys();
+        if (allkeys == null) return;
+
+        var delAllkeys = allkeys.Where(u => u.StartsWith(key)).ToList();
+        delAllkeys.ForEach(u =>
+        {
+            _cache.Remove(u);
+        });
+        // 更新所有缓存键
+        allkeys = allkeys.Where(u => !u.StartsWith(key)).ToList();
+        await _cache.SetStringAsync(CacheConst.KeyAll, JSON.Serialize(allkeys));
+    }
 }

+ 6 - 2
Vben2/src/api/sys/admin.ts

@@ -145,6 +145,7 @@ enum Api {
   GetAllCacheKeys = '/sysCache/keyList',
   GetStringAsync = '/sysCache/detail',
   RemoveAsync = '/sysCache/remove',
+  DelByParentKeyAsync = '/sysCache/delByParentKey',
 }
 
 ////////// 账号管理接口 //////////
@@ -458,9 +459,12 @@ export const getAllConstSelectorWithOptions = () =>
 ////////// 缓存管理接口 //////////
 // 获取所有缓存列表
 export const getAllCacheKeys = () => defHttp.get<any>({ url: Api.GetAllCacheKeys });
-// 根据类名获取下拉框数据
+// 根据键获取缓存数据
 export const getCacheStringAsync = (cacheKey?: string) =>
   defHttp.get<any>({ url: Api.GetStringAsync, params: { cacheKey } });
-// 获取所有下拉框及选项
+// 删除缓存
 export const removeCacheAsync = (key?: string) =>
   defHttp.get<any>({ url: Api.RemoveAsync, params: { key } });
+// 根据父键删除缓存
+export const delByParentKeyAsync = (key?: string) =>
+  defHttp.get<any>({ url: Api.DelByParentKeyAsync, params: { key } });

+ 7 - 5
Vben2/src/views/sys/admin/cache/index.vue

@@ -55,7 +55,12 @@
   import { PageWrapper } from '/@/components/Page';
   import { PopConfirmButton } from '/@/components/Button';
 
-  import { getAllCacheKeys, getCacheStringAsync, removeCacheAsync } from '/@/api/sys/admin';
+  import {
+    getAllCacheKeys,
+    getCacheStringAsync,
+    removeCacheAsync,
+    delByParentKeyAsync,
+  } from '/@/api/sys/admin';
   import { BasicTree, TreeActionType, TreeItem } from '/@/components/Tree/index';
   import { JsonPreview } from '/@/components/CodeEditor';
   import { ScrollContainer } from '/@/components/Container/index';
@@ -143,10 +148,7 @@
 
       async function onEmptyCache() {
         openFullLoading();
-        for (let i = 0; i < currentNode.value.children.length; i++) {
-          const e = currentNode.value.children[i];
-          await removeCacheAsync(e.key);
-        }
+        await delByParentKeyAsync(currentNode.value.key);
         closeFullLoading();
         currentNode.value = {};
         isJson.value = false;