赞
踩
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<version>2.7.2</version>
</dependency>
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=30000
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheWriter; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import java.time.Duration; import java.util.HashMap; import java.util.Map; /** * SpringCache中配置 RedisCache * * @author xlwang55 */ @EnableCaching @Configuration public class CacheConfig { /** * 测试缓存key */ public static final String STUDENT_CACHE = "studentCache"; public static final String MESSAGE_CACHE = "messageCache"; /** * 设置SpringCache缓存时间:默认策略和自定义指定key策略的超时时间 */ @Bean public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { return new RedisCacheManager( RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), this.getRedisCacheConfigurationWithTtl(60), this.getRedisCacheConfigurationMap()); } /** * 自定义指定key策略的超时时间 * * @author xlwang55 * @date 2022/12/12 */ private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() { Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(2); //SsoCache和BasicDataCache进行过期时间配置 redisCacheConfigurationMap.put(MESSAGE_CACHE, this.getRedisCacheConfigurationWithTtl(30 * 60)); //自定义设置缓存时间 redisCacheConfigurationMap.put(STUDENT_CACHE, this.getRedisCacheConfigurationWithTtl(60)); return redisCacheConfigurationMap; } /** * 初始化缓存序列化配置和过期时间 * * @param seconds 缓存时间 * @author xlwang55 * @date 2022/12/12 */ private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) { Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig(); return redisCacheConfiguration.serializeValuesWith(RedisSerializationContext .SerializationPair .fromSerializer(jackson2JsonRedisSerializer)).entryTtl(Duration.ofSeconds(seconds)); } }
@EnableCaching开启缓存
@Configuration 标记当前类为配置类,确保springboot能够扫描到此配置类。
userName参数为 “小明”
第一次访问,会查询数据库并将缓存存入redis中
第二次访问,直接命中缓存返回结果
使用@CachePut修改缓存信息,当我们对数据库修改时,为了保持缓存数据一致性需要对缓存信息修改。但是思考了一下,我们在实际使用的时候采用修改缓存的方式并不能完全保证一致性。比如上述queryAllUser案例中,查询全部用户也做了缓存,要想修改缓存并不是件容易的事情。所以建议使用@CacheEvict将缓存清除。
缓存结果
方式一:
@CacheEvict(cacheNames = “user”,allEntries = true)
表示删除缓存名称为user的整个分区
allEntries = true
表示直接清空掉整个分区
@CacheEvict(cacheNames = “user”, key = “#userName”)
方式二 @Caching + @CacheEvict
@Caching中evict可以添加多个@CacheEvict注解,删除指定key的缓存。
添加多个@CacheEvict测试
CacheConfig配置类中定义信息
测试默认缓存时间
通过如下结果发现缓存时间为6000秒,符合定义
测试自定义key缓存时间
通过结果发现 缓存时间为60秒
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。