SpringBoot2.3整合redis緩存自定義序列化的實(shí)現(xiàn)
1.引言
我們使用redis作為緩存中間件時(shí),當(dāng)我們第一次查詢數(shù)據(jù)的時(shí)候,是去數(shù)據(jù)庫(kù)查詢,然后查到的數(shù)據(jù)封裝到實(shí)體類中,實(shí)體類會(huì)被序列化存入緩存中,當(dāng)?shù)诙尾閿?shù)據(jù)時(shí),會(huì)直接去緩存中查找被序列化的數(shù)據(jù),然后反序列化被我們獲取。我們?cè)诰彺嬷锌吹降男蛄谢瘮?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.測(cè)試
DeptService:
@Service public class DeptService { @Autowired DepartmentMapper departmentMapper; @Cacheable(value = "dept") public Department findById(Integer id){ System.out.println("查詢"+id+"號(hào)部門"); 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+"號(hào)員工"); 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Web開(kāi)發(fā)之基于Session的購(gòu)物商店實(shí)現(xiàn)方法
這篇文章主要介紹了Java Web開(kāi)發(fā)之基于Session的購(gòu)物商店實(shí)現(xiàn)方法,涉及Java針對(duì)session的操作及數(shù)據(jù)庫(kù)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Java使用jxl包寫(xiě)Excel文件適合列寬實(shí)現(xiàn)
用jxl.jar包,讀寫(xiě)過(guò)Excel文件。也沒(méi)有注意最適合列寬的問(wèn)題,但是jxl.jar沒(méi)有提供最適合列寬的功能,上次用到寫(xiě)了一下,可以基本實(shí)現(xiàn)最適合列寬。2013-11-11java把字符串寫(xiě)入文件里的簡(jiǎn)單方法分享
這篇文章主要介紹了java把字符串寫(xiě)入到文件里的簡(jiǎn)單方法,這是跟一個(gè)外國(guó)朋友學(xué)的代碼,需要的朋友可以參考下2014-03-03SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)動(dòng)態(tài)權(quán)限問(wèn)題
這篇文章主要介紹了SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)-動(dòng)態(tài)權(quán)限,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06詳解SpringBoot中@NotNull,@NotBlank注解使用
這篇文章主要為大家詳細(xì)介紹了Spring?Boot中集成Validation與@NotNull,@NotBlank等注解的簡(jiǎn)單使用,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-08-08詳解SpringBoot開(kāi)發(fā)使用@ImportResource注解影響攔截器
這篇文章主要介紹了詳解SpringBoot開(kāi)發(fā)使用@ImportResource注解影響攔截器,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11