Redis字典 Hash
- 一个
key
对应一个hash
;
- 一个
hash
中又是一个key
对应一个value
;
使用redis-cli
47:0>hset user name lc age 18
2
47:0>hget user name
lc
47:0>hmset user1 name lc666 age 18
OK
47:0>hmget user1 name age
1) lc666
2) 18
47:0>hkeys user1
1) name
2) age
47:0>hvals user1
1) lc666
2) 18
47:0>hgetall user1
1) name
2) lc666
3) age
4) 18
47:0>hexists user1 name
1
47:0>hlen user1
2
- 修改已存在的hash,如果修改已经存在的值,值不会改变;
47:0>hsetnx user gender m
1
47:0>hsetnx user name lc666
0
47:0>hget user name
lc
47:0>hdel user name age
2
47:0>hget user name
NULL
Java代码操作
public class RedisHash {
public static void main(String[] args) throws InterruptedException {
Jedis jedis = new Jedis("127.0.0.1", 6379);
String redisKey = "user";
jedis.hset(redisKey, "key1", "value1");
Map<String, String> singleMap = jedis.hgetAll(redisKey);
System.out.println(singleMap.get("key1"));
Map<String, String> allMap = jedis.hgetAll(redisKey);
System.out.println(allMap.get("k2"));
System.out.println(allMap);
Long delResult = jedis.hdel(redisKey, "key1");
System.out.println("删除结果:" + delResult);
System.out.println(jedis.hget(redisKey, "key1"));
}
}
数据结构
- 由数组加链表构成,类似于
Java
中的HashMap
;