1. 程式人生 > 實用技巧 >【Azure Redis 快取 Azure Cache For Redis】使用Redis自帶redis-benchmark.exe命令測試Azure Redis的效能

【Azure Redis 快取 Azure Cache For Redis】使用Redis自帶redis-benchmark.exe命令測試Azure Redis的效能

1、put(Hkey,HKhashKey,HVvalue)

新增hashMap值。

  1. redisTemplate.opsForHash().put("hashValue","map1","map1-1");
  2. redisTemplate.opsForHash().put("hashValue","map2","map2-2");

2、values(Hkey)

獲取指定變數中的hashMap值。

  1. List<Object> hashList = redisTemplate.opsForHash().values("hashValue");
  2. System.out.println("通過values(H key)方法獲取變數中的hashMap值:" + hashList);

3、entries(Hkey)

獲取變數中的鍵值對。

  1. Map<Object,Object> map = redisTemplate.opsForHash().entries("hashValue");
  2. System.out.println("通過entries(H key)方法獲取變數中的鍵值對:" + map);

4、get(Hkey,ObjecthashKey)

獲取變數中的指定map鍵是否有值,如果存在該map鍵則獲取值,沒有則返回null。

  1. Object mapValue = redisTemplate.opsForHash().get("hashValue","map1");
  2. System.out.println("通過get(H key, Object hashKey)方法獲取map鍵的值:" + mapValue);

5、hasKey(Hkey,ObjecthashKey)

判斷變數中是否有指定的map鍵。

  1. boolean hashKeyBoolean = redisTemplate.opsForHash().hasKey("hashValue","map3");
  2. System.out.println("通過hasKey(H key, Object hashKey)方法判斷變數中是否存在map鍵:" + hashKeyBoolean);

6、keys(Hkey)

獲取變數中的鍵。

  1. Set<Object> keySet = redisTemplate.opsForHash().keys("hashValue");
  2. System.out.println("通過keys(H key)方法獲取變數中的鍵:" + keySet);

7、size(Hkey)

獲取變數的長度。

  1. long hashLength = redisTemplate.opsForHash().size("hashValue");
  2. System.out.println("通過size(H key)方法獲取變數的長度:" + hashLength);

8、increment(Hkey,HKhashKey, doubledelta)

使變數中的鍵以double值的大小進行自增長。

  1. double hashIncDouble = redisTemplate.opsForHash().increment("hashInc","map1",3);
  2. System.out.println("通過increment(H key, HK hashKey, double delta)方法使變數中的鍵以值的大小進行自增長:" + hashIncDouble);

9、increment(Hkey,HKhashKey, longdelta)

使變數中的鍵以long值的大小進行自增長。

  1. long hashIncLong = redisTemplate.opsForHash().increment("hashInc","map2",6);
  2. System.out.println("通過increment(H key, HK hashKey, long delta)方法使變數中的鍵以值的大小進行自增長:" + hashIncLong);

10、multiGet(Hkey,Collection<HK>hashKeys)

以集合的方式獲取變數中的值。

  1. List<Object> list = new ArrayList<Object>();
  2. list.add("map1");
  3. list.add("map2");
  4. List mapValueList = redisTemplate.opsForHash().multiGet("hashValue",list);
  5. System.out.println("通過multiGet(H key, Collection<HK> hashKeys)方法以集合的方式獲取變數中的值:"+mapValueList);

11、putAll(Hkey,Map<? extendsHK,? extendsHV>m)

以map集合的形式新增鍵值對。

  1. Map newMap = new HashMap();
  2. newMap.put("map3","map3-3");
  3. newMap.put("map5","map5-5");
  4. redisTemplate.opsForHash().putAll("hashValue",newMap);
  5. map = redisTemplate.opsForHash().entries("hashValue");
  6. System.out.println("通過putAll(H key, Map<? extends HK,? extends HV> m)方法以map集合的形式新增鍵值對:" + map);

12、putIfAbsent(Hkey,HKhashKey,HVvalue)

