Redis hash类型
Redis hash类型是键值对的集合类型。经常被用来做对象的映射。
Redis hash命令
hash格式如下 hash_key_name field value field value 。 field必须和value成对出现。
hset命令
hset命令用来创建hash对象,或者修改hash中field对应的value.如果key不存在,则创建该key对应的hash对象,返回新增的field值。如果hash中对应的field存在则修改对应的值,返回0。
127.0.0.1:6379> help hset HSET key field value [field value ...] summary: Set the string value of a hash field since: 2.0.0 group: hash 127.0.0.1:6379> hset user:1 name zhangsan age 42 (integer) 2 127.0.0.1:6379> hset user:2 name lisi age 28 (integer) 2 127.0.0.1:6379> hset user:1 name wangwu age 50 (integer) 0 127.0.0.1:6379> hset user:1 name wangwu age 50 sex m (integer) 1
hget命令
hget命令 用来获取hash中field对应的value。如果hash中field不存在或者hash不存在则返回nil。
127.0.0.1:6379> hget user:1 agea (nil) 127.0.0.1:6379> help hget HGET key field summary: Get the value of a hash field since: 2.0.0 group: hash 127.0.0.1:6379> hset user:1 name wangwu age 50 sex m (integer) 1 127.0.0.1:6379> hget user:1 name "wangwu" 127.0.0.1:6379> hget user:1 age "50" 127.0.0.1:6379> hget user:1 agea (nil)
hsetnx命令
hsetnx命令只有当field的field在hash中不存在时才创建该field,并返回创建的filed个数。如果hash中field已经存在则什么都不做,返回0。
127.0.0.1:6379> help hsetnx HSETNX key field value summary: Set the value of a hash field, only if the field does not exist since: 2.0.0 group: hash 127.0.0.1:6379> hsetnx nxset keynx 1 (integer) 1 127.0.0.1:6379> hget nxset keynx "1" 127.0.0.1:6379> hsetnx nxset keynx 2 (integer) 0 127.0.0.1:6379> hget nxset keynx "1" 127.0.0.1:6379> hsetnx nxset keynx2 2 (integer) 1 127.0.0.1:6379> hget nxset keynx2 "2" 127.0.0.1:6379>
hmset命令
hmset命令已经由hset命令替换,语法和hset类似
127.0.0.1:6379> help hmset HMSET key field value [field value ...] summary: Set multiple hash fields to multiple values since: 2.0.0 group: hash 127.0.0.1:6379> hmset mhash mkey1 mv1 mkey2 mv2 OK 127.0.0.1:6379> hget mhash mkey1 "mv1" 127.0.0.1:6379> hget mhash mkey2 "mv2" 127.0.0.1:6379>
hmget命令
hmget命令用来获取多个fields对应的value,如果某个fields不存在该field对应的value则返回nil。如果该hash对象不存在,则每个fields都返回nil。
127.0.0.1:6379> hset user:1 name wangwu age 50 sex m (integer) 3 127.0.0.1:6379> hset user:2 name lisi age 28 (integer) 2 127.0.0.1:6379> hmget user:1 name age sex 1) "wangwu" 2) "50" 3) "m" 127.0.0.1:6379> hmget user:2 name age 1) "lisi" 2) "28"
hlen命令
hlen命令 获取hash中 fields的个数。
127.0.0.1:6379> help hlen HLEN key summary: Get the number of fields in a hash since: 2.0.0 group: hash 127.0.0.1:6379> hset user:1 name wangwu age 50 sex m (integer) 3 127.0.0.1:6379> hlen user:1 (integer) 3 127.0.0.1:6379> hlen user:2 (integer) 0 127.0.0.1:6379>
hstrlen命令
hstrlen命令 获取hash中具体field对应的value字符串的长度。
127.0.0.1:6379> help hstrlen HSTRLEN key field summary: Get the length of the value of a hash field since: 3.2.0 group: hash 127.0.0.1:6379> hset user:1 name wangwu age 50 sex m (integer) 3 127.0.0.1:6379> hstrlen user:1 name (integer) 6 127.0.0.1:6379> hstrlen user:1 age (integer) 2 127.0.0.1:6379> hstrlen user:1 sex (integer) 1 127.0.0.1:6379>
hkeys命令
hkeys命令获取hash中所有的fields。当hash不存在时,返回empty array, hash存在则返回所有的fields。
127.0.0.1:6379> help hkeys HKEYS key summary: Get all the fields in a hash since: 2.0.0 group: hash 127.0.0.1:6379> hkeys not:exits:hash (empty array) 127.0.0.1:6379> hset user:1 name wangwu age 50 sex m (integer) 3 127.0.0.1:6379> hkeys user:1 1) "name" 2) "age" 3) "sex"
hvals命令
hvals获取一个hash中所有fields的value,如果hash不存在则返回empty array。
127.0.0.1:6379> help hvals HVALS key summary: Get all the values in a hash since: 2.0.0 group: hash 127.0.0.1:6379> hvals not:exists:hash (empty array) 127.0.0.1:6379> hset user:1 name wangwu age 50 sex m (integer) 3 127.0.0.1:6379> hvals user:1 1) "wangwu" 2) "50" 3) "m"
hgetall命令
hgetall命令获取hashs中所有的fields信息,包含 field和对应value。
127.0.0.1:6379> help hgetall HGETALL key summary: Get all the fields and values in a hash since: 2.0.0 group: hash 127.0.0.1:6379> hgetall user:1 1) "name" 2) "wangwu" 3) "age" 4) "50" 5) "sex" 6) "m" 127.0.0.1:6379> hgetall not:exist:hash (empty array) 127.0.0.1:6379>
hdel命令
hdel命令删除hash中的field。如果field不存在或hash不存在则返回0。如果存在则返回删除的fields个数。
127.0.0.1:6379> help hdel HDEL key field [field ...] summary: Delete one or more hash fields since: 2.0.0 group: hash 127.0.0.1:6379> hset user:1 name wangwu age 50 sex m (integer) 1 127.0.0.1:6379> hgetall user:1 1) "name" 2) "wangwu" 3) "age" 4) "50" 5) "sex" 6) "m" 127.0.0.1:6379> hdel user:1 name age no:sex (integer) 2 127.0.0.1:6379> hgetall user:1 1) "sex" 2) "m" 127.0.0.1:6379> hdel not:exist:hash not:exist:field (integer) 0
hexists命令
hexists命令判断field是否存在hash中,存在则返回1,否则返回0。
127.0.0.1:6379> help hexists HEXISTS key field summary: Determine if a hash field exists since: 2.0.0 group: hash 127.0.0.1:6379> hkeys user:1 1) "sex" 127.0.0.1:6379> hexists user:1 name (integer) 0 127.0.0.1:6379> hexists user:1 sex (integer) 1
hincrby命令
hincrby命令对hash中field对应的数字value进行整数自增或整数自减,自增increment则为正整数,自减increment为负整数。
127.0.0.1:6379> help hincrby HINCRBY key field increment summary: Increment the integer value of a hash field by the given number since: 2.0.0 group: hash 127.0.0.1:6379> hincrby user:1 age 1 (integer) 25 127.0.0.1:6379> hget user:1 age "25" 127.0.0.1:6379> hincrby user:1 age -1 (integer) 24 127.0.0.1:6379> hget user:1 age "24" 127.0.0.1:6379>
hincrbyfloat命令
hincrbyfloat命令对hash中field对应的浮点数或整数进行浮点数自增或自减。自增increment则为正浮点数,自减increment为负浮点数。
127.0.0.1:6379> help hincrbyfloat HINCRBYFLOAT key field increment summary: Increment the float value of a hash field by the given amount since: 2.6.0 group: hash 127.0.0.1:6379> hset student:1 name wangling major automation GPA 3.5 (integer) 3 127.0.0.1:6379> hget student:1 GPA "3.5" 127.0.0.1:6379> hincrbyfloat student:1 GPA 0.3 "3.8" 127.0.0.1:6379> hget student:1 GPA "3.8" 127.0.0.1:6379> hincrbyfloat student:1 GPA 0.2 "4" 127.0.0.1:6379> hget student:1 GPA "4"
hrandfield命令
hrandfield命令随机返回hash中 count个 field。后面可以 选择接收一个参数或者两个参数 count和withvalue。
127.0.0.1:6379> help hrandfield HRANDFIELD key [count [WITHVALUES]] summary: Get one or multiple random fields from a hash since: 6.2.0 group: hash 127.0.0.1:6379> hrandfield student:1 "major" 127.0.0.1:6379> hrandfield student:1 1 withvalues 1) "name" 2) "wangling" 127.0.0.1:6379> hrandfield student:1 2 1) "name" 2) "major" 127.0.0.1:6379> hrandfield student:1 2 withvalues 1) "GPA" 2) "4" 3) "name" 4) "wangling"
hscan命令
hscan命令迭代 hash中的field和values。
127.0.0.1:6379> help hscan HSCAN key cursor [MATCH pattern] [COUNT count] summary: Incrementally iterate hash fields and associated values since: 2.8.0 group: hash 127.0.0.1:6379> hscan student:1 0 1) "0" 2) 1) "name" 2) "wangling" 3) "major" 4) "automation" 5) "GPA" 6) "4" 127.0.0.1:6379> hscan student:1 0 match major 1) "0" 2) 1) "major" 2) "automation"
标签:127.0,hash,field,0.1,Redis,6379,user,类型 From: https://www.cnblogs.com/ryanpan/p/16582749.html