From 7295b90320a1fac386fc898f68088b230f152b17 Mon Sep 17 00:00:00 2001 From: yandanyang Date: Mon, 11 Oct 2021 20:31:11 +0800 Subject: [PATCH] system cache --- .../common/swagger/SwaggerTagConst.java | 2 + .../systemcache/SystemCacheController.java | 48 +++++++++++++++ .../systemcache/SystemCacheService.java | 61 +++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/systemcache/SystemCacheController.java create mode 100644 admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/systemcache/SystemCacheService.java diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/common/swagger/SwaggerTagConst.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/common/swagger/SwaggerTagConst.java index 3162747e..7e6f0923 100644 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/common/swagger/SwaggerTagConst.java +++ b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/common/swagger/SwaggerTagConst.java @@ -51,5 +51,7 @@ public class SwaggerTagConst { public static final String MANAGER_CATEGORY = "管理端-分类"; public static final String MANAGER_GOODS = "管理端-商品业务"; + + public static final String MANAGER_SYSTEM_CACHE = "管理端-系统缓存"; } } diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/systemcache/SystemCacheController.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/systemcache/SystemCacheController.java new file mode 100644 index 00000000..baada088 --- /dev/null +++ b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/systemcache/SystemCacheController.java @@ -0,0 +1,48 @@ +package net.lab1024.smartadmin.service.module.system.systemcache; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import net.lab1024.smartadmin.service.common.domain.ResponseDTO; +import net.lab1024.smartadmin.service.common.swagger.SwaggerTagConst; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +/** + * [ ] + * + * @author yandanyang + * @date 2021/10/11 20:07 + */ +@RestController +@Api(tags = {SwaggerTagConst.Admin.MANAGER_SYSTEM_CACHE}) +public class SystemCacheController { + + @Autowired + private SystemCacheService systemCacheService; + + @ApiOperation(value = "获取所有缓存", notes = "@author 罗伊") + @GetMapping("/cache/names") + public ResponseDTO> cacheNames() { + return ResponseDTO.ok(systemCacheService.cacheNames()); + } + + + @ApiOperation(value = "移除某个缓存", notes = "@author 罗伊") + @GetMapping("/cache/remove/{cacheName}") + public ResponseDTO removeCache(@PathVariable String cacheName) { + systemCacheService.removeCache(cacheName); + return ResponseDTO.ok(); + } + + + @ApiOperation(value = "获取某个缓存的所有key", notes = "@author 罗伊") + @GetMapping("/cache/keys/{cacheName}") + public ResponseDTO> cacheKeys(@PathVariable String cacheName) { + return ResponseDTO.ok(systemCacheService.cacheKey(cacheName)); + } + +} diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/systemcache/SystemCacheService.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/systemcache/SystemCacheService.java new file mode 100644 index 00000000..d9805637 --- /dev/null +++ b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/systemcache/SystemCacheService.java @@ -0,0 +1,61 @@ +package net.lab1024.smartadmin.service.module.system.systemcache; + +import com.google.common.collect.Lists; +import net.lab1024.smartadmin.service.module.support.reload.core.annoation.SmartReload; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.caffeine.CaffeineCache; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * [ ] + * + * @author yandanyang + * @date 2021/10/11 20:07 + */ +@Service +public class SystemCacheService { + + @Resource + private CaffeineCacheManager caffeineCacheManager; + + /** + * 获取所有缓存名称 + * @return + */ + public List cacheNames(){ + return Lists.newArrayList(caffeineCacheManager.getCacheNames()); + } + + + /** + * 移除某个key + * @param cacheName + */ + @SmartReload("removeCache") + public void removeCache(String cacheName){ + CaffeineCache cache = (CaffeineCache) caffeineCacheManager.getCache(cacheName); + if(cache != null){ + cache.clear(); + } + } + + /** + * 某个缓存下的所有key + * @param cacheName + * @return + */ + public List cacheKey(String cacheName){ + CaffeineCache cache = (CaffeineCache) caffeineCacheManager.getCache(cacheName); + if(cache == null){ + return Lists.newArrayList(); + } + Set cacheKey = cache.getNativeCache().asMap().keySet(); + return cacheKey.stream().map(e->e.toString()).collect(Collectors.toList()); + } +}