如果變數值存在,在變數中可以新增不存在的的鍵值對,如果變數不存在,則新增一個變數,同時將鍵值對新增到該變數。

  1. redisTemplate.opsForHash().putIfAbsent("hashValue","map6","map6-6");
  2. map = redisTemplate.opsForHash().entries("hashValue");
  3. System.out.println("通過putIfAbsent(H key, HK hashKey, HV value)方法新增不存在於變數中的鍵值對:" + map);

13、scan(Hkey,ScanOptionsoptions)

匹配獲取鍵值對,ScanOptions.NONE為獲取全部鍵對,ScanOptions.scanOptions().match("map1").build() 匹配獲取鍵位map1的鍵值對,不能模糊匹配。

  1. Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("hashValue",ScanOptions.scanOptions().match("map1").build());
  2. //Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("hashValue",ScanOptions.NONE);
  3. while (cursor.hasNext()){
  4. Map.Entry<Object,Object> entry = cursor.next();
  5. System.out.println("通過scan(H key, ScanOptions options)方法獲取匹配鍵值對:" + entry.getKey() + "---->" + entry.getValue());
  6. }

14、delete(Hkey,Object...hashKeys)

刪除變數中的鍵值對,可以傳入多個引數,刪除多個鍵值對。

  1. redisTemplate.opsForHash().delete("hashValue","map1","map2");
  2. map = redisTemplate.opsForHash().entries("hashValue");
  3. System.out.println("通過delete(H key, Object... hashKeys)方法刪除變數中的鍵值對後剩餘的:" + map);

    1、set(Kkey,Vvalue)

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

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

    2、get(Objectkey)

    獲取key鍵對應的值。

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

    3、append(Kkey,Stringvalue)

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

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

    4、get(Kkey, longstart, longend)

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

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

    5、getAndSet(Kkey,Vvalue)

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

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

    6、setBit(Kkey, longoffset, booleanvalue)

    key鍵對應的值value對應的ascii碼,在offset的位置(從左向右數)變為value。

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

    7、getBit(Kkey, longoffset)

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

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

    8、size(Kkey)

    獲取指定字串的長度。

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

    9、increment(Kkey, doubledelta)

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

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

    10、increment(Kkey, longdelta)

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

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

    11、setIfAbsent(Kkey,Vvalue)

    如果鍵不存在則新增,存在則不改變已經有的值。

    1. boolean absentBoolean = redisTemplate.opsForValue().setIfAbsent("absentValue","fff");
    2. System.out.println("通過setIfAbsent(K key, V value)方法判斷變數值absentValue是否存在:" + absentBoolean);
    3. if(absentBoolean){
    4. String absentValue = redisTemplate.opsForValue().get("absentValue")+"";
    5. System.out.print(",不存在,則新增後的值是:"+absentValue);
    6. boolean existBoolean = redisTemplate.opsForValue().setIfAbsent("absentValue","eee");
    7. System.out.print(",再次呼叫setIfAbsent(K key, V value)判斷absentValue是否存在並重新賦值:" + existBoolean);
    8. if(!existBoolean){
    9. absentValue = redisTemplate.opsForValue().get("absentValue")+"";
    10. System.out.print("如果存在,則重新賦值後的absentValue變數的值是:" + absentValue);
    11. }
    12. }

    12、set(Kkey,Vvalue, longtimeout,TimeUnitunit)

    設定變數值的過期時間。

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

    13、set(Kkey,Vvalue, longoffset)

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

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

    14、multiSet(Map<? extendsK,? extendsV>map)

    設定map集合到redis。

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

    15、multiGet(Collection<K>keys)

    根據集合取出對應的value值。

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

    16、multiSetIfAbsent(Map<? extendsK,? extendsV>map)

    如果對應的map集合名稱不存在,則新增,如果存在則不做修改。

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

      1、leftPush(Kkey,Vvalue)

      在變數左邊新增元素值。

      1. redisTemplate.opsForList().leftPush("list","a");
      2. redisTemplate.opsForList().leftPush("list","b");
      3. redisTemplate.opsForList().leftPush("list","c");

      2、index(Kkey, longindex)

      獲取集合指定位置的值。

      1. String listValue = redisTemplate.opsForList().index("list",1) + "";
      2. System.out.println("通過index(K key, long index)方法獲取指定位置的值:" + listValue);

      3、range(Kkey, longstart, longend)

      獲取指定區間的值。

      1. List<Object> list = redisTemplate.opsForList().range("list",0,-1);
      2. System.out.println("通過range(K key, long start, long end)方法獲取指定範圍的集合值:"+list);

      4、leftPush(Kkey,Vpivot,Vvalue)

      把最後一個引數值放到指定集合的第一個出現中間引數的前面,如果中間引數值存在的話。否則返回-1

      1. redisTemplate.opsForList().leftPush("list","a","n");
      2. list = redisTemplate.opsForList().range("list",0,-1);
      3. System.out.println("通過leftPush(K key, V pivot, V value)方法把值放到指定引數值前面:" + list);

      5、leftPushAll(Kkey,V...values)

      向左邊批量新增引數元素。

      1. redisTemplate.opsForList().leftPushAll("list","w","x","y");
      2. list = redisTemplate.opsForList().range("list",0,-1);
      3. System.out.println("通過leftPushAll(K key, V... values)方法批量新增元素:" + list);

      6、leftPushAll(Kkey,Collection<V>values)

      以集合的方式向左邊批量新增元素。

      1. List newList = new ArrayList();
      2. newList.add("o");
      3. newList.add("p");
      4. newList.add("q");
      5. redisTemplate.opsForList().leftPushAll("list",newList);
      6. list = redisTemplate.opsForList().range("list",0,-1);
      7. System.out.println("通過leftPushAll(K key, Collection<V> values)方法以集合的方式批量新增元素:" + list);

      7、leftPushIfPresent(Kkey,Vvalue)

      如果存在集合則新增元素。

      1. redisTemplate.opsForList().leftPushIfPresent("presentList","o");
      2. list = redisTemplate.opsForList().range("presentList",0,-1);
      3. System.out.println("通過leftPushIfPresent(K key, V value)方法向已存在的集合新增元素:" + list);

      8、rightPush(Kkey,Vvalue)

      向集合最右邊新增元素。

      1. redisTemplate.opsForList().rightPush("list","w");
      2. list = redisTemplate.opsForList().range("list",0,-1);
      3. System.out.println("通過rightPush(K key, V value)方法向最右邊新增元素:" + list);

      9、rightPush(Kkey,Vpivot,Vvalue)

      向集合中第一次出現第二個引數變數元素的右邊新增第三個引數變數的元素值。

      1. redisTemplate.opsForList().rightPush("list","w","r");
      2. list = redisTemplate.opsForList().range("list",0,-1);
      3. System.out.println("通過rightPush(K key, V pivot, V value)方法向最右邊新增元素:" + list);

      10、rightPushAll(Kkey,V...values)

      向右邊批量新增元素。

      1. redisTemplate.opsForList().rightPushAll("list","j","k");
      2. list = redisTemplate.opsForList().range("list",0,-1);
      3. System.out.println("通過rightPushAll(K key, V... values)方法向最右邊批量新增元素:" + list);

      11、rightPushAll(Kkey,Collection<V>values)

      以集合方式向右邊新增元素。

      1. newList.clear();
      2. newList.add("g");
      3. newList.add("h");
      4. redisTemplate.opsForList().rightPushAll("list",newList);
      5. list = redisTemplate.opsForList().range("list",0,-1);
      6. System.out.println("通過rightPushAll(K key, Collection<V> values)方法向最右邊以集合方式批量新增元素:" + list);

      12、rightPushIfPresent(Kkey,Vvalue)

      向已存在的集合中新增元素。

      1. redisTemplate.opsForList().rightPushIfPresent("presentList","d");
      2. list = redisTemplate.opsForList().range("presentList",0,-1);
      3. System.out.println("通過rightPushIfPresent(K key, V value)方法已存在的集合向最右邊新增元素:" + list);

      13、size(Kkey)

      獲取集合長度。

      1. long listLength = redisTemplate.opsForList().size("list");
      2. System.out.println("通過size(K key)方法獲取集合list的長度為:" + listLength);

      14、leftPop(Kkey)

      移除集合中的左邊第一個元素。

      1. Object popValue = redisTemplate.opsForList().leftPop("list");
      2. System.out.print("通過leftPop(K key)方法移除的元素是:" + popValue);
      3. list = redisTemplate.opsForList().range("list",0,-1);
      4. System.out.println(",剩餘的元素是:" + list);

      15、leftPop(Kkey, longtimeout,TimeUnitunit)

      移除集合中左邊的元素在等待的時間裡,如果超過等待的時間仍沒有元素則退出。

      1. popValue = redisTemplate.opsForList().leftPop("presentList",1, TimeUnit.SECONDS);
      2. System.out.print("通過leftPop(K key, long timeout, TimeUnit unit)方法移除的元素是:" + popValue);
      3. list = redisTemplate.opsForList().range("presentList",0,-1);
      4. System.out.println(",剩餘的元素是:" + list);

      16、rightPop(Kkey)

      移除集合中右邊的元素。

      1. popValue = redisTemplate.opsForList().rightPop("list");
      2. System.out.print("通過rightPop(K key)方法移除的元素是:" + popValue);
      3. list = redisTemplate.opsForList().range("list",0,-1);
      4. System.out.println(",剩餘的元素是:" + list);

      17、rightPop(Kkey, longtimeout,TimeUnitunit)

      移除集合中右邊的元素在等待的時間裡,如果超過等待的時間仍沒有元素則退出。

      1. popValue = redisTemplate.opsForList().rightPop("presentList",1, TimeUnit.SECONDS);
      2. System.out.print("通過rightPop(K key, long timeout, TimeUnit unit)方法移除的元素是:" + popValue);
      3. list = redisTemplate.opsForList().range("presentList",0,-1);
      4. System.out.println(",剩餘的元素是:" + list);

      18、rightPopAndLeftPush(KsourceKey,KdestinationKey)

      移除集合中右邊的元素,同時在左邊加入一個元素。

      1. popValue = redisTemplate.opsForList().rightPopAndLeftPush("list","12");
      2. System.out.print("通過rightPopAndLeftPush(K sourceKey, K destinationKey)方法移除的元素是:" + popValue);
      3. list = redisTemplate.opsForList().range("list",0,-1);
      4. System.out.println(",剩餘的元素是:" + list);

      19、rightPopAndLeftPush(KsourceKey,KdestinationKey, longtimeout,TimeUnitunit)

      移除集合中右邊的元素在等待的時間裡,同時在左邊新增元素,如果超過等待的時間仍沒有元素則退出。

      1. popValue = redisTemplate.opsForList().rightPopAndLeftPush("presentList","13",1,TimeUnit.SECONDS);
      2. System.out.println("通過rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit)方法移除的元素是:" + popValue);
      3. list = redisTemplate.opsForList().range("presentList",0,-1);
      4. System.out.print(",剩餘的元素是:" + list);

      20、set(Kkey, longindex,Vvalue)

      在集合的指定位置插入元素,如果指定位置已有元素,則覆蓋,沒有則新增,超過集合下標+n則會報錯。

      1. redisTemplate.opsForList().set("presentList",3,"15");
      2. list = redisTemplate.opsForList().range("presentList",0,-1);
      3. System.out.print("通過set(K key, long index, V value)方法在指定位置新增元素後:" + list);

      21、remove(Kkey, longcount,Objectvalue)

      從儲存在鍵中的列表中刪除等於值的元素的第一個計數事件。count> 0:刪除等於從左到右移動的值的第一個元素;count< 0:刪除等於從右到左移動的值的第一個元素;count = 0:刪除等於value的所有元素。

      1. long removeCount = redisTemplate.opsForList().remove("list",0,"w");
      2. list = redisTemplate.opsForList().range("list",0,-1);
      3. System.out.println("通過remove(K key, long count, Object value)方法移除元素數量:" + removeCount);
      4. System.out.println(",剩餘的元素:" + list);

      22、trim(Kkey, longstart, longend)

      擷取集合元素長度,保留長度內的資料。

      1. redisTemplate.opsForList().trim("list",0,5);
      2. list = redisTemplate.opsForList().range("list",0,-1);
      3. System.out.println("通過trim(K key, long start, long end)方法擷取後剩餘元素:" + list);