Springboot如何操作redis數(shù)據(jù)
StringRedisTemplate與RedisTemplate區(qū)別點(diǎn)
兩者的關(guān)系是StringRedisTemplate繼承RedisTemplate。
兩者的數(shù)據(jù)是不共通的;也就是說StringRedisTemplate只能管理StringRedisTemplate里面的數(shù)據(jù),RedisTemplate只能管理RedisTemplate中的數(shù)據(jù)。
其實(shí)他們兩者之間的區(qū)別主要在于他們使用的序列化類:
RedisTemplate使用的是JdkSerializationRedisSerializer 存入數(shù)據(jù)會(huì)將數(shù)據(jù)先序列化成字節(jié)數(shù)組然后在存入Redis數(shù)據(jù)庫。
StringRedisTemplate使用的是StringRedisSerializer
使用時(shí)注意事項(xiàng):
當(dāng)你的redis數(shù)據(jù)庫里面本來存的是字符串?dāng)?shù)據(jù)或者你要存取的數(shù)據(jù)就是字符串類型數(shù)據(jù)的時(shí)候,那么你就使用
StringRedisTemplate即可。
但是如果你的數(shù)據(jù)是復(fù)雜的對(duì)象類型,而取出的時(shí)候又不想做任何的數(shù)據(jù)轉(zhuǎn)換,直接從Redis里面取出一個(gè)對(duì)象,那么使用
RedisTemplate是更好的選擇。
RedisTemplate使用時(shí)常見問題:
redisTemplate 中存取數(shù)據(jù)都是字節(jié)數(shù)組。當(dāng)redis中存入的數(shù)據(jù)是可讀形式而非字節(jié)數(shù)組時(shí),使用redisTemplate取值的時(shí)候會(huì)無法獲取導(dǎo)出數(shù)據(jù),獲得的值為null??梢允褂?StringRedisTemplate 試試。
RedisTemplate中定義了5種數(shù)據(jù)結(jié)構(gòu)操作
- redisTemplate.opsForValue(); //操作字符串
- redisTemplate.opsForHash(); //操作hash
- redisTemplate.opsForList(); //操作list
- redisTemplate.opsForSet(); //操作set
- redisTemplate.opsForZSet(); //操作有序set
StringRedisTemplate常用操作
- stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//向redis里存入數(shù)據(jù)和設(shè)置緩存時(shí)間
- stringRedisTemplate.boundValueOps("test").increment(-1);//val做-1操作
- stringRedisTemplate.opsForValue().get("test")//根據(jù)key獲取緩存中的val
- stringRedisTemplate.boundValueOps("test").increment(1);//val +1
- stringRedisTemplate.getExpire("test")//根據(jù)key獲取過期時(shí)間
- stringRedisTemplate.getExpire("test",TimeUnit.SECONDS)//根據(jù)key獲取過期時(shí)間并換算成指定單位
- stringRedisTemplate.delete("test");//根據(jù)key刪除緩存
- stringRedisTemplate.hasKey("546545");//檢查key是否存在,返回boolean值
- stringRedisTemplate.opsForSet().add("red_123", "1","2","3");//向指定key中存放set集合
- stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS);//設(shè)置過期時(shí)間
- stringRedisTemplate.opsForSet().isMember("red_123", "1")//根據(jù)key查看集合中是否存在指定數(shù)據(jù)
- stringRedisTemplate.opsForSet().members("red_123");//根據(jù)key獲取set集合
StringRedisTemplate的使用
springboot中使用注解@Autowired 即可
@Autowired
public StringRedisTemplate stringRedisTemplate;
使用樣例:
@RestController @RequestMapping("/user") public class UserResource { private static final Logger log = LoggerFactory.getLogger(UserResource.class); @Autowired private UserService userService; @Autowired public StringRedisTemplate stringRedisTemplate; @RequestMapping("/num") public String countNum() { String userNum = stringRedisTemplate.opsForValue().get("userNum"); if(StringUtils.isNull(userNum)){ stringRedisTemplate.opsForValue().set("userNum", userService.countNum().toString()); } return userNum; } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解SpringBoot初始教程之Tomcat、Https配置以及Jetty優(yōu)化
本篇文章主要介紹了詳解SpringBoot初始教程之Tomcat、Https配置以及Jetty優(yōu)化,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09SSH框架網(wǎng)上商城項(xiàng)目第23戰(zhàn)之在線支付功能實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項(xiàng)目第23戰(zhàn)之在線支付功能實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-06-06profiles.active多環(huán)境開發(fā)、測試、部署過程
這篇文章主要介紹了profiles.active多環(huán)境開發(fā)、測試、部署,主要講如何使用profiles.active這個(gè)變量,讓我們?cè)陂_發(fā)過程快速切換環(huán)境配置,以及如何使一個(gè)部署適配各種不同的環(huán)境,需要的朋友可以參考下2023-03-03IDEA 中創(chuàng)建Spring Data Jpa 項(xiàng)目的示例代碼
這篇文章主要介紹了IDEA 中創(chuàng)建Spring Data Jpa 項(xiàng)目的示例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04詳解Java并發(fā)包中線程池ThreadPoolExecutor
ThreadPoolExecutor是Java語言對(duì)于線程池的實(shí)現(xiàn)。線程池技術(shù)使線程在使用完畢后不回收而是重復(fù)利用。如果線程能夠復(fù)用,那么我們就可以使用固定數(shù)量的線程來解決并發(fā)問題,這樣一來不僅節(jié)約了系統(tǒng)資源,而且也會(huì)減少線程上下文切換的開銷2021-06-06idea的spring boot項(xiàng)目實(shí)現(xiàn)更改端口號(hào)操作
這篇文章主要介紹了idea的spring boot項(xiàng)目實(shí)現(xiàn)更改端口號(hào)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09