ltd.fdsa.redis.cache.RedisCache Maven / Gradle / Ivy
package ltd.fdsa.redis.cache;
import lombok.extern.slf4j.Slf4j;
import ltd.fdsa.redis.properties.RedisConfigProperties;
import org.springframework.cache.CacheManager;
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.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Slf4j
public class RedisCache {
final RedisConfigProperties properties;
final RedisTemplate redisTemplate;
final RedisConnectionFactory redisConnectionFactory;
public RedisCache(RedisConfigProperties properties, RedisConnectionFactory redisConnectionFactory) {
this.properties = properties;
this.redisConnectionFactory = redisConnectionFactory;
this.redisTemplate = new RedisTemplate<>();
this.redisTemplate.setConnectionFactory(redisConnectionFactory);
this.redisTemplate.setKeySerializer(keySerializer());
this.redisTemplate.setHashKeySerializer(keySerializer());
this.redisTemplate.setValueSerializer(valueSerializer());
this.redisTemplate.setHashValueSerializer(valueSerializer());
}
public CacheManager cacheManager() {
//缓存配置对象
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration = redisCacheConfiguration.entryTtl(this.properties.getCacheTtl()) //设置缓存的默认超时时间:30分钟
.disableCachingNullValues() //如果是空值,不缓存
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer())) //设置key序列化器
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer((valueSerializer()))); //设置value序列化器
return RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}
private RedisSerializer keySerializer() {
return new StringRedisSerializer();
}
private RedisSerializer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy