【Azure Redis 快取 Azure Cache For Redis】使用Redis自帶redis-benchmark.exe命令測試Azure Redis的效能
1、put(Hkey,
HKhashKey,
HVvalue)
新增hashMap值。
- redisTemplate.opsForHash().put("hashValue","map1","map1-1");
- redisTemplate.opsForHash().put("hashValue","map2","map2-2");
2、values(Hkey)
獲取指定變數中的hashMap值。
- List<Object> hashList = redisTemplate.opsForHash().values("hashValue");
-
System.out.println("通過values(H key)方法獲取變數中的hashMap值:" + hashList);
3、entries(Hkey)
獲取變數中的鍵值對。
- Map<Object,Object> map = redisTemplate.opsForHash().entries("hashValue");
- System.out.println("通過entries(H key)方法獲取變數中的鍵值對:" + map);
4、get(Hkey,ObjecthashKey)
獲取變數中的指定map鍵是否有值,如果存在該map鍵則獲取值,沒有則返回null。
-
Object mapValue = redisTemplate.opsForHash().get("hashValue","map1");
- System.out.println("通過get(H key, Object hashKey)方法獲取map鍵的值:" + mapValue);
5、hasKey(Hkey,
ObjecthashKey)
判斷變數中是否有指定的map鍵。
- boolean hashKeyBoolean = redisTemplate.opsForHash().hasKey("hashValue","map3");
-
System.out.println("通過hasKey(H key, Object hashKey)方法判斷變數中是否存在map鍵:" + hashKeyBoolean);
6、keys(Hkey)
獲取變數中的鍵。
- Set<Object> keySet = redisTemplate.opsForHash().keys("hashValue");
- System.out.println("通過keys(H key)方法獲取變數中的鍵:" + keySet);
7、size(Hkey)
獲取變數的長度。
- long hashLength = redisTemplate.opsForHash().size("hashValue");
- System.out.println("通過size(H key)方法獲取變數的長度:" + hashLength);
8、increment(Hkey,HKhashKey, doubledelta)
使變數中的鍵以double值的大小進行自增長。
- double hashIncDouble = redisTemplate.opsForHash().increment("hashInc","map1",3);
- System.out.println("通過increment(H key, HK hashKey, double delta)方法使變數中的鍵以值的大小進行自增長:" + hashIncDouble);
9、increment(Hkey,HKhashKey, longdelta)
使變數中的鍵以long值的大小進行自增長。
- long hashIncLong = redisTemplate.opsForHash().increment("hashInc","map2",6);
- System.out.println("通過increment(H key, HK hashKey, long delta)方法使變數中的鍵以值的大小進行自增長:" + hashIncLong);
10、multiGet(Hkey,
Collection<HK>hashKeys)
以集合的方式獲取變數中的值。
- List<Object> list = new ArrayList<Object>();
- list.add("map1");
- list.add("map2");
- List mapValueList = redisTemplate.opsForHash().multiGet("hashValue",list);
- System.out.println("通過multiGet(H key, Collection<HK> hashKeys)方法以集合的方式獲取變數中的值:"+mapValueList);
11、putAll(Hkey,Map<? extendsHK,? extendsHV>m)
以map集合的形式新增鍵值對。
- Map newMap = new HashMap();
- newMap.put("map3","map3-3");
- newMap.put("map5","map5-5");
- redisTemplate.opsForHash().putAll("hashValue",newMap);
- map = redisTemplate.opsForHash().entries("hashValue");
- System.out.println("通過putAll(H key, Map<? extends HK,? extends HV> m)方法以map集合的形式新增鍵值對:" + map);
12、putIfAbsent(Hkey,HKhashKey,HVvalue)
如果變數值存在,在變數中可以新增不存在的的鍵值對,如果變數不存在,則新增一個變數,同時將鍵值對新增到該變數。
- redisTemplate.opsForHash().putIfAbsent("hashValue","map6","map6-6");
- map = redisTemplate.opsForHash().entries("hashValue");
- System.out.println("通過putIfAbsent(H key, HK hashKey, HV value)方法新增不存在於變數中的鍵值對:" + map);
13、scan(Hkey,ScanOptionsoptions)
匹配獲取鍵值對,ScanOptions.NONE為獲取全部鍵對,ScanOptions.scanOptions().match("map1").build() 匹配獲取鍵位map1的鍵值對,不能模糊匹配。
- Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("hashValue",ScanOptions.scanOptions().match("map1").build());
- //Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("hashValue",ScanOptions.NONE);
- while (cursor.hasNext()){
- Map.Entry<Object,Object> entry = cursor.next();
- System.out.println("通過scan(H key, ScanOptions options)方法獲取匹配鍵值對:" + entry.getKey() + "---->" + entry.getValue());
- }
14、delete(Hkey,Object...hashKeys)
刪除變數中的鍵值對,可以傳入多個引數,刪除多個鍵值對。
- redisTemplate.opsForHash().delete("hashValue","map1","map2");
- map = redisTemplate.opsForHash().entries("hashValue");
-
System.out.println("通過delete(H key, Object... hashKeys)方法刪除變數中的鍵值對後剩餘的:" + map);
1、set(Kkey,Vvalue)
新增一個字串型別的值,key是鍵,value是值。
redisTemplate.opsForValue().set("stringValue","bbb");
2、get(Objectkey)
獲取key鍵對應的值。
- String stringValue = redisTemplate.opsForValue().get("stringValue")+"";
- System.out.println("通過get(Object key)方法獲取set(K key, V value)方法新增的字串值:" + stringValue);
3、append(Kkey,Stringvalue)
在原有的值基礎上新增字串到末尾。
- redisTemplate.opsForValue().append("stringValue","aaa");
- String stringValueAppend = redisTemplate.opsForValue().get("stringValue")+"";
- System.out.println("通過append(K key, String value)方法修改後的字串:"+stringValueAppend);
4、get(Kkey, longstart, longend)
擷取key鍵對應值得字串,從開始下標位置開始到結束下標的位置(包含結束下標)的字串。
- String cutString = redisTemplate.opsForValue().get("stringValue",0,3);
- System.out.println("通過get(K key, long start, long end)方法獲取擷取的字串:"+cutString);
5、getAndSet(Kkey,Vvalue)
獲取原來key鍵對應的值並重新賦新值。
- 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(Kkey, longoffset, booleanvalue)
key鍵對應的值value對應的ascii碼,在offset的位置(從左向右數)變為value。
- redisTemplate.opsForValue().setBit("stringValue",1,false);
- newStringValue = redisTemplate.opsForValue().get("stringValue")+"";
- System.out.println("通過setBit(K key,long offset,boolean value)方法修改過後的值:"+newStringValue);
7、getBit(Kkey, longoffset)
判斷指定的位置ASCII碼的bit位是否為1。
- boolean bitBoolean = redisTemplate.opsForValue().getBit("stringValue",1);
- System.out.println("通過getBit(K key,long offset)方法判斷指定bit位的值是:" + bitBoolean);
8、size(Kkey)
獲取指定字串的長度。
- Long stringValueLength = redisTemplate.opsForValue().size("stringValue");
- System.out.println("通過size(K key)方法獲取字串的長度:"+stringValueLength);
9、increment(Kkey, doubledelta)
以增量的方式將double值儲存在變數中。
- double stringValueDouble = redisTemplate.opsForValue().increment("doubleValue",5);
- System.out.println("通過increment(K key, double delta)方法以增量方式儲存double值:" + stringValueDouble);
10、increment(Kkey, longdelta)
以增量的方式將long值儲存在變數中。
- double stringValueLong = redisTemplate.opsForValue().increment("longValue",6);
- System.out.println("通過increment(K key, long delta)方法以增量方式儲存long值:" + stringValueLong);
11、setIfAbsent(Kkey,Vvalue)
如果鍵不存在則新增,存在則不改變已經有的值。
- 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(Kkey,Vvalue, longtimeout,TimeUnitunit)
設定變數值的過期時間。
- 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(Kkey,Vvalue, longoffset)
覆蓋從指定位置開始的值。
- 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<? extendsK,? extendsV>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);
1、leftPush(Kkey,Vvalue)
在變數左邊新增元素值。
- redisTemplate.opsForList().leftPush("list","a");
- redisTemplate.opsForList().leftPush("list","b");
- redisTemplate.opsForList().leftPush("list","c");
2、index(Kkey, longindex)
獲取集合指定位置的值。
- String listValue = redisTemplate.opsForList().index("list",1) + "";
- System.out.println("通過index(K key, long index)方法獲取指定位置的值:" + listValue);
3、range(Kkey, longstart, longend)
獲取指定區間的值。
- List<Object> list = redisTemplate.opsForList().range("list",0,-1);
- System.out.println("通過range(K key, long start, long end)方法獲取指定範圍的集合值:"+list);
4、leftPush(Kkey,Vpivot,Vvalue)
把最後一個引數值放到指定集合的第一個出現中間引數的前面,如果中間引數值存在的話。否則返回-1
- redisTemplate.opsForList().leftPush("list","a","n");
- list = redisTemplate.opsForList().range("list",0,-1);
- System.out.println("通過leftPush(K key, V pivot, V value)方法把值放到指定引數值前面:" + list);
5、leftPushAll(Kkey,V...values)
向左邊批量新增引數元素。
- redisTemplate.opsForList().leftPushAll("list","w","x","y");
- list = redisTemplate.opsForList().range("list",0,-1);
- System.out.println("通過leftPushAll(K key, V... values)方法批量新增元素:" + list);
6、leftPushAll(Kkey,Collection<V>values)
以集合的方式向左邊批量新增元素。
- List newList = new ArrayList();
- newList.add("o");
- newList.add("p");
- newList.add("q");
- redisTemplate.opsForList().leftPushAll("list",newList);
- list = redisTemplate.opsForList().range("list",0,-1);
- System.out.println("通過leftPushAll(K key, Collection<V> values)方法以集合的方式批量新增元素:" + list);
7、leftPushIfPresent
(Kkey,
Vvalue)
如果存在集合則新增元素。
- redisTemplate.opsForList().leftPushIfPresent("presentList","o");
- list = redisTemplate.opsForList().range("presentList",0,-1);
- System.out.println("通過leftPushIfPresent(K key, V value)方法向已存在的集合新增元素:" + list);
8、rightPush(Kkey,Vvalue)
向集合最右邊新增元素。
- redisTemplate.opsForList().rightPush("list","w");
- list = redisTemplate.opsForList().range("list",0,-1);
- System.out.println("通過rightPush(K key, V value)方法向最右邊新增元素:" + list);
9、rightPush(Kkey,Vpivot,Vvalue)
向集合中第一次出現第二個引數變數元素的右邊新增第三個引數變數的元素值。
- redisTemplate.opsForList().rightPush("list","w","r");
- list = redisTemplate.opsForList().range("list",0,-1);
- System.out.println("通過rightPush(K key, V pivot, V value)方法向最右邊新增元素:" + list);
10、rightPushAll(Kkey,V...values)
向右邊批量新增元素。
- redisTemplate.opsForList().rightPushAll("list","j","k");
- list = redisTemplate.opsForList().range("list",0,-1);
- System.out.println("通過rightPushAll(K key, V... values)方法向最右邊批量新增元素:" + list);
11、rightPushAll(Kkey,Collection<V>values)
以集合方式向右邊新增元素。
- newList.clear();
- newList.add("g");
- newList.add("h");
- redisTemplate.opsForList().rightPushAll("list",newList);
- list = redisTemplate.opsForList().range("list",0,-1);
- System.out.println("通過rightPushAll(K key, Collection<V> values)方法向最右邊以集合方式批量新增元素:" + list);
12、rightPushIfPresent(Kkey,Vvalue)
向已存在的集合中新增元素。
- redisTemplate.opsForList().rightPushIfPresent("presentList","d");
- list = redisTemplate.opsForList().range("presentList",0,-1);
- System.out.println("通過rightPushIfPresent(K key, V value)方法已存在的集合向最右邊新增元素:" + list);
13、size(Kkey)
獲取集合長度。
- long listLength = redisTemplate.opsForList().size("list");
- System.out.println("通過size(K key)方法獲取集合list的長度為:" + listLength);
14、leftPop(Kkey)
移除集合中的左邊第一個元素。
- Object popValue = redisTemplate.opsForList().leftPop("list");
- System.out.print("通過leftPop(K key)方法移除的元素是:" + popValue);
- list = redisTemplate.opsForList().range("list",0,-1);
- System.out.println(",剩餘的元素是:" + list);
15、leftPop(Kkey, longtimeout,TimeUnitunit)
移除集合中左邊的元素在等待的時間裡,如果超過等待的時間仍沒有元素則退出。
- popValue = redisTemplate.opsForList().leftPop("presentList",1, TimeUnit.SECONDS);
- System.out.print("通過leftPop(K key, long timeout, TimeUnit unit)方法移除的元素是:" + popValue);
- list = redisTemplate.opsForList().range("presentList",0,-1);
- System.out.println(",剩餘的元素是:" + list);
16、rightPop
(Kkey)
移除集合中右邊的元素。
- popValue = redisTemplate.opsForList().rightPop("list");
- System.out.print("通過rightPop(K key)方法移除的元素是:" + popValue);
- list = redisTemplate.opsForList().range("list",0,-1);
- System.out.println(",剩餘的元素是:" + list);
17、rightPop(Kkey, longtimeout,TimeUnitunit)
移除集合中右邊的元素在等待的時間裡,如果超過等待的時間仍沒有元素則退出。
- popValue = redisTemplate.opsForList().rightPop("presentList",1, TimeUnit.SECONDS);
- System.out.print("通過rightPop(K key, long timeout, TimeUnit unit)方法移除的元素是:" + popValue);
- list = redisTemplate.opsForList().range("presentList",0,-1);
- System.out.println(",剩餘的元素是:" + list);
18、rightPopAndLeftPush
(KsourceKey,
KdestinationKey)
移除集合中右邊的元素,同時在左邊加入一個元素。
- popValue = redisTemplate.opsForList().rightPopAndLeftPush("list","12");
- System.out.print("通過rightPopAndLeftPush(K sourceKey, K destinationKey)方法移除的元素是:" + popValue);
- list = redisTemplate.opsForList().range("list",0,-1);
- System.out.println(",剩餘的元素是:" + list);
19、rightPopAndLeftPush(KsourceKey,KdestinationKey, longtimeout,TimeUnitunit)
移除集合中右邊的元素在等待的時間裡,同時在左邊新增元素,如果超過等待的時間仍沒有元素則退出。
- popValue = redisTemplate.opsForList().rightPopAndLeftPush("presentList","13",1,TimeUnit.SECONDS);
- System.out.println("通過rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit)方法移除的元素是:" + popValue);
- list = redisTemplate.opsForList().range("presentList",0,-1);
- System.out.print(",剩餘的元素是:" + list);
20、set
(Kkey, longindex,
Vvalue)
在集合的指定位置插入元素,如果指定位置已有元素,則覆蓋,沒有則新增,超過集合下標+n則會報錯。
- redisTemplate.opsForList().set("presentList",3,"15");
- list = redisTemplate.opsForList().range("presentList",0,-1);
- System.out.print("通過set(K key, long index, V value)方法在指定位置新增元素後:" + list);
21、remove(Kkey, longcount,Objectvalue)
從儲存在鍵中的列表中刪除等於值的元素的第一個計數事件。count> 0:刪除等於從左到右移動的值的第一個元素;count< 0:刪除等於從右到左移動的值的第一個元素;count = 0:刪除等於value的所有元素。
- long removeCount = redisTemplate.opsForList().remove("list",0,"w");
- list = redisTemplate.opsForList().range("list",0,-1);
- System.out.println("通過remove(K key, long count, Object value)方法移除元素數量:" + removeCount);
- System.out.println(",剩餘的元素:" + list);
22、trim
(Kkey, longstart, longend)
擷取集合元素長度,保留長度內的資料。
- redisTemplate.opsForList().trim("list",0,5);
- list = redisTemplate.opsForList().range("list",0,-1);
- System.out.println("通過trim(K key, long start, long end)方法擷取後剩餘元素:" + list);