整合SpringCache

整合SpringCache

1、引入依赖cache还有redis

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

2、写配置

spring:
 cache:
  type: redis
  

3、测试使用缓存

@Cacheable:触发将数据保存到缓存的操作
@CacheEvict:触发将数据从缓存中删除的操作
@CachePut:不影响方法更新缓存
@Caching:组合以上多个操作
@CacheConfig:在类级别共享缓存的相同配置

1.开启缓存注解@EnableCaching

//指定缓存的名字
@Cacheable({"category"})  //代表当前的方法是需要缓存的,如果缓存中有,方法不调用,如果缓存中没有,则调用方法,最后将方法的结果放入缓存

默认行为:

key是默认自动生成的

缓存的value的值,默认使用jdk序列化机制,将序列化后的数据存到redis中

默认ttl时间-1

自定义操作:

1、指定key:key属性指定,接受一个

2、指定缓存的存活时间

3、将数据保存为json

spring:
 cache:
   type: redis
     redis:
       time-to-live: 60000   #毫秒
@Cacheable(value = {"category"},key = "'level1Cateogrys'")

修改保存为json格式

创建配置类

@EnableConfigurationProperties(CacheProperties.class)
@Configuration
@EnableCaching
public class MyCacheConfig {
    @Autowired
    CacheProperties cacheProperties;


    @Bean
    RedisCacheConfiguration redisCacheConfiguration(){

        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();

        //key的序列化机制
        config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));

        //Value的序列化机制
        config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));


        CacheProperties.Redis redisProperties = cacheProperties.getRedis();

        //将配置文件中的所有配置都生效
        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }

      if (redisProperties.getKeyPrefix()!=null){
          config=config.prefixCacheNameWith(redisProperties.getKeyPrefix());
      }
        if (!redisProperties.isCacheNullValues()){
            config=config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()){
            config=config.disableKeyPrefix();
        }

        return config;

    }
}

其他的配置

spring:
 cache:
   type: redis
   redis:
     time-to-live: 60000   #毫秒
     key-prefix: CACHE_    #前缀
      cache-null-values: true  #是否缓存空值----能够防止缓存穿透

@CaheEvict的使用

这里的key注意里面是单引号

@CacheEvict(value = "category",key = "'level1Cateogrys'")

在这里插入图片描述

当修改三级分类后缓存会被自动删除

将获取三级分类的json也添加缓存
在这里插入图片描述

 @Cacheable(value = "category",key = "#root.methodName")

访问gulimall.com首页

查看redis中的缓存情况

当修改菜单后,如何将上面这两个缓存同时删除

在更新菜单的方法上实现

使用@Caching注解实现

@Caching(evict = {
        @CacheEvict(value = "category",key = "'level1Cateogrys'"),
        @CacheEvict(value = "category",key = "'getCatalogJson'")

})

还可以使用

@CacheEvict(value = "category",allEntries = true)

删除category分区下的所有缓存

如果下面这种方式,产生的缓存就会按照下面的图片一样展示

@CacheEvict(value = "category",allEntries = true)

在这里插入图片描述

Spring-Cache的不足

1、读模式:

缓存穿透:查询一个null数据。解决:缓存空数据

缓存击穿:大量并发进来同时查到一个正好过期的数据 。解决:默认无锁;sync=true(加锁)

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

2、写模式(缓存与数据库一致)

读写加锁

引入Canal、感知到Mysql的更新去更新数据库

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值