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的位置(從左向右數)變爲value。
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)
如果鍵不存在則新增,存在則不改變已經有的值。
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);
}
}
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)方法設置過期時間,過期之前獲取的數據:"+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)
根據集合取出對應的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);
}
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);