溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

RedisTemplate之opsForValue如何使用

發(fā)布時間:2022-06-06 13:53:30 來源:億速云 閱讀:3591 作者:iii 欄目:開發(fā)技術

這篇文章主要介紹“RedisTemplate之opsForValue如何使用”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“RedisTemplate之opsForValue如何使用”文章能幫助大家解決問題。

Redis中opsForValue()方法的使用

1、set(K key, V value)

新增一個字符串類型的值,key是鍵,value是值。

redisTemplate.opsForValue().set("stringValue","bbb");

2、get(Object key)

獲取key鍵對應的值。

String stringValue = redisTemplate.opsForValue().get("key")

3、append(K key, String value)

在原有的值基礎上新增字符串到末尾。

redisTemplate.opsForValue().append("key", "appendValue");
String stringValueAppend = redisTemplate.opsForValue().get("key");
System.out.println("通過append(K key, String value)方法修改后的字符串:"+stringValueAppend);

4、get(K key, long start, long end)

截取key鍵對應值得字符串,從開始下標位置開始到結束下標的位置(包含結束下標)的字符串。

String cutString = redisTemplate.opsForValue().get("key", 0, 3);  
System.out.println("通過get(K key, long start, long end)方法獲取截取的字符串:"+cutString);

5、getAndSet(K key, V value)

獲取原來key鍵對應的值并重新賦新值。

String oldAndNewStringValue = redisTemplate.opsForValue().getAndSet("key", "ccc");  
System.out.print("通過getAndSet(K key, V value)方法獲取原來的值:" + oldAndNewStringValue );  
String newStringValue = redisTemplate.opsForValue().get("key");  
System.out.println("修改過后的值:"+newStringValue);

6、setBit(K key, long offset, boolean value)

key鍵對應的值value對應的ascii碼,在offset的位置(從左向右數(shù))變?yōu)関alue。

redisTemplate.opsForValue().setBit("key",1,false);  
newStringValue = redisTemplate.opsForValue().get("key")+"";  
System.out.println("通過setBit(K key,long offset,boolean value)方法修改過后的值:"+newStringValue);

7、getBit(K key, long offset)

判斷指定的位置ASCII碼的bit位是否為1。

boolean bitBoolean = redisTemplate.opsForValue().getBit("key",1);  
System.out.println("通過getBit(K key,long offset)方法判斷指定bit位的值是:" + bitBoolean);

8、size(K key)

獲取指定字符串的長度

Long stringValueLength = redisTemplate.opsForValue().size("key");  
System.out.println("通過size(K key)方法獲取字符串的長度:"+stringValueLength);

9、increment(K key, double delta)

以增量的方式將double值存儲在變量中。

double stringValueDouble = redisTemplate.opsForValue().increment("doubleKey",5);   
System.out.println("通過increment(K key, double delta)方法以增量方式存儲double值:" + stringValueDouble);

10、increment(K key, long delta)

以增量的方式將long值存儲在變量中。

double stringValueLong = redisTemplate.opsForValue().increment("longKey",6);   
System.out.println("通過increment(K key, long delta)方法以增量方式存儲long值:" + stringValueLong);

11、setIfAbsent(K key, V value)  

如果鍵不存在則新增,存在則不改變已經(jīng)有的值。

