SpringBoot2.3整合redis緩存自定義序列化的實現(xiàn)
1.引言
我們使用redis作為緩存中間件時,當(dāng)我們第一次查詢數(shù)據(jù)的時候,是去數(shù)據(jù)庫查詢,然后查到的數(shù)據(jù)封裝到實體類中,實體類會被序列化存入緩存中,當(dāng)?shù)诙尾閿?shù)據(jù)時,會直接去緩存中查找被序列化的數(shù)據(jù),然后反序列化被我們獲取。我們在緩存中看到的序列化數(shù)據(jù)不直觀,如果想看到類似json的數(shù)據(jù)格式,就需要自定義序列化規(guī)則。
2.整合redis
pom.xml:
<!--引入redis-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
application.yml:
spring: redis: host: 192.168.85.130 port: 6379 database: 0
springboot主配置類要加上@EnableCaching注解
3.自定義序列化
@Configuration
public class MyRedisConfig {
@Bean
public RedisTemplate<Object, Object> empRedisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException {
RedisTemplate<Object,Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
template.setDefaultSerializer(serializer);
return template;
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory){
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofDays(1))
.disableCachingNullValues()
.serializeKeysWith(RedisSerializationContext.SerializationPair
.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build();}
}
4.測試
DeptService:
@Service
public class DeptService {
@Autowired
DepartmentMapper departmentMapper;
@Cacheable(value = "dept")
public Department findById(Integer id){
System.out.println("查詢"+id+"號部門");
Department department = departmentMapper.getDeptById(id);
return department;
}
}
EmployeeService:
@Service
public class EmployeeService {
@Autowired
EmployeeMapper employeeMapper;
@Cacheable(value = "emp")
public Employee findById(Integer id){
System.out.println("查詢"+id+"號員工");
Employee employee = employeeMapper.getEmpById(id);
return employee;
}
}
@Cacheable(value = “dept”) :該注解在方法上,方法傳入?yún)?shù)默認(rèn)為key值,方法返回值為value值,注解的參數(shù)value = "dept"是緩存的名子
結(jié)果:

到此這篇關(guān)于SpringBoot2.3整合redis緩存自定義序列化的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot2.3 redis自定義序列化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Web開發(fā)之基于Session的購物商店實現(xiàn)方法
這篇文章主要介紹了Java Web開發(fā)之基于Session的購物商店實現(xiàn)方法,涉及Java針對session的操作及數(shù)據(jù)庫操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Java使用jxl包寫Excel文件適合列寬實現(xiàn)
用jxl.jar包,讀寫過Excel文件。也沒有注意最適合列寬的問題,但是jxl.jar沒有提供最適合列寬的功能,上次用到寫了一下,可以基本實現(xiàn)最適合列寬。2013-11-11
SpringBoot整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題
這篇文章主要介紹了SpringBoot整合SpringSecurityOauth2實現(xiàn)鑒權(quán)-動態(tài)權(quán)限,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
詳解SpringBoot中@NotNull,@NotBlank注解使用
這篇文章主要為大家詳細(xì)介紹了Spring?Boot中集成Validation與@NotNull,@NotBlank等注解的簡單使用,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-08-08
詳解SpringBoot開發(fā)使用@ImportResource注解影響攔截器
這篇文章主要介紹了詳解SpringBoot開發(fā)使用@ImportResource注解影響攔截器,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11

