RedisTemplateのopsForValue使用説明

5828 ワード

RedisでのopsForValue()メソッドの使用方法の説明:
1、set(K key, V value)
文字列タイプの値を追加します.keyはキーで、valueは値です.
redisTemplate.opsForValue().set("stringValue","bbb");  

2、get(Object 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)
キーキーを切り取ると、値文字列に対応し、開始下付き位置から終了下付き位置(終了下付きを含む)までの文字列になります.
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 keys)
対応するvalue値をコレクションに基づいて取り出します.
//  List       value   
List paraList = new ArrayList();  
paraList.add("valueMap1");  
paraList.add("valueMap2");  
paraList.add("valueMap3");  
List valueList = redisTemplate.opsForValue().multiGet(paraList);  
for (String value : valueList){  
    System.out.println("  multiGet(Collection 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); 

完了