RedisTemplate -opsForHash 的用法說明


 

 

1、put(H key, HK hashKey, HV value)

新增hashMap值。

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

 

2、values(H key)

獲取指定變量中的hashMap值。

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

3、entries(H key)

獲取變量中的鍵值對。

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

 

4、get(H key, Object hashKey)

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

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

 

5、hasKey(H key, Object hashKey)

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

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

 

6、keys(H key)

獲取變量中的鍵。

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

 

7、size(H key)

 獲取變量的長度。

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

 

8、increment(H key, HK hashKey, double delta)

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

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

 

9、increment(H key, HK hashKey, long delta)

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

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

 

10、multiGet(H key, Collection<HKhashKey)

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

  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(H key, Map<? extends HK,? extends HV> 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(H key, HK hashKey, HV value)

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

  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(H key, ScanOptions options)

匹配獲取鍵值對,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());

 

14、delete(H key, 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);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM