springboot中redis正確的使用詳解
redis實(shí)現(xiàn)了對(duì)數(shù)據(jù)的緩存,在項(xiàng)目里一些字典數(shù)據(jù),會(huì)話數(shù)據(jù),臨時(shí)性數(shù)據(jù)都會(huì)向redis來存儲(chǔ),而在springboot里對(duì)redis也有支持,一般來說多個(gè)線程共同使用一個(gè)redis實(shí)現(xiàn)是有線程安全的風(fēng)險(xiǎn)的,而每個(gè)實(shí)現(xiàn)一個(gè)線程又太浪費(fèi)資源,無法控制線程數(shù)量是非常危險(xiǎn)的,所以就出現(xiàn)了一些redis線程池組件,下面說一下兩個(gè)主要的組件。
jedis 線程池主要是每個(gè)實(shí)例有自己的線程,線程可以從它建立的池子里獲取
lettuce lettuce是 apache推出的線程池工具,它的redis實(shí)例是可以被多個(gè)線程共享訪問的,提高了資源使用率
redis序列化配置
一般來說, redis-key采用字符串序列化; redis-value采用json序列化, json的體積小,可讀性高,不需要實(shí)現(xiàn)serializer接口。
/**
?* 對(duì)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);
? ? ? ? //序列化時(shí)允許非常量字段均輸出類型,即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串是帶有類型的,這樣再反序列化時(shí)可以直接通過類型去推斷。
[
"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");
? ? }注意:對(duì)于實(shí)體類Token它應(yīng)該有一個(gè)無參構(gòu)造方法,這是反序列化時(shí)需要的。
到此這篇關(guān)于springboot中redis正確的使用詳解的文章就介紹到這了,更多相關(guān)springboot redis使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring帶bean和config如何通過main啟動(dòng)測(cè)試
這篇文章主要介紹了spring帶bean和config,通過main啟動(dòng)測(cè)試,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
SpringBoot內(nèi)存數(shù)據(jù)導(dǎo)出成Excel的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于SpringBoot內(nèi)存數(shù)據(jù)導(dǎo)出成Excel的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
淺談為什么Java中1000==1000為false而100==100為true
這篇文章主要介紹了淺談為什么Java中1000==1000為false而100==100為true,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
SpringBoot整合Web開發(fā)之文件上傳與@ControllerAdvice
@ControllerAdvice注解是Spring3.2中新增的注解,學(xué)名是Controller增強(qiáng)器,作用是給Controller控制器添加統(tǒng)一的操作或處理。對(duì)于@ControllerAdvice,我們比較熟知的用法是結(jié)合@ExceptionHandler用于全局異常的處理,但其作用不止于此2022-08-08
java 數(shù)據(jù)結(jié)構(gòu)基本算法希爾排序
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)基本算法希爾排序的相關(guān)資料,需要的朋友可以參考下2017-08-08
Java中使用異或語句實(shí)現(xiàn)兩個(gè)變量的互換
這篇文章主要介紹了Java中使用異或語句實(shí)現(xiàn)兩個(gè)變量的互換,本文直接給出代碼實(shí)例以及運(yùn)行結(jié)果,需要的朋友可以參考下2015-06-06
SpringBoot環(huán)境搭建及第一個(gè)程序運(yùn)行(小白教程)
這篇文章主要介紹了SpringBoot環(huán)境搭建及第一個(gè)程序運(yùn)行,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

