REDIS12_Spring Cache概述、@Cacheable、@CacheEvict、@Caching、@CachePut的使用(五)

简介: REDIS12_Spring Cache概述、@Cacheable、@CacheEvict、@Caching、@CachePut的使用(五)

④. @CacheEvict、@Caching、@CachePut的使用


  • ①. @CacheEvict:触发将数据从缓存删除的操作(相当于失效模式)


  • ②. @Caching:组合以上多个操作


  • ③. @CachePut:不影响方法执行更新缓存(双写模式) 需要有返回值


    /**
     * 存储同一个类型的数据,都可以指定成同一个分区。分区名默认就是缓存的前缀
     * (我们需要在配置文件中将前缀的使用关闭)
     * @param category
     */
    //@CacheEvict(value = "category",key = "'getLevel1Category'")
//    @Caching(evict = {
//            @CacheEvict(value = "category",key = "'getLevel1Category'"),
//            @CacheEvict(value = "category",key = "'getCatalogJson'")
//    })
    //删除category分区中所有的数据
    //@CacheEvict(value = "category",allEntries = true)//失效模式
    @Transactional
    @Override
    public void updateCasCade(CategoryEntity category) {
        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
    }


//application.properties
spring.cache.type=redis
#spring.cache.cache-names=
#设置存活时间,ms为单位
spring.cache.redis.time-to-live=3600000
#如果指定了前缀,就使用我们指定的前缀,如果没有就默认使用缓存的名字作为前缀
#CACHE_getLevel1Category
#spring.cache.redis.key-prefix=CACHE_
#默认是使用前缀的
#如果开始了这个,那么缓存的key=getLevel1Category,没有CACHE
#spring.cache.redis.use-key-prefix=false
#是否缓存空值,防止缓存穿透
spring.cache.redis.cache-null-values=true


⑤. SpringCache原理与不足


  • ①. 读模式


缓存穿透:查询一个null数据。解决方案:缓存空数据,可通过spring.cache.redis.cache-null-values=true


缓存击穿:大量并发进来同时查询一个正好过期的数据。解决方案:加锁 ? 默认是无加锁的;

使用sync = true来解决击穿问题


缓存雪崩:大量的key同时过期。解决:加随机时间。


// 解决缓存击穿加锁 
@Cacheable(value={"category"},key = "#root.method.name",sync = true)


  • ②. 写模式(缓存与数据库一致)


  1. 读写加锁。


  1. 引入Canal,感知到MySQL的更新去更新Redis


  1. 读多写多,直接去数据库查询就行


  • ③. 总结


  1. 常规数据(读多写少,即时性,一致性要求不高的数据,完全可以使用Spring-Cache):


  1. 写模式(只要缓存的数据有过期时间就足够了)


相关文章
|
7月前
|
存储 缓存 NoSQL
Spring Cache缓存框架
Spring Cache是Spring体系下的标准化缓存框架,支持多种缓存(如Redis、EhCache、Caffeine),可独立或组合使用。其优势包括平滑迁移、注解与编程两种使用方式,以及高度解耦和灵活管理。通过动态代理实现缓存操作,适用于不同业务场景。
610 0
|
存储 缓存 NoSQL
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
272 2
|
NoSQL Unix 网络安全
【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')
【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')
181 0
【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')
|
缓存 弹性计算 NoSQL
【Azure Redis 缓存 Azure Cache For Redis】Redis连接池
【Azure Redis 缓存 Azure Cache For Redis】Redis连接池
176 0
|
存储 缓存 Java
Spring缓存注解【@Cacheable、@CachePut、@CacheEvict、@Caching、@CacheConfig】使用及注意事项
Spring缓存注解【@Cacheable、@CachePut、@CacheEvict、@Caching、@CacheConfig】使用及注意事项
4260 2
|
NoSQL 网络协议 Redis
【Azure Redis】AKS中使用Lettuce连接Redis Cache出现 timed out 问题的解决思路
【Azure Redis】AKS中使用Lettuce连接Redis Cache出现 timed out 问题的解决思路
422 1
【Azure Redis】AKS中使用Lettuce连接Redis Cache出现 timed out 问题的解决思路
|
Java 数据库连接 数据库
让星星⭐月亮告诉你,SSH框架01、Spring概述
Spring是一个轻量级的Java开发框架,旨在简化企业级应用开发。它通过IoC(控制反转)和DI(依赖注入)降低组件间的耦合度,支持AOP(面向切面编程),简化事务管理和数据库操作,并能与多种第三方框架无缝集成,提供灵活的Web层支持,是开发高性能应用的理想选择。
176 1
|
NoSQL Redis 容器
【Azure Cache for Redis】Redis的导出页面无法配置Storage SAS时通过az cli来完成
【Azure Cache for Redis】Redis的导出页面无法配置Storage SAS时通过az cli来完成
151 3
|
缓存 NoSQL Redis
【Azure Redis 缓存】Azure Cache for Redis 服务的导出RDB文件无法在自建的Redis服务中导入
【Azure Redis 缓存】Azure Cache for Redis 服务的导出RDB文件无法在自建的Redis服务中导入
168 0
|
缓存 开发框架 NoSQL
【Azure Redis 缓存】VM 里的 Redis 能直接迁移到 Azure Cache for Redis ? 需要改动代码吗?
【Azure Redis 缓存】VM 里的 Redis 能直接迁移到 Azure Cache for Redis ? 需要改动代码吗?
185 0