springboot整合springcache(初使用)

1.导入springcache和redis的pom依赖:(redis做缓存)

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

2.写配置

        2.1 CacheAutoConfiguration回导入RdisCacheConfiguration:自动配好了缓存管理器RedisCacheManager(能力暂时不够,源码看不懂,知道有这么个东西)。

        2.2 配置使用redis作为缓存(最简化配置)

spring.cache.type=redis
#缓存值的过期时间
spring.cache.redis.time-to-live=3600000
#是否开启缓存空值,防止缓存穿透
spring.cache.redis.cache-null-values=true

        2.3配置redis

spring:
  redis:
    host: 192.168.56.10
    port: 6379

3.常用注解

@Cacheable:触发将数据保存到缓存的操作。

@CAcheEvict:触发将数据从缓存中删除的操作。

@CachePut:不影响方法执行更新缓存。

@Caching:组合以上多个操作。

@CacheConfig:在类级别,共享缓存的相同配置。

        3.1 开启缓存功能 在启动类上添加注解:@EnableCacheing

        3.2只需使用注解就能完成缓存操作

自定义:

        1)指定生成的缓存使用的key:key属性指定,接受一个SpEL表达式

@Cacheable(value = {"category"},key = "'level1Category'") 

@Cacheable代表当前的方法返回的结果需要缓存,如果缓存中有,方法不用调用;如果缓存中没有,会调用方法,将最后的结果放入缓存。每一个需要缓存的数据我们都来指定要放入哪个名字的缓存【缓存的分区,j建议按照业务类型来分】,可以是数组@Cacheable({"category","product","product2"})。key = "'level1Category'",指定生成缓存的key值。

        2)指定缓存数据的存活时间:在配置文件中配置,单位ms。

spring.cache.redis.time-to-live=3600000

        3)将数据保存为JSON格式

        自定义配置类,自定义一个缓存管理器

@Configuration
@EnableCaching
public class MyCacheConfig {
    @Bean
    RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){

        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
//        RedisCacheConfiguration redisCacheConfiguration = config.entryTtl();
        //自定义配置覆盖原来的配置
         config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
         config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

         //将配置文件中的所有配置都生效
        CacheProperties.Redis redis = cacheProperties.getRedis();
        if (redis.getTimeToLive() != null){
            config = config.entryTtl(redis.getTimeToLive());
        }
        if (redis.getKeyPrefix() != null){
            config = config.prefixKeysWith(redis.getKeyPrefix());
        }
        if (!redis.isCacheNullValues()){
            config = config.disableCachingNullValues();
        }
        if (!redis.isUseKeyPrefix()){
            config = config.disableKeyPrefix();
        }

        return config;
    }
}

使用:使用@Cacheable注解,会将返回的结果自动存入redis中,分区是指定的cache,key是指定的cache_hello。如果缓存中有这个数据,这不会调用此方法,直接走redis缓存中取值(这个地方不是很确定)。可以添加一个本地锁(sync -true),防止缓存击穿(大量并发进来同时查询一个正好过期的数据)

   //每一个需要缓存的数据我们都来指定要放入哪个名字的缓存【缓存的分区,按照业务类型来分】,可以是数组 @Cacheable({"cache","product","product2"})
    @Cacheable(value = {"cache"},key = "'cache_hello'")  //代表当前的方法返回的结果需要缓存,如果缓存中有,方法不用调用,如果缓存中没有,会调用方法,将最后的结果放入缓存。
    @Override
    public List<Hello> hello() {
        .......
        return list;
    }

 @CacheEvict触发将数据从缓存中删除的操作。当updateCasecode方法执行后会触发删缓存操作。@Caching组合注解操作。

 //@CacheEvict(value = "Cache",key = "'Cache_hello'") // 失效模式、;保证数据一致性。删除对应分区的对应的缓存
    @Caching(evict = {
            @CacheEvict(value = "Cache",key = "'Cache_hello'"),
            @CacheEvict(value = "Cache2",key = "'Cache_hello'")
    })
    // @Caching(value = "Cache",allEntries = true) //删除指定的"Cache"分区所有数据
    @Transactional
    @Override
    public void updateCascode(Hello hello) {
       this.updateById(hello.getID());                  

    }

多看源码!!!!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值