当前位置:网站首页>配置类总结
配置类总结
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;
}
}
边栏推荐
- 执掌图表
- MongoDB construction and basic operations
- Software Testing Interview Questions: About Automated Testing Tools?
- 国内网站用香港服务器会被封吗?
- Software Testing Interview Questions: What aspects should be considered when designing test cases, i.e. what aspects should different test cases test against?
- ORA-00257
- Opencv——视频跳帧处理
- 4. PCIe 接口时序
- 2022 Hangzhou Electric Power Multi-School Session 3 K Question Taxi
- 【无标题】
猜你喜欢

node使用redis

Memory Forensics Series 1

码率vs.分辨率,哪一个更重要?
![[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots](/img/fa/5bdc81b1ebfc22d31f42da34427f3e.png)
[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots

面试汇总:为何大厂面试官总问 Framework 的底层原理?

Matlab uses plotting method for data simulation and simulation

MongoDB搭建及基础操作

Kubernetes 网络入门
![[idea] idea configures sql formatting](/img/89/98cd23aff3e2f15ecb489f8b3c50e9.png)
[idea] idea configures sql formatting

2022 Hangzhou Electric Power Multi-School Session 3 K Question Taxi
随机推荐
【idea】idea配置sql格式化
主库预警日志报错ORA-00270
E - Distance Sequence (prefix and optimized dp
About I double-checked and reviewed the About staff page, returning an industry question
LiveVideoStackCon 2022 Shanghai Station opens tomorrow!
NMS原理及其代码实现
tiup update
Redis visual management software Redis Desktop Manager2022
Software testing interview questions: What are the three modules of LoadRunner?
软件测试面试题:系统测试的策略有?
Zombie and orphan processes
《WEB安全渗透测试》(28)Burp Collaborator-dnslog外带技术
node uses redis
gorm联表查询-实战
Software testing interview questions: Have you used some tools for software defect (Bug) management in your past software testing work? If so, please describe the process of software defect (Bug) trac
QSunSync 七牛云文件同步工具,批量上传
2022牛客多校训练第二场 H题 Take the Elevator
The principle of NMS and its code realization
More than 2022 cattle school training topic Link with the second L Level Editor I
软件测试面试题:测试用例通常包括那些内容?