RedisTemplate 獲取key的資料型別

2021-10-11 02:48:33 字數 584 閱讀 5144

spring給我們提供了redistemplate來訪問redis服務,它的keys介面返回匹配的所有資料型別的鍵集合,但繼續訪問它們對應的值需要用valueoperations、hashoperations、listoperations等分別進行操作。

這裡就有個問題是拿到一組多種的key之後,需要先判斷key的型別。

redistemplate裡能找到自帶的type(key)介面,傳入key不能為null值。

返回型別是列舉型的org.springframework.data.redis.connection.datatype

它包含了redis現有的5種資料型別以及none

因此可以使用redistemplate.type(key)再配合datatype進行key資料型別的判斷。

RedisTemplate操作Redis常用

redistemplate中定義了對5種資料結構操作 redistemplate.opsforvalue 操作字串 redistemplate.opsforhash 操作hash redistemplate.opsforlist 操作list redistemplate.opsforset 操作se...

redistemplate事務實踐

code public object testredismulti catch interruptedexception e now string operations.opsforvalue get testredismulti system.out.println now object rs o...

RedisTemplate快取用法小記

2 redis的引用包 org.springframework.data spring data redis 1.8.6.release 3 redistemplate裡面有如下幾種常用的形式 1 string型別 redistemplate.opsforvalue 2 list型別 rediste...