SpringBoot2.3整合redis緩存自定義序列化的實(shí)現(xiàn)
1.引言
我們使用redis作為緩存中間件時,當(dāng)我們第一次查詢數(shù)據(jù)的時候,是去數(shù)據(jù)庫查詢,然后查到的數(shù)據(jù)封裝到實(shí)體類中,實(shí)體類會被序列化存入緩存中,當(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緩存自定義序列化的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot2.3 redis自定義序列化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java編程實(shí)現(xiàn)屏幕截圖(截屏)代碼總結(jié)
這篇文章主要介紹了java編程實(shí)現(xiàn)屏幕截圖(截屏)代碼,結(jié)合3個實(shí)例總結(jié)分析了Java截屏?xí)r頁面抓取及圖片保存的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
Spring?Boot整合阿里開源中間件Canal實(shí)現(xiàn)數(shù)據(jù)增量同步
這篇文章主要為大家介紹了Spring?Boot整合阿里開源中間件Canal實(shí)現(xiàn)數(shù)據(jù)增量同步示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Java實(shí)現(xiàn)銳化圖片并保存功能(附源碼)
在圖像處理領(lǐng)域,銳化(Sharpening)?是一種常見的操作,用于增強(qiáng)圖像中邊緣和細(xì)節(jié),使圖像看起來更清晰,下面小編就來介紹一下如何使用Java?SE?原生?API實(shí)現(xiàn)對圖像的銳化處理并保存為常見格式文件吧2025-05-05
SpringBoot整合ELK實(shí)現(xiàn)日志監(jiān)控
這篇文章主要為大家詳細(xì)介紹了SpringBoot整合ELK實(shí)現(xiàn)日志監(jiān)控的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
Java實(shí)現(xiàn)按權(quán)重隨機(jī)數(shù)
這篇文章主要介紹了Java實(shí)現(xiàn)按權(quán)重隨機(jī)數(shù),本文給出了提出問題、分析問題、解決問題三個步驟,需要的朋友可以參考下2015-04-04
SpringMVC自定義參數(shù)綁定實(shí)現(xiàn)詳解
這篇文章主要介紹了SpringMVC自定義參數(shù)綁定實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11

