RedisTemplate與StringRedisTemplate區別

1.兩者數據各自存,各自取,數據不互通。

RedisTemplate不能取StringRedisTemplate存入的數據。
StringRedisTemplate不能取RedisTemplate存入的數據 
2.序列化策略不同。

RedisTemplate採用JDK的序列化策略
StringRedisTemplate採用String的序列化策略

RedisTemplate用法

redisTemplate.opsForValue();  //操作字符串
redisTemplate.opsForHash();   //操作hash
redisTemplate.opsForList();   //操作list
redisTemplate.opsForSet();    //操作set
redisTemplate.opsForZSet();   //操作有序set



Redis中opsForValue()方法的使用介紹:

1、set(K key, V value)
新增一個字符串類型的值,key是鍵,value是值。

Java代碼  

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



2、get(Object key)
獲取key鍵對應的值。

Java代碼  

String stringValue = redisTemplate.opsForValue().get("stringValue")+"";  
System.out.println("通過get(Object key)方法獲取set(K key, V value)方法新增的字符串值:" + stringValue); 

 
3、append(K key, String value)
在原有的值基礎上新增字符串到末尾。

Java代碼  

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

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

Java代碼  

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

  
5、getAndSet(K key, V value)
獲取原來key鍵對應的值並重新賦新值。

Java代碼  

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

  
6、setBit(K key, long offset, boolean value)
key鍵對應的值value對應的ascii碼,在offset的位置(從左向右數)變爲value。

Java代碼  

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

  
7、getBit(K key, long offset)
判斷指定的位置ASCII碼的bit位是否爲1。

Java代碼  

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

 
8、size(K key)
獲取指定字符串的長度。

Java代碼  

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

  
9、increment(K key, double delta)
以增量的方式將double值存儲在變量中。

Java代碼  

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


10、increment(K key, long delta)
 以增量的方式將long值存儲在變量中。

Java代碼  

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

 
11、setIfAbsent(K key, V value)
如果鍵不存在則新增,存在則不改變已經有的值。

Java代碼  

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


12、set(K key, V value, long timeout, TimeUnit unit)
設置變量值的過期時間。

Java代碼  

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

  
13、set(K key, V value, long offset)
覆蓋從指定位置開始的值。

Java代碼  

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

 
14、multiSet(Map<? extends K,? extends V> map)
設置map集合到redis。

Java代碼  

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值。

Java代碼  

//根據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集合名稱不存在,則添加,如果存在則不做修改。

Java代碼  

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

 

StringRedisTemplate用法

//向redis裏存入數據和設置緩存時間

stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);


//val做-1操作
stringRedisTemplate.boundValueOps("test").increment(-1);


//根據key獲取緩存中的val 
stringRedisTemplate.opsForValue().get("test")


//val +1 
stringRedisTemplate.boundValueOps("test").increment(1);//val +1


//根據key獲取過期時間
stringRedisTemplate.getExpire("test")


//根據key獲取過期時間並換算成指定單位
stringRedisTemplate.getExpire("test",TimeUnit.SECONDS)


//根據key刪除緩存
stringRedisTemplate.delete("test");


//檢查key是否存在,返回boolean值
stringRedisTemplate.hasKey("546545");


//向指定key中存放set集合 
stringRedisTemplate.opsForSet().add("red_123", "1","2","3");


//設置過期時間 
stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS);


//根據key查看集合中是否存在指定數據 
stringRedisTemplate.opsForSet().isMember("red_123", "1")


//根據key獲取set集合
stringRedisTemplate.opsForSet().members("red_123");

RedisTemplate存入數據後,key與value在Redis中均變爲了字節數組。那麼,如果使用StringRedisTemplate以相同的key去Redis中查詢,會查詢不到該key,則會報NULL。

StringRedisTemplate存入數據後,key與value在Redis中均爲可讀數據。那麼,如果使用RedisTemplate以相同key去Redus中查詢,key變爲了字節數組的形式,查詢不到該字節數組形式的key,則會報NULL。

所以,RedisTemplate與StringRedisTemplate的數據不互通
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章