当前位置:网站首页>配置类总结
配置类总结
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;
}
}
边栏推荐
- 执掌图表
- 2022杭电多校 第三场 B题 Boss Rush
- Kubernetes 网络入门
- "WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology
- Pytorch usage and tricks
- 软件测试面试题:一套完整的测试应该由哪些阶段组成?
- Matlab uses plotting method for data simulation and simulation
- LiveVideoStackCon 2022 上海站明日开幕!
- torch.autograd.grad finds the second derivative
- Software Testing Interview Questions: Qualifying Criteria for Software Acceptance Testing?
猜你喜欢
![[idea] idea configures sql formatting](/img/89/98cd23aff3e2f15ecb489f8b3c50e9.png)
[idea] idea configures sql formatting

内存取证系列1

5. PCIe official example

活动推荐 | 快手StreamLake品牌发布会,8月10日一起见证!

"WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology

Lattice PCIe 学习 1

2022 Hangzhou Electric Power Multi-School Session 3 K Question Taxi

JUC线程池(一): FutureTask使用

Bit rate vs. resolution, which one is more important?

### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionExcep
随机推荐
tensor.nozero(),面具,面具
国内网站用香港服务器会被封吗?
2022 Multi-school Second Session K Question Link with Bracket Sequence I
leetcode: 266. All Palindromic Permutations
软件测试面试题:BIOS, Fat, IDE, Sata, SCSI, Ntfs windows NT?
2022牛客多校第三场 J题 Journey
2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
EL定时刷新页面中的皕杰报表实例
2022杭电多校 第三场 B题 Boss Rush
Inter-process communication and inter-thread communication
Knowledge Points for Network Planning Designers' Morning Questions in November 2021 (Part 2)
tiup status
The method of freely controlling concurrency in the sync package in GO
软件测试面试题:关于自动化测试工具?
软件测试面试题:黑盒测试、白盒测试以及单元测试、集成测试、系统测试、验收测试的区别与联系?
2022杭电多校第三场 K题 Taxi
软件测试面试题:您以往所从事的软件测试工作中,是否使用了一些工具来进行软件缺陷(Bug)的管理?如果有,请结合该工具描述软件缺陷(Bug)跟踪管理的流程?
软件测试面试题:一套完整的测试应该由哪些阶段组成?
torch.autograd.grad求二阶导数
GCC:屏蔽动态库之间的依赖