更新时间:2024-10-23 GMT+08:00
RedisCacheManager
import com.huawei.devspore.mas.Redis.core.MultiZoneClient; import com.huawei.devspore.mas.Redis.spring.boot.cache.DcsConnectionFactory; import org.springframework.cache.CacheManager; 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.connection.RedisConnectionFactory; 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; import java.time.Duration; @Configuration public class TemplateConfig { @Bean public DcsConnectionFactory dcsConnectionFactory(MultiZoneClient client) { return new DcsConnectionFactory(client); } @Bean public CacheManager cacheManager(RedisConnectionFactory dcsConnectionFactory) { RedisSerializer<String> RedisSerializer = new StringRedisSerializer(); GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); // 配置序列化 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofSeconds(5)) //设置缓存失效时间 .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer)) .disableCachingNullValues(); return RedisCacheManager.builder(dcsConnectionFactory) .cacheDefaults(config) .build(); } }