這篇文章主要介紹“RedisTemplate之opsForValue如何使用”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“RedisTemplate之opsForValue如何使用”文章能幫助大家解決問題。
新增一個字符串類型的值,key是鍵,value是值。
redisTemplate.opsForValue().set("stringValue","bbb");獲取key鍵對應的值。
String stringValue = redisTemplate.opsForValue().get("key")在原有的值基礎上新增字符串到末尾。
redisTemplate.opsForValue().append("key", "appendValue");
String stringValueAppend = redisTemplate.opsForValue().get("key");
System.out.println("通過append(K key, String value)方法修改后的字符串:"+stringValueAppend);截取key鍵對應值得字符串,從開始下標位置開始到結束下標的位置(包含結束下標)的字符串。
String cutString = redisTemplate.opsForValue().get("key", 0, 3);
System.out.println("通過get(K key, long start, long end)方法獲取截取的字符串:"+cutString);獲取原來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);key鍵對應的值value對應的ascii碼,在offset的位置(從左向右數)變為value。
redisTemplate.opsForValue().setBit("key",1,false);
newStringValue = redisTemplate.opsForValue().get("key")+"";
System.out.println("通過setBit(K key,long offset,boolean value)方法修改過后的值:"+newStringValue);判斷指定的位置ASCII碼的bit位是否為1。
boolean bitBoolean = redisTemplate.opsForValue().getBit("key",1);
System.out.println("通過getBit(K key,long offset)方法判斷指定bit位的值是:" + bitBoolean);獲取指定字符串的長度
Long stringValueLength = redisTemplate.opsForValue().size("key");
System.out.println("通過size(K key)方法獲取字符串的長度:"+stringValueLength);以增量的方式將double值存儲在變量中。
double stringValueDouble = redisTemplate.opsForValue().increment("doubleKey",5);
System.out.println("通過increment(K key, double delta)方法以增量方式存儲double值:" + stringValueDouble);以增量的方式將long值存儲在變量中。
double stringValueLong = redisTemplate.opsForValue().increment("longKey",6);
System.out.println("通過increment(K key, long delta)方法以增量方式存儲long值:" + stringValueLong);如果鍵不存在則新增,存在則不改變已經有的值。
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(",再次調用setIfAbsent(K key, V value)判斷absentValue是否存在并重新賦值:" + existBoolean);
if(!existBoolean){
absentValue = redisTemplate.opsForValue().get("absentKey")+"";
System.out.print("如果存在,則重新賦值后的absentValue變量的值是:" + absentValue);設置變量值的過期時間。
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)方法設置過期時間,過期之前獲取的數據:"+timeOutValue);
Thread.sleep(5*1000);
timeOutValue = redisTemplate.opsForValue().get("timeOutKey")+"";
System.out.print(",等待10s過后,獲取的值:"+timeOutValue);覆蓋從指定位置開始的值。
redisTemplate.opsForValue().set("absentKey","dd",1);
String overrideString = redisTemplate.opsForValue().get("absentKey");
System.out.println("通過set(K key, V value, long offset)方法覆蓋部分的值:"+overrideString);設置map集合到redis。
Map valueMap = new HashMap();
valueMap.put("valueMap1","map1");
valueMap.put("valueMap2","map2");
valueMap.put("valueMap3","map3");
redisTemplate.opsForValue().multiSet(valueMap);根據集合取出對應的value值。
//根據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);
}如果對應的map集合名稱不存在,則添加;如果存在則不做修改。
Map valueMap = new HashMap();
valueMap.put("valueMap1","map1");
valueMap.put("valueMap2","map2");
valueMap.put("valueMap3","map3");
redisTemplate.opsForValue().multiSetIfAbsent(valueMap);項目一般都會有緩存,常常使用redis來存取緩存(現在已不推薦使用session存儲緩存),我們的鍵(key)和值(value)都是通過Spring提供的Serializer序列化到數據庫的。
RedisTemplate默認使用的是JdkSerializationRedisSerializer,StringRedisTemplate默認使用的是StringRedisSerializer。
//新增一個字符串類型的值,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的位置(從左向右數)變為ascll碼的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);
//如果鍵不存在則新增,存在則不改變已經有的值
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(){{//匿名內部內
put("valueMap1","map1");
put("valueMap2","map2");
put("valueMap3","map3");
}};
redisTemplate.opsForValue().multiSet(valueMap);關于“RedisTemplate之opsForValue如何使用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。