Spring Boot cache backed redis
2017-04-11 00:00
483 查看
摘要: Spring Boot cache backed redis
在应用部署多实例时,使用本地缓存(guava/caffeine)会造成数据不一致,所以需要采用集中的存储,此处使用redis。
0. 添加相关依赖
1. 添加相关配置
2. 开启缓存,在Application类加入@EnableCaching
3. 通过@Cacheable使用缓存
二. 指定缓存参数[以过期时间为例,不同的数据类型要求的缓存时间不同]
0. 配置不同过期时间Cache
1. 使用指定Cache
PS:spring-gs-caching,spring-data-redis reference
在应用部署多实例时,使用本地缓存(guava/caffeine)会造成数据不一致,所以需要采用集中的存储,此处使用redis。
0. 添加相关依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>${v}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>${v}</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${v}</version> </dependency>
1. 添加相关配置
spring: redis: host: ${host} port: ${port}
2. 开启缓存,在Application类加入@EnableCaching
3. 通过@Cacheable使用缓存
@Cacheable("{cacheNames}") public Object needCache() { return ...; }
二. 指定缓存参数[以过期时间为例,不同的数据类型要求的缓存时间不同]
0. 配置不同过期时间Cache
@EnableCaching @Configuration public class CacheConfig { @Autowired private RedisTemplate<Object, Object> redisTemplate; /** * Define cache strategy * * @return CacheManager */ @Bean public CacheManager cacheManager() { RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate); Map<String, Long> expires = new HashMap<>(); // Test expires[1min] expires.put("10S", 10L); redisCacheManager.setExpires(expires); return redisCacheManager; } }
1. 使用指定Cache
@Cacheable("10S")
PS:spring-gs-caching,spring-data-redis reference
相关文章推荐
- spring boot 下 shiro+redis与mybatis redis cache冲突的解决办法
- Spring-Boot (四) cache/ehcache/redis-cache集成使用
- spring boot redis cache 缓存学习
- spring boot 整合 redis,使用@Cacheable,@CacheEvict,@CachePut,jedisPool操作redis数据库
- Springboot 2.X中Spring-cache与redis整合
- Spring Boot Cache + redis 设置有效时间和自动刷新缓存,时间支持在配置文件中配置
- Spring Boot cache backed guava/caffeine
- springboot cache redis 缓存
- Spring boot shiro session cache ecache redis 共存配置
- SpringBoot使用Redis做缓存,@Cacheable、@CachePut、@CacheEvict等注解的使用
- spring boot+spring cache实现两级缓存(redis+caffeine)
- 在 spring boot 中使用spring cache + redis
- Springboot中Spring-cache与redis整合
- SpringBoot -- 集成Redis/CacheManager
- SpringBoot -- 集成Redis/CacheManager
- SpringBoot 整合redis实现缓存 记录@CachePut值为1
- spring boot + redis 实现session共享
- (38)Spring Boot分布式Session状态保存Redis【从零开始学Spring Boot】
- (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】
- spring boot + redis 实现session共享