赞
踩
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<!-- <version>2.0.8.RELEASE</version>-->
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<!-- <version>2.9.1</version>-->
</dependency>
package com.cfzq.lz.config; import org.springframework.cache.CacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.io.Serializable; /** * @Author: admin * created on 2020/10/12 */ @EnableCaching @Configuration public class CachingConfig { // springboot2.*配置,需要继承CachingConfigurerSupport // @Bean // @Override // public CacheManager cacheManager() { // // // 重新配置缓存 // RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig(); // // //设置缓存的默认超时时间:30分钟 // redisCacheConfiguration = redisCacheConfiguration.entryTtl(Duration.ofMinutes(30L)) // .disableCachingNullValues() // .disableKeyPrefix() // .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) // .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer((new GenericJackson2JsonRedisSerializer()))); // // return RedisCacheManager.builder(RedisCacheWriter // .nonLockingRedisCacheWriter(redisConnectionFactory)) // .cacheDefaults(redisCacheConfiguration).build(); // } @Bean public RedisTemplate<String, Serializable> redisTemplate(JedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Serializable> template = new RedisTemplate<>(); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager rcm = new RedisCacheManager(redisTemplate); // 设置缓存过期时间(单位:秒),60秒 rcm.setDefaultExpiration(120); return rcm; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。