首页 > 其他分享 >JetCacheUtil 删除本地及远端缓存

JetCacheUtil 删除本地及远端缓存

时间:2024-02-21 11:33:04浏览次数:18  
标签:JetCacheUtil 缓存 return cacheLocate cache area targetCache 远端

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

相关文章

  • 面试官:如何实现多级缓存?
    对于高并发系统来说,有三个重要的机制来保障其高效运行,它们分别是:缓存、限流和熔断。而缓存是排在最前面也是高并发系统之所以高效运行的关键手段,那么问题来了:缓存只使用Redis就够了吗?1.冗余设计理念当然不是,不要把所有鸡蛋放到一个篮子里,成熟的系统在关键功能实现时一定会考......
  • 认识Redis:不只是缓存,还有这些厉害的功能!
    在当今数据驱动的世界中,快速存取信息成为了技术发展的关键。而在众多存储解决方案中,Redis以其独特的魅力和强大的功能,成为了开发者们的宠儿。今天,就让我们一起来认识一下Redis。一、Redis是什么,可以用来干什么?Redis,英文全称是RemoteDictionaryServer(远程字典服务),是一个开源......
  • redis高频问题--缓存--数据淘汰策略
    redis-数据淘汰策略redis具体的有八种淘汰策略数据淘汰策略建议总结问答......
  • redis高频问题--缓存--数据过期策略
    redis的数据过期策略惰性删除定期删除总结回答......
  • redis高频问题--缓存击穿
    缓存击穿互斥锁==分布式锁互斥锁多用于关于钱的业务,保持强一致性性能差一些,因为需要互相等待逻辑过期保证高可用性,注重于用户的体验......
  • Vulkan中的同步与缓存控制
    1.IntroductionVulkan提供显式的同步结构,允许CPU与GPU同步命令的执行。并且还可以控制GPU中命令的执行顺序。所有执行的Vulkan命令都将进入队列,并以某种未定义的顺序“不间断”执行。有时,我们明确希望在执行新操作之前确保某些操作已完成。在编写vulkan应用时,虽然对给......
  • 分布式缓存应用:Memcache 或 Redis
    为什么要使用分布式缓存高并发环境下,例如典型的淘宝双11秒杀,几分钟内上亿的用户涌入淘宝,这个时候如果访问不加拦截,让大量的读写请求涌向数据库,由于磁盘的处理速度与内存显然不在一个量级,服务器马上就要宕机。缓存可以将经常读取的数据存储在快速的内存中,从而避免了频繁访问慢速......
  • 通过注解实现本地缓存caffeine的学习
    注解源码如下1@Target(ElementType.METHOD)2@Retention(RetensionPolicy.RUNTIME)3@Documented4public@interfaceRvcCache{5Strngkey();6Stringid()defaultStringUtils.EMPTY;7}1@Component2@Aspect3@RequiredArgsConstructor4......
  • 【性能测试】MYSQL缓存命中率03
    一、查询缓存(querycache) 缓存命中率:所有的查询语句,命中缓存的请求数,占所有请求数的比例查看是否开启缓存命中率#缓存的开关showvariableslike'%query_cache_type%';#缓存的大小showvariableslike'%query_cache_size%';开启缓存设置MySQL的配置文件my......
  • dotnet aspnet redis 缓存 分布式缓存
    分布式缓存\appsettings.Development.json{"Logging":{"LogLevel":{"Default":"Information","Microsoft.AspNetCore":"Warning"}}}分布式缓存\appsettings.json{"Logg......