04_Redis_Hash command

One: Redis hash (Hash)

 1.1: Redis hash is a mapping table of field and value of string type, and hash is especially suitable for storing objects.

 1.2: Each hash in Redis can store 2 32 - 1 key-value pairs (more than 4 billion).

 1.3: The Hash type in Redis can be regarded as a map container with String Key and String Value. So this type is very suitable for storing information about value objects. Such as Username, Password and Age. If the Hash contains few fields, then the number of the type

    Data will also take up very little disk space. Each Hash can store 4294967295 key-value pairs.

      Hash  ---->  {username:”张三”,age:”18”,sex:”man”}------javaBean

          Hash features: takes up very little disk space.

Two: Redis hash command

  2.1: hset key field value: set the value of the field field in the hash table key to value;

  2.2: hget key field: Get the value of the specified field stored in the hash table  

redis 127.0.0.1:6379> HSET myhash field1 "foo"
OK
redis 127.0.0.1:6379> HGET myhash field1
"foo"

redis 127.0.0.1:6379> HSET website google "www.g.cn"        # Set up a new domain
(integer) 1

redis 127.0.0.1:6379>HSET website google "www.google.com" # Overwrite an old domain
(integer) 0 # If the field is a new field in the hash table and the value is set successfully, return 1; if the field field in the hash table already exists and the old value has been overwritten by the new value, return 0 
redis 127.0.0.1: 6379> HGET website google "www.google.com"

  2.3: hmset key field1 value1 [field2 value2]: Set multiple field-value (field-value) pairs to the hash table key at the same time

  2.4: hmget key field1 [field2] : Get the values ​​of multiple fileds in the key

redis 127.0.0.1:6379> HMSET myhash2 uname zhangsan age 18 sex man
OK
redis 127.0.0.1:6379> HMGET myhash2 uname age
1>"zhangsan "
2>"18 "

  2.5: HGETAL L key: Get all fields and values ​​of the specified key in the hash table

redis 127.0.0.1:6379> HGETALL myhash2
1) "uname"
2) "zhangsan"
3) "age"
4) "18"
5) "sex"
6) "man"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325016059&siteId=291194637