Springboot如何操作redis數(shù)據(jù)
StringRedisTemplate與RedisTemplate區(qū)別點
兩者的關系是StringRedisTemplate繼承RedisTemplate。
兩者的數(shù)據(jù)是不共通的;也就是說StringRedisTemplate只能管理StringRedisTemplate里面的數(shù)據(jù),RedisTemplate只能管理RedisTemplate中的數(shù)據(jù)。
其實他們兩者之間的區(qū)別主要在于他們使用的序列化類:
RedisTemplate使用的是JdkSerializationRedisSerializer 存入數(shù)據(jù)會將數(shù)據(jù)先序列化成字節(jié)數(shù)組然后在存入Redis數(shù)據(jù)庫。
StringRedisTemplate使用的是StringRedisSerializer
使用時注意事項:
當你的redis數(shù)據(jù)庫里面本來存的是字符串數(shù)據(jù)或者你要存取的數(shù)據(jù)就是字符串類型數(shù)據(jù)的時候,那么你就使用
StringRedisTemplate即可。
但是如果你的數(shù)據(jù)是復雜的對象類型,而取出的時候又不想做任何的數(shù)據(jù)轉換,直接從Redis里面取出一個對象,那么使用
RedisTemplate是更好的選擇。
RedisTemplate使用時常見問題:
redisTemplate 中存取數(shù)據(jù)都是字節(jié)數(shù)組。當redis中存入的數(shù)據(jù)是可讀形式而非字節(jié)數(shù)組時,使用redisTemplate取值的時候會無法獲取導出數(shù)據(jù),獲得的值為null??梢允褂?StringRedisTemplate 試試。
RedisTemplate中定義了5種數(shù)據(jù)結構操作
- 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ù)和設置緩存時間
- 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獲取過期時間
- stringRedisTemplate.getExpire("test",TimeUnit.SECONDS)//根據(jù)key獲取過期時間并換算成指定單位
- 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);//設置過期時間
- 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;
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
springboot?vue測試平臺接口定義及發(fā)送請求功能實現(xiàn)
這篇文章主要為大家介紹了springboot+vue測試平臺接口定義及發(fā)送請求功能實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
Spring 中使用反射創(chuàng)建 Bean 實例的幾種方式
文章介紹了在Spring框架中如何使用反射來創(chuàng)建Bean實例,包括使用Class.newInstance()、Constructor.newInstance()、工廠方法以及Spring的BeanUtils工具類,文章還強調了反射操作的注意事項,如異常處理、性能、安全性以及類型安全,感興趣的朋友一起看看吧2025-03-03
SpringBoot中使用MyBatis-Plus實現(xiàn)分頁接口的詳細教程
MyBatis-Plus是一個MyBatis的增強工具,在MyBatis的基礎上只做增強不做改變,為簡化開發(fā)、提高效率而生,在SpringBoot項目中使用MyBatis-Plus可以大大簡化分頁邏輯的編寫,本文將介紹如何在 SpringBoot項目中使用MyBatis-Plus實現(xiàn)分頁接口2024-03-03
Netty分布式編碼器及寫數(shù)據(jù)事件處理使用場景
這篇文章主要為大家介紹了Netty分布式編碼器及寫數(shù)據(jù)事件處理使用場景剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,2022-03-03
如何使用攔截器獲取請求的入?yún)⒉⑵滢D化為Java對象詳解
這篇文章主要介紹了如何使用攔截器獲取請求的入?yún)⒉⑵滢D化為Java對象的相關資料,文中介紹了兩種實現(xiàn)的方法,并給出了詳細的代碼示例,需要的朋友可以參考下2025-02-02

