Redis字典 Hash
- 一個(gè)
key
對(duì)應(yīng)一個(gè)hash
;
- 一個(gè)
hash
中又是一個(gè)key
對(duì)應(yīng)一個(gè)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
- 查詢某個(gè)
hash
中是否存在某個(gè)key
严衬;
47:0>hexists user1 name
1
- 查詢某個(gè)
hash
中元素個(gè)數(shù):
47:0>hlen user1
2
- 修改已存在的hash,如果修改已經(jīng)存在的值笆呆,值不會(huì)改變请琳;
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("刪除結(jié)果:" + delResult);
System.out.println(jedis.hget(redisKey, "key1"));
}
}
數(shù)據(jù)結(jié)構(gòu)
- 由數(shù)組加鏈表構(gòu)成赠幕,類似于
Java
中的HashMap
俄精;