springCache怎么用

redis分布式缓存大家都知道

那么怎么用springCache集成redis缓存呢

1.pom中引入依赖包

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

2.配置文件中,配置缓存依赖

cache:
    type: REDIS
    redis:
      timeToLive: 200000 #缓存超时时间ms
      cache-null-values: false #是否缓存空值

启动类加注解:

@EnableCaching

3.配置缓存机制,自定义缓存序列化和过期时间(这里配置的是一天)

    @Bean
    public RedisCacheManager cacheManager1Day(RedisConnectionFactory connectionFactory) {
​
        RedisCacheConfiguration config = instanceConfig(3600 * 24L);
        return RedisCacheManager.builder(connectionFactory)
                .cacheDefaults(config)
                .transactionAware()
                .build();
    }
​
    private RedisCacheConfiguration instanceConfig(Long ttl) {
​
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.registerModule(new JavaTimeModule());
        // 去掉各种@JsonSerialize注解的解析
        objectMapper.configure(MapperFeature.USE_ANNOTATIONS, false);
        // 只针对非空的值进行序列化
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        // 将类型序列化到属性json字符串中
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,
                ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
​
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
​
        return RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(ttl))
                .disableCachingNullValues()
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));
​
    }

4.接下来 我们就可以在方法上加注解

@Cacheable(value = {"zhongweit"}, key="#root.methodName")

还有

@CachePut
@CacheEvict

还可以使用

@Caching组合多个注解使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring CacheSpring框架提供的一种缓存机制,用于提高应用程序的性能和响应速度。通过使用Spring Cache,可以将方法的返回值缓存起来,当下次调用相同的方法时,可以直接从缓存中获取结果,而不需要再执行方法的逻辑。 在使用Spring Cache时,需要进行一些配置。首先,需要添加Redis的配置信息,包括缓存类型、缓存过期时间、缓存键的前缀等。可以通过设置`spring.cache.type`为`redis`来指定使用Redis作为缓存类型。可以使用`spring.cache.redis.time-to-live`设置缓存的过期时间,单位为毫秒。可以使用`spring.cache.redis.key-prefix`设置缓存键的前缀,如果不指定前缀,则默认使用缓存的名字作为前缀。可以使用`spring.cache.redis.use-key-prefix`设置是否使用前缀,默认为true。可以使用`spring.cache.redis.cache-null-values`设置是否缓存空值,以防止缓存穿透。 另外,Spring Cache还支持使用JCache(JSR-107)注解来简化开发。从Spring 3.1开始,定义了`org.springframework.cache.Cache`和`org.springframework.cache.CacheManager`接口来统一不同的缓存技术。 在使用Spring Cache时,可以通过在方法上添加`@Cacheable`注解来启用缓存功能。当调用带有`@Cacheable`注解的方法时,Spring会首先检查缓存中是否存在相应的结果,如果存在,则直接返回缓存中的结果,如果不存在,则执行方法的逻辑,并将结果存入缓存中。 总结起来,使用Spring Cache可以通过配置Redis等缓存信息,并在方法上添加`@Cacheable`注解来实现缓存功能,提高应用程序的性能和响应速度。 #### 引用[.reference_title] - *1* [SpringCache的使用](https://blog.csdn.net/ABestRookie/article/details/121297482)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [springcache的使用详解(使用redis做分布式缓存)](https://blog.csdn.net/A_art_xiang/article/details/125580962)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值