当前位置:网站首页>配置类总结
配置类总结
2022-08-05 00:43:00 【周雨彤的小迷弟】
Redis
Redis 配置类 + 缓存管理器
关于缓存管理器相关见:https://blog.csdn.net/hc1285653662/article/details/125382597
里面介绍了什么是缓存管理器,以及缓存管理器如何使用等
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
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.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
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;
@EnableCaching //开启缓存
@Configuration //配置类
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setConnectionFactory(factory);
//key序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//解决查询缓存转换异常的问题
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置序列化(解决乱码的问题),过期时间600秒
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(600))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
return cacheManager;
}
}
边栏推荐
- 2021年11月网络规划设计师上午题知识点(下)
- Software Testing Interview Questions: What's the Difference Between Manual Testing and Automated Testing?
- 主库预警日志报错ORA-00270
- If capturable=False, state_steps should not be CUDA tensors
- Opencv——视频跳帧处理
- [230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots
- leetcode: 269. The Martian Dictionary
- Raw and scan of gorm
- 软件测试面试题:BIOS, Fat, IDE, Sata, SCSI, Ntfs windows NT?
- 动态规划/背包问题总结/小结——01背包、完全背包
猜你喜欢
QSunSync 七牛云文件同步工具,批量上传
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
TinyMCE禁用转义
动态规划/背包问题总结/小结——01背包、完全背包
SV 类的虚方法 多态
gorm joint table query - actual combat
B站7月榜单丨飞瓜数据B站UP主排行榜发布!
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
2022 Hangzhou Electric Power Multi-School Session 3 K Question Taxi
码率vs.分辨率,哪一个更重要?
随机推荐
软件测试面试题:网络七层协仪具体?
JUC thread pool (1): FutureTask use
2022杭电多校 第三场 B题 Boss Rush
软件测试面试题:您如何看待软件过程改进?在您曾经工作过的企业中,是否有一些需要改进的东西呢?您期望的理想的测试人员的工作环境是怎样的?
gorm joint table query - actual combat
[idea] idea configures sql formatting
软件测试面试题:BIOS, Fat, IDE, Sata, SCSI, Ntfs windows NT?
D - I Hate Non-integer Number (count of selected number dp
【FreeRTOS】FreeRTOS与stm32内置堆栈的占用情况
Lattice PCIe 学习 1
2022牛客多校第三场 J题 Journey
MongoDB construction and basic operations
2021年11月网络规划设计师上午题知识点(上)
canvas Gaussian blur effect
动态规划/背包问题总结/小结——01背包、完全背包
DHCP的工作过程
软件测试面试题:系统测试的策略有?
2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
tensor.nozero(), mask, [mask]
2022 Hangzhou Electric Power Multi-School Session 3 Question B Boss Rush