您的位置:首页 > 数据库 > Redis

redis 数据类型——Hash SortedSet List

2016-07-30 22:46 429 查看
接着上一篇redis 数据类型——key String Set

Hash –(哈希表)

SortedSet –(有序集合)

List –(列表)

1.Hash – 哈希表

HSET key field value
将key 中field 的值设为 value
如果 key 不存在,则创建
如果key存在,则覆盖

127.0.0.1:6379> hset eason_hoo giftId '100231'
(integer) 1

----------
HGET key field
返回key 中给定 field 值,么有返回nil

127.0.0.1:6379> hset eason_hoo giftId '100231'
(integer) 1
127.0.0.1:6379> hget eason_hoo giftId
"100231"

----------
HGETALL key
返回哈希表 key 中,所有的域和值, 如key 对于value么有值返回nil
对于提高redids 性能使用该方法

127.0.0.1:6379> hget eason_hoo giftId
"100231"
127.0.0.1:6379> hset eason_hoo giftNum 100
(integer) 1
127.0.0.1:6379> hset eason_hoo giftAmont 1000
(integer) 1
127.0.0.1:6379> hgetAll eason_hoo
1) "giftId"
2) "100231"
3) "giftNum"
4) "100"
5) "giftAmont"
6) "1000"

----------
HEXISTS key field
查看 key 中,给定 field 是否存在,存在返回1 否则返回0
127.0.0.1:6379> hexists  eason_hoo giftId
(integer) 1
127.0.0.1:6379> hexists  eason_hoo giftPrice
(integer) 0

----------
HKEYS key
返回哈希表 key 中的所有域

127.0.0.1:6379> hkeys eason_hoo
1) "giftId"
2) "giftNum"
3) "giftAmont"

----------
HLEN key
返回哈希表 key 中域的数量

127.0.0.1:6379> hlen eason_hoo
(integer) 3

----------
HMSET key field value [field value ...]

同时将多个 field-value (域-值)对设置表 key 中,如果表不存在会创建
127.0.0.1:6379> hmset person  name json sex f
OK

----------
HMGET key field [field ...]

返回表 key 中,一个或多个给定域的值。
如果给定key不存在,返回一个 nil 值
127.0.0.1:6379> hmset person  name json sex f
OK
127.0.0.1:6379> hmget person name sex
1) "json"
2) "f"

----------
HSETNX key field value
将表 key 中的域 field 的值设置为 value ,当且仅当域 field 不存在。如果 field 已经存在,该操作无效

127.0.0.1:6379> hsetnx  db  mysql redis
(integer) 1
127.0.0.1:6379> hsetnx  db  mysql redis
(integer) 0

----------
HVALS key
返回表 key 中所有域的值

127.0.0.1:6379> hvals eason_hoo
1) "100231"
2) "100"
3) "1000"
127.0.0.1:6379>

----------
HSTRLEN key field
返回指定key field 字段上value 值得长度


2.SortedSet –(有序集合)

ZADD key score member
添加key值 给member 赋值 score
127.0.0.1:6379> zadd home 9 baidu 10 google 11 jd
(integer) 1

----------
ZCARD key

返回有序集 key 的集合
127.0.0.1:6379> zcard home
(integer) 3

----------
COUNT key min max

返回有key 中 score 值在 min 和 max 之间值
127.0.0.1:6379> zcount home 0 5
(integer) 0
127.0.0.1:6379> zcount home 0 50
(integer) 3

----------
ZRANGE key start stop
返回 key 中指定区间内的成员

127.0.0.1:6379> zrange home 0 50
1) "baidu"
2) "google"
3) "jd"

----------
ZREM key member [member ...]
移除集 key 中的一个或多个成员

127.0.0.1:6379> zrem home jd
(integer) 1
127.0.0.1:6379> zrange home 0 50
1) "baidu"
2) "google"


3.List – 列表

PUSH key value [value ...]
将一个或多个值 value 插入到列表 key 的表头

127.0.0.1:6379> lpush code java oc
(integer) 2
127.0.0.1:6379> type code
list

LPOP key
移除并返回列表 key 的头元素。

127.0.0.1:6379> lpop code
"oc"

LPUSHX key value
当key不存在的时候操作不成功,已经存在了将不做任何操作

127.0.0.1:6379> llen php
(integer) 0
127.0.0.1:6379> lpushx study php
(integer) 0

----------
LSET key index value
将列表 key 下标为 index 的元素的值设置为 value

127.0.0.1:6379> lset code 0 c
OK

LLEN key
返回列表 key 的长度

127.0.0.1:6379> llen code
(integer) 4

----------
RPOP key
移除并返回列表 key 的尾元素

127.0.0.1:6379> rpop code
"java"

RPUSH key value
将一个或多个值 value 插入到列表 key 列表表尾

127.0.0.1:6379> rpush code c++ android
(integer) 5

RPUSHX key value
将 value 插入到列表 key 表尾,并且key 存在

127.0.0.1:6379> rpushx study  sale
(integer) 0


总结:从性能角度考虑,在开发中一般使用Key Hash Set SortedSet 数据类型
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: