hash操作
redisTemplate.opsForHash()
@Autowired
private RedisTemplate redisTemplate;
redis的哈希类型指的是键值本身又是一个键值对结构:
如:vaule = {{key1,value1},{key2,value2}...{keyn,valuen}}
相对于java可以理解为map:Key=>(Map)
redisTemplate
//以下表格使用hash代替redisTemplate.opsForHash() :
HashOperations hash = redisTemplate.opsForHash();
命令操作返回值
hash.delete(H key, Object... hashKeys)
删除,可以传入多个map的key
Long
hash.hasKey(key, hashKey)
查看hash中的某个hashKey是否存在
Boolean
hash.put(key, hashKey, value)
设置值,hashKay相当于map的key
void
hash.get(key, hashKey)
获取值
Object(HV 泛型约束对象)
hash.multiGet(H key, Collection hashKeys)
批量获取集合中的key对应的值
List
hash.increment(H key, HK hashKey, long delta)
对值进行+(delta值)操作
Long
hash.increment(H key, HK hashKey, double delta)
~
double
hash.keys(key)
返回map内hashKey的集合
Set
hash.lengthOfValue(H key, HK hashKey)
返回查询键关联的值的长度,为null则返回0
Long
hash.size(H key)
获取hash类型key的size
Long
hash.putAll(H key, Map extends HK, ? extends HV> m)
相当于map的putAll
void
hash.put(H key, HK hashKey, HV value)
添加hashKey-value
void
hash.putIfAbsent(H key, HK hashKey, HV value)
仅当hashKey不存在时设置值
Boolean
hash.values(key)
返回value的集合
List
hase.entries(H key)
获取map
Map
hash.scan(H key, ScanOptions options)
基于游标的迭代查询
Cursor>(返回的Cursor要手动关闭,见下面示例2)
hash.getOperations()
返回RedisOperation,它就是redis操作的接口
RedisOperations
示例测试操作1:
//key
String k = "phoneMap";//map的key
String hashKey = "13838383838";
Map phoneMap = new HashMap<>();
phoneMap.put("13838383839","1");
phoneMap.put(hashKey,"2");//添加map//redisTemplate.opsForHash().putAll(k, phoneMap);//在key的value中添加单个hash值
redisTemplate.opsForHash().put(k, "19199999999","2");//获取map
Map m1 =redisTemplate.opsForHash().entries(k);
Object o=redisTemplate.opsForHash().get(k, hashKey);
log.info("get key:{}", o);
log.info("map:{}", m1);
log打印的结果:
2019-12-13 15:45:36.836 INFO 4156 --- [ main] : get key:null
2019-12-13 15:45:36.836 INFO 4156 --- [ main] : map:{13838121333=1, 13838121556=2, 19199999999=2}
如果这里使用了redisTemplate.opsForHash().putAll(k, phoneMap); ,则get key打印的结果就是2。这里打印的map是包含之前测试的值。
示例测试操作1:
@Testpublic voidhashTest(){
String k= "phoneMap";
String hashKey= "19199999999";
HashOperations hash =redisTemplate.opsForHash();
hash.increment(k,hashKey,1);
log.info("19199999999:{}", hash.get(k,hashKey));
Set set=hash.keys(k);
log.info("phoneMap:{}", set);
Long sizeLong=hash.size(k);
log.info("sizeLong:{}",sizeLong);
Cursor> cursor = hash.scan(k,ScanOptions.scanOptions().count(1000).build());while(cursor.hasNext()){
Map.Entry entry =cursor.next();
log.info("entry:{}:{}", entry.getKey(), entry.getValue());
}try{if (!cursor.isClosed()){
cursor.close();
}
}catch(IOException e) {
log.error("关闭cursor异常");
e.printStackTrace();
}
}
打印结果:
2019-12-13 16:59:35.573 INFO 1540 --- [ main] : 19199999999:6
2019-12-13 16:59:35.577 INFO 1540 --- [ main] : phoneMap:[13838121373, 13838121556, 19199999999, 13838383839, 13838383838]2019-12-13 16:59:35.578 INFO 1540 --- [ main] : sizeLong:5
2019-12-13 16:59:35.587 INFO 1540 --- [ main] : entry:13838121373:1
2019-12-13 16:59:35.587 INFO 1540 --- [ main] : entry:13838121556:2
2019-12-13 16:59:35.587 INFO 1540 --- [ main] : entry:19199999999:6
2019-12-13 16:59:35.587 INFO 1540 --- [ main] : entry:13838383839:1
2019-12-13 16:59:35.587 INFO 1540 --- [ main] : entry:13838383838:2