
JetCacheUtil 删除本地及远端缓存 。。。
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();
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);
}
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;
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析