boolean absentBoolean = redisTemplate.opsForValue().setIfAbsent("absentKey","fff");  
System.out.println("通過setIfAbsent(K key, V value)方法判斷變量值absentValue是否存在:" + absentBoolean);  
if(absentBoolean){  
    String absentValue = redisTemplate.opsForValue().get("absentKey")+"";  
    System.out.print(",不存在,則新增后的值是:"+absentValue);  
    boolean existBoolean = redisTemplate.opsForValue().setIfAbsent("absentKey","eee");  
    System.out.print(",再次調(diào)用setIfAbsent(K key, V value)判斷absentValue是否存在并重新賦值:" + existBoolean);  
    if(!existBoolean){  
        absentValue = redisTemplate.opsForValue().get("absentKey")+"";  
        System.out.print("如果存在,則重新賦值后的absentValue變量的值是:" + absentValue);

12、set(K key, V value, long timeout, TimeUnit unit)

設置變量值的過期時間。

redisTemplate.opsForValue().set("timeOutKey", "timeOut", 5, TimeUnit.SECONDS);  
String timeOutValue = redisTemplate.opsForValue().get("timeOutKey")+"";  
System.out.println("通過set(K key, V value, long timeout, TimeUnit unit)方法設置過期時間,過期之前獲取的數(shù)據(jù):"+timeOutValue);  
Thread.sleep(5*1000);  
timeOutValue = redisTemplate.opsForValue().get("timeOutKey")+"";  
System.out.print(",等待10s過后,獲取的值:"+timeOutValue);

13、set(K key, V value, long offset)

覆蓋從指定位置開始的值。

redisTemplate.opsForValue().set("absentKey","dd",1);  
String overrideString = redisTemplate.opsForValue().get("absentKey");  
System.out.println("通過set(K key, V value, long offset)方法覆蓋部分的值:"+overrideString);

14、multiSet(Map<? extends K,? extends V> map)

設置map集合到redis。

Map valueMap = new HashMap();  
valueMap.put("valueMap1","map1");  
valueMap.put("valueMap2","map2");  
valueMap.put("valueMap3","map3");  
redisTemplate.opsForValue().multiSet(valueMap);

15、multiGet(Collection<K> keys)

根據(jù)集合取出對應的value值。

//根據(jù)List集合取出對應的value值  
List paraList = new ArrayList();  
paraList.add("valueMap1");  
paraList.add("valueMap2");  
paraList.add("valueMap3");  
List<String> valueList = redisTemplate.opsForValue().multiGet(paraList);  
for (String value : valueList){  
    System.out.println("通過multiGet(Collection<K> keys)方法獲取map值:" + value);  
}

16、multiSetIfAbsent(Map<? extends K,? extends V> map)

如果對應的map集合名稱不存在,則添加;如果存在則不做修改。

Map valueMap = new HashMap();  
valueMap.put("valueMap1","map1");  
valueMap.put("valueMap2","map2");  
valueMap.put("valueMap3","map3");  
redisTemplate.opsForValue().multiSetIfAbsent(valueMap);

RedisTemplate.opsForValue常用方法

簡介

項目一般都會有緩存,常常使用redis來存取緩存(現(xiàn)在已不推薦使用session存儲緩存),我們的鍵(key)和值(value)都是通過Spring提供的Serializer序列化到數(shù)據(jù)庫的。

RedisTemplate默認使用的是JdkSerializationRedisSerializer,StringRedisTemplate默認使用的是StringRedisSerializer。

RedisTemplate.opsForValue常用方法

 //新增一個字符串類型的值,key是鍵,value是值
set(K key, V value)
redisTemplate.opsForValue().set("stringValue","bbb");
//獲取key鍵對應的值
get(Object key)
redisTemplate.opsForValue().get("stringValue");
//在原有的值基礎上新增字符串到末尾
append(K key, String value)
redisTemplate.opsForValue().append("stringValue","aaa");
//截取key鍵對應值得字符串,從開始下標位置開始到結束下標的位置(包含結束下標)的字符串
get(K key, long start, long end)
redisTemplate.opsForValue().get("stringValue",0,3);
//獲取原來key鍵對應的值并重新賦新值
getAndSet(K key, V value)
String oldValue = redisTemplate.opsForValue().getAndSet("stringValue","ccc");
//key鍵對應的值value對應的ascll碼,在offset的位置(從左向右數(shù))變?yōu)閍scll碼的value
setBit(K key, long offset, boolean value)
redisTemplate.opsForValue().setBit("stringValue",1,false);
//判斷指定的位置ASCII碼的bit位是否為1
getBit(K key, long offset)
boolean bitBoolean = redisTemplate.opsForValue().getBit("stringValue",1);
//獲取指定字符串的長度
size(K key)
Long stringValueLength = redisTemplate.opsForValue().size("stringValue");
//以增量的方式將long值存儲在變量中
increment(K key, long delta)
double stringValueLong = redisTemplate.opsForValue().increment("longValue",6);
//如果鍵不存在則新增,存在則不改變已經(jīng)有的值
setIfAbsent(K key, V value)
boolean absentBoolean = redisTemplate.opsForValue().setIfAbsent("absentValue","fff");
//設置變量值的過期時間
set(K key, V value, long timeout, TimeUnit unit)
redisTemplate.opsForValue().set("timeOutValue","timeOut",5,TimeUnit.SECONDS);
//覆蓋從指定位置開始的值
set(K key, V value, long offset)
redisTemplate.opsForValue().set("absentValue","dd",1);
//設置map集合到redis
multiSet(Map<? extends K,? extends V> map)
Map valueMap = new HashMap(){{//匿名內(nèi)部內(nèi)
        put("valueMap1","map1");
        put("valueMap2","map2");
        put("valueMap3","map3");
}};
redisTemplate.opsForValue().multiSet(valueMap);

關于“RedisTemplate之opsForValue如何使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識,可以關注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI