Spring Boot 中集成 Redis 作为数据缓存

时间:2023-03-08 17:33:36
Spring Boot 中集成 Redis 作为数据缓存
  • 只添加注解:@Cacheable,不配置key时,redis 中默认存的 key 是:users::SimpleKey [](1.redis-cli 中,通过命令:keys * 查看;2.key:缓存对象存储在Map集合中的key值,非必需,缺省按照函数的所有参数组合作为key值,若自己配置需使用SpEL表达式,比如:@Cacheable(key = "#p0"):使用函数第一个参数作为缓存的key值,更多关于SpEL表达式的详细内容可参考官方文档)。

  • SpringBoot从1.4版本开始,spring-boot-starter-redis依赖改名了,改成了:spring-boot-starter-data-redis。

缓存对象集合中,缓存是以key-value形式保存的。当不指定缓存的key时,SpringBoot会使用SimpleKeyGenerator生成key。

如果2个方法,参数是一样的,但执行逻辑不同,那么将会导致执行第二个方法时命中第一个方法的缓存。
Spring同样提供了方案:继承 CachingConfigurerSupport 并重写 keyGenerator()

项目demo:

相关文章 网址
SpringBoot中使用Redis实现缓存 https://blog.csdn.net/u011851478/article/details/70239722
Spring Boot(三):Spring Boot 中 Redis 的使用 http://www.ityouknow.com/springboot/2016/03/06/spring-boot-redis.html