public class JetCacheUtil {
private JetCacheUtil() {
}
/**
* 删除缓存
**/
public static boolean removeCache(CacheLocate cacheLocate) {
Assert.isTrue(StringUtils.hasText(cacheLocate.getCacheKey()) && StringUtils.hasText(cacheLocate.getCacheName()) && null != cacheLocate.getCacheType(), "cacheName,cacheKey,cacheType,为必传项,请重新调用");
if (!exitCacheName(cacheLocate.getArea(), cacheLocate.getCacheName())) {
return false;
}
SpringConfigProvider springConfigProvider = (SpringConfigProvider) SpringContextHolder.getApplicationContext().getBean("springConfigProvider");
CacheManager cacheManager = springConfigProvider.getCacheManager();
// 如果根据 area 及 name 找不到 cache 返回false
LoadingCache cache;
try {
if (StringUtils.hasText(cacheLocate.getArea())) {
cache = (LoadingCache) cacheManager.getCache(cacheLocate.getArea(), cacheLocate.getCacheName());
} else {
cache = (LoadingCache) cacheManager.getCache(cacheLocate.getCacheName());
}
} catch (Exception e) {
log.error("RedisCacheHelper.removeLocalCache cache definition not found:{}", cacheLocate, e);
return false;
}
return handleRemove(cacheLocate, cache);
}
/**
* 判断当前是否存在cacheName
*
* @param area area
* @param cacheName cacheName
* @return true:存在 false:不存在
*/
private static boolean exitCacheName(String area, String cacheName) {
ApplicationContext applicationContext = SpringContextHolder.getApplicationContext();
if (applicationContext.getBeanNamesForType(ConfigMap.class).length > 0) {
area = StringUtils.hasText(area) ? area : "default";
ConfigMap jetCacheConfigMap = applicationContext.getBean(ConfigMap.class);
return Objects.nonNull(jetCacheConfigMap.getByCacheName(area, cacheName));
}
return false;
}
/**
* 删除缓存 - 具体操作
**/
private static boolean handleRemove(CacheLocate cacheLocate, LoadingCache cache) {
// 开始缓存操作
Cache targetCache = cache.getTargetCache();
// 如果只删除本地缓存
if (cacheLocate.getCacheType().equals(CacheType.LOCAL)) {
//【多级缓存类型】
if (targetCache instanceof MultiLevelCache) {
for (Cache cach : ((MultiLevelCache<?, ?>) targetCache).caches()) {
// 如果是本地缓存类型 - 进行删除
if (cach instanceof AbstractEmbeddedCache) {
return cach.remove(cacheLocate.getCacheKey());
}
}
}
//【本地缓存类型】
if (targetCache instanceof AbstractEmbeddedCache) {
return targetCache.remove(cacheLocate.getCacheKey());
}
}
// 如果删除两端缓存
if (cacheLocate.getCacheType().equals(CacheType.BOTH)) {
return cache.remove(cacheLocate.getCacheKey());
}
// 如果只删除远端缓存
if (cacheLocate.getCacheType().equals(CacheType.REMOTE)) {
//【多级缓存类型】
if (targetCache instanceof MultiLevelCache) {
for (Cache cach : ((MultiLevelCache<?, ?>) targetCache).caches()) {
// 如果是远程缓存类型 - 进行删除
if (cach instanceof AbstractExternalCache) {
return cach.remove(cacheLocate.getCacheKey());
}
}
}
//【远程缓存类型】
if (targetCache instanceof AbstractExternalCache) {
return targetCache.remove(cacheLocate.getCacheKey());
}
}
return true;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class CacheLocate {
private String area;
private String cacheName;
private String cacheKey;
private CacheType cacheType;
}
}
标签:JetCacheUtil,缓存,return,cacheLocate,cache,area,targetCache,远端
From: https://www.cnblogs.com/gustavo/p/18024830