hashes类型
hashes类型及操作
Redis hash是一个string类型的field和value的映射表。它的添加、删除操作都是0(1)(平均)。hash特别适合用于存储对象。相较于将对象的每个字段存成单个string类型。将一个对象存储在hash类型中会占用更少的内存,并且更方便的存取整个对象。
hset
设置hash field为指定值,如果key不存在,则先创建
127.0.0.1:> hset myhash name guodaxia
(integer)
127.0.0.1:> hget myhash name
"guodaxia"
hsetnx
设置hash field为指定值,如果key不存在,则先创建。如果存在返回0
127.0.0.1:> hsetnx user name name1
(integer)
127.0.0.1:> hget user name
"name1"
127.0.0.1:> hsetnx user name name2
(integer)
127.0.0.1:> hget user name
"name1"
hmset
同时设置hash的多个field
127.0.0.1:> hmset user: name lijie age sex
OK
127.0.0.1:> hget user: name
"lijie"
127.0.0.1:> hget user: age
""
127.0.0.1:> hget user: sex
""
hget
获取hash里面的指定field
127.0.0.1:> hset myhash name guodaxia
(integer)
127.0.0.1:> hget myhash name
"guodaxia"
hmget
获取某个hash里面的一些field
127.0.0.1:> hmget user: name age sex
) "lijie"
) ""
) ""
hincrby
指定的hash field加上给定值
127.0.0.1:> hmget user: name age sex
) "lijie"
) ""
) ""
127.0.0.1:> hincrby user: age -
(integer)
127.0.0.1:> hincrby user: age
(integer)
127.0.0.1:> hget user: age
""
hexists
判断hash里面某个field是否存在
127.0.0.1:> hexists user: name
(integer)
127.0.0.1:> hexists user: parent
(integer)
hlen
返回hash里面所有的field数
127.0.0.1:> hlen user:
(integer)
127.0.0.1:> hlen myhash
(integer)
hdel
删除指定hash的field
127.0.0.1:> hget user: age
""
127.0.0.1:> hdel user: age
(integer)
127.0.0.1:> hget user: age
(nil)
hkeys
返回hash里面所有的field
127.0.0.1:> hlen user:
(integer)
127.0.0.1:> hkeys user:
) "name"
) "sex"
127.0.0.1:> hkeys myhash
) "name"
hvals
返回此hash的所有value
127.0.0.1:> hvals user:
) "lijie"
) ""
hgerall
获取某个hash中全部的field和value
127.0.0.1:> hgetall user:
) "name"
) "lijie"
) "sex"
) ""