Springboot下RedisTemplate的兩種序列化方式實(shí)例詳解
一、定義一個配置類,自定義RedisTemplate的序列化方式
@Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory){ // 創(chuàng)建RedisTemplate對象 RedisTemplate<String, Object> template = new RedisTemplate<>(); // 設(shè)置連接工廠 template.setConnectionFactory(connectionFactory); // 創(chuàng)建JSON序列化工具 GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // 設(shè)置Key的序列化 template.setKeySerializer(stringRedisSerializer); template.setHashKeySerializer(stringRedisSerializer); // 設(shè)置Value的序列化 template.setValueSerializer(jsonRedisSerializer); template.setHashValueSerializer(jsonRedisSerializer); // 返回 return template; } }
測試類進(jìn)行測試
@SpringBootTest class RedisDemoApplicationTests { @Autowired private RedisTemplate<String, Object> redisTemplate; @Test void testString() { // 寫入一條String數(shù)據(jù) redisTemplate.opsForValue().set("name", "虎哥"); // 獲取string數(shù)據(jù) Object name = redisTemplate.opsForValue().get("name"); System.out.println("name = " + name); } @Test void testObject() { User user = new User(); user.setName("何雨泊"); user.setTel(110); redisTemplate.opsForValue().set("user",user); System.out.println("name = " + user); } }
測試存入字符串
測試存入對象
對象類:
測試結(jié)果如下,能將Java對象自動的序列化為JSON字符串,并且查詢時能自動把JSON反序列化為Java對象。
不過,其中記錄了序列化時對應(yīng)的class名稱,目的是為了查詢時實(shí)現(xiàn)自動反序列化。這會帶來額外的內(nèi)存開銷。
二、繼續(xù)優(yōu)化
這個問題的解決我們可以采用手動序列化的方式,換句話說,就是不借助默認(rèn)的序列化器,而是我們自己來控制序列化的動作,同時,我們只采用String的序列化器,這樣,在存儲value時,我們就不需要在內(nèi)存中就不用多存儲數(shù)據(jù),從而節(jié)約我們的內(nèi)存空間。
這種用法比較普遍,因此SpringDataRedis就提供了RedisTemplate的子類:StringRedisTemplate,它的key和value的序列化方式默認(rèn)就是String方式。省去了我們自定義RedisTemplate的序列化方式的步驟,而是直接使用:
@SpringBootTest public class RedisStringTests { @Autowired private StringRedisTemplate stringRedisTemplate; @Test void testString() { // 寫入一條String數(shù)據(jù) stringRedisTemplate.opsForValue().set("stringRedisTemplate:name", "虎哥"); // 獲取string數(shù)據(jù) Object name = stringRedisTemplate.opsForValue().get("name"); System.out.println("name = " + name); } /* *手動進(jìn)行序列化,比較麻煩 */ @Test void testObject() throws JsonProcessingException{ // 創(chuàng)建 ObjectMapper mapper = new ObjectMapper(); User user = new User(); user.setName("何雨泊"); user.setTel(110); // 手動序列化 String json = mapper.writeValueAsString(user); stringRedisTemplate.opsForValue().set("stringRedisTemplate:user",json); String string = stringRedisTemplate.opsForValue().get("stringRedisTemplate:user"); // 手動反序列化 User user1 = mapper.readValue(string, User.class); System.out.println("name = " + user1); } /* * 利用hutool庫內(nèi)的JSONUtil工具類,省去手動序列化,庫的依賴在下面 * */ @Test void testObject1() { User user = new User(); user.setName("何雨泊"); user.setTel(110); /*利用hutool庫內(nèi)的JSONUtil進(jìn)行自動序列化,將user轉(zhuǎn)json*/ stringRedisTemplate.opsForValue().set("user", JSONUtil.toJsonStr(user)); String userjson = stringRedisTemplate.opsForValue().get("user"); /*將json轉(zhuǎn)為bean對象*/ User user1 = JSONUtil.toBean(userjson, User.class); System.out.println("name = " + user1); } }
hutool庫的依賴
<!--hutool--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.17</version> </dependency>
測試結(jié)果:
三、總結(jié)
RedisTemplate的兩種序列化實(shí)踐方案:
- 方案一(存在一定問題):
- 自定義RedisTemplate
- 修改RedisTemplate的序列化器為GenericJackson2JsonRedisSerializer
- 方案二(對方案一進(jìn)行優(yōu)化):
- 使用StringRedisTemplate
- 寫入Redis時,手動把對象序列化為JSON 或者 使用工具類JSONUtil進(jìn)行序列化(推薦)
- 讀取Redis時,手動把讀取到的JSON反序列化為對象
到此這篇關(guān)于Springboot下RedisTemplate的兩種序列化方式的文章就介紹到這了,更多相關(guān)Springboot RedisTemplate序列化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java MyBatis攔截器Inteceptor詳細(xì)介紹
這篇文章主要介紹了java MyBatis攔截器Inteceptor詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11SpringBoot使用AES對JSON數(shù)據(jù)加密和解密的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot使用AES對JSON數(shù)據(jù)加密和解密的實(shí)現(xiàn)方法,文章通過代碼示例介紹的非常詳細(xì),對我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08SpringBoot實(shí)現(xiàn)登錄校驗(yàn)(JWT令牌)
JWT全稱為JSON Web Token,是一種用于身份驗(yàn)證的開放標(biāo)準(zhǔn),本文主要介紹了SpringBoot實(shí)現(xiàn)登錄校驗(yàn)(JWT令牌),具有一定的參考價值,感興趣的可以了解一下2023-12-12Spring?boot集成easy?excel實(shí)現(xiàn)導(dǎo)入導(dǎo)出功能
這篇文章主要介紹了Spring?boot集成easy?excel實(shí)現(xiàn)導(dǎo)入導(dǎo)出操作,使用easyexcel,首先要引入easyexcel的maven依賴,具體的版本根據(jù)你的需求去設(shè)置,本文結(jié)合實(shí)例代碼講解的非常詳細(xì),需要的朋友可以參考下2024-05-05springboot 事件監(jiān)聽的實(shí)現(xiàn)方法
這篇文章主要介紹了springboot 事件監(jiān)聽的實(shí)現(xiàn)方法,并詳細(xì)的介紹了四種監(jiān)聽方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04Java中使用print、printf、println的示例及區(qū)別
Java?的輸出方式一般有這三種,print、println、printf,它們都是?java.long?包里的System類中的方法,本文重點(diǎn)給大家介紹Java中使用print、printf、println的示例,需要的朋友可以參考下2023-05-05