system cache

This commit is contained in:
yandanyang 2021-10-11 20:31:11 +08:00
parent d0086cf68d
commit 7295b90320
3 changed files with 111 additions and 0 deletions

View File

@ -51,5 +51,7 @@ public class SwaggerTagConst {
public static final String MANAGER_CATEGORY = "管理端-分类"; public static final String MANAGER_CATEGORY = "管理端-分类";
public static final String MANAGER_GOODS = "管理端-商品业务"; public static final String MANAGER_GOODS = "管理端-商品业务";
public static final String MANAGER_SYSTEM_CACHE = "管理端-系统缓存";
} }
} }

View File

@ -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<List<String>> cacheNames() {
return ResponseDTO.ok(systemCacheService.cacheNames());
}
@ApiOperation(value = "移除某个缓存", notes = "@author 罗伊")
@GetMapping("/cache/remove/{cacheName}")
public ResponseDTO<String> removeCache(@PathVariable String cacheName) {
systemCacheService.removeCache(cacheName);
return ResponseDTO.ok();
}
@ApiOperation(value = "获取某个缓存的所有key", notes = "@author 罗伊")
@GetMapping("/cache/keys/{cacheName}")
public ResponseDTO<List<String>> cacheKeys(@PathVariable String cacheName) {
return ResponseDTO.ok(systemCacheService.cacheKey(cacheName));
}
}

View File

@ -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<String> 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<String> cacheKey(String cacheName){
CaffeineCache cache = (CaffeineCache) caffeineCacheManager.getCache(cacheName);
if(cache == null){
return Lists.newArrayList();
}
Set<Object> cacheKey = cache.getNativeCache().asMap().keySet();
return cacheKey.stream().map(e->e.toString()).collect(Collectors.toList());
}
}