springboot中redis正確的使用詳解
redis實現(xiàn)了對數(shù)據(jù)的緩存,在項目里一些字典數(shù)據(jù),會話數(shù)據(jù),臨時性數(shù)據(jù)都會向redis來存儲,而在springboot里對redis也有支持,一般來說多個線程共同使用一個redis實現(xiàn)是有線程安全的風(fēng)險的,而每個實現(xiàn)一個線程又太浪費(fèi)資源,無法控制線程數(shù)量是非常危險的,所以就出現(xiàn)了一些redis線程池組件,下面說一下兩個主要的組件。
jedis 線程池主要是每個實例有自己的線程,線程可以從它建立的池子里獲取
lettuce lettuce是 apache推出的線程池工具,它的redis實例是可以被多個線程共享訪問的,提高了資源使用率
redis序列化配置
一般來說, redis-key采用字符串序列化; redis-value采用json序列化, json的體積小,可讀性高,不需要實現(xiàn)serializer接口。
/** ?* 對redis的配置. ?*/ @Configuration public class RedisConfig { ? ? @Autowired ? ? private RedisConnectionFactory redisConnectionFactory; ? ? /** ? ? ?* redis重寫RedisTemplate. ? ? ?*/ ? ? @Bean ? ? public RedisTemplate redisTemplate() { ? ? ? ? RedisTemplate redisTemplate = new RedisTemplate(); ? ? ? ? RedisSerializer<String> stringSerializer = new StringRedisSerializer(); ? ? ? ? Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ? ? ? ? ObjectMapper om = new ObjectMapper(); ? ? ? ? om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); ? ? ? ? //序列化時允許非常量字段均輸出類型,即redis序列化后帶有類型 ? ? ? ? om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); ? ? ? ? jackson2JsonRedisSerializer.setObjectMapper(om); ? ? ? ? // redis key的序列化 ? ? ? ? redisTemplate.setKeySerializer(stringSerializer); ? ? ? ? redisTemplate.setHashKeySerializer(stringSerializer); ? ? ? ? // redis value的序列化 ? ? ? ? redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); ? ? ? ? redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); ? ? ? ? redisTemplate.setConnectionFactory(redisConnectionFactory); ? ? ? ? return redisTemplate; ? ? } }
上面代碼中,om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL)產(chǎn)生的json串是帶有類型的,這樣再反序列化時可以直接通過類型去推斷。
[ "com.lind.basic.entity.Token", { "credentials": "ok", "region": "hello", "bucket": null } ]
看一下添加和讀取的代碼
?@GetMapping("set") ? ? public String set() throws JsonProcessingException { ? ? ? ? Token token = Token.builder() ? ? ? ? ? ? ? ? .credentials("ok") ? ? ? ? ? ? ? ? .region("hello") ? ? ? ? ? ? ? ? .build(); ? ? ? ? redisTemplate.opsForValue().set("test:user", token);//redisTemplate幫我們序列化 ? ? ? ? redisTemplate.opsForHash().put("author", "zzl", token); ? ? ? ? return "OK"; ? ? } ? ? @GetMapping("get") ? ? public Token get() throws IOException { ? ? ? ? return (Token) redisTemplate.opsForValue().get("test:user"); ? ? }
注意:對于實體類Token它應(yīng)該有一個無參構(gòu)造方法,這是反序列化時需要的。
到此這篇關(guān)于springboot中redis正確的使用詳解的文章就介紹到這了,更多相關(guān)springboot redis使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring帶bean和config如何通過main啟動測試
這篇文章主要介紹了spring帶bean和config,通過main啟動測試,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07SpringBoot內(nèi)存數(shù)據(jù)導(dǎo)出成Excel的實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于SpringBoot內(nèi)存數(shù)據(jù)導(dǎo)出成Excel的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12淺談為什么Java中1000==1000為false而100==100為true
這篇文章主要介紹了淺談為什么Java中1000==1000為false而100==100為true,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09SpringBoot整合Web開發(fā)之文件上傳與@ControllerAdvice
@ControllerAdvice注解是Spring3.2中新增的注解,學(xué)名是Controller增強(qiáng)器,作用是給Controller控制器添加統(tǒng)一的操作或處理。對于@ControllerAdvice,我們比較熟知的用法是結(jié)合@ExceptionHandler用于全局異常的處理,但其作用不止于此2022-08-08java 數(shù)據(jù)結(jié)構(gòu)基本算法希爾排序
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)基本算法希爾排序的相關(guān)資料,需要的朋友可以參考下2017-08-08SpringBoot環(huán)境搭建及第一個程序運(yùn)行(小白教程)
這篇文章主要介紹了SpringBoot環(huán)境搭建及第一個程序運(yùn)行,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06