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

Redis中五种数据类型总结

2014-08-04 00:35 453 查看
redis可以存储5种基本的数据类型,主要包括:String、Hash、List、Set、Sort Set。

以下就对存储的每种类型进行一个总结,以及记录他们基本的命令和各个命令之间的区别:

1)、String类型

1、set [设置key-value键值对]

> set name "John"

2、setnx [设置Key-value键值对,nx代表not exist,表示在键不存在的情况下去设置,如果键存在返回0,否则返回1]

> set name "malibin"

OK

> get name

"malibin"

> setnx name "xuminke"

(integer) 0

> setnx email mlb@163.com

(integer) 1

> get email

"mlb@163.com"

3、setex [设置Key-value键值对,ex表示设置键的有效时间,当超过设置的时间该键失效]

> setex name 5 "malibin" [键name的有效时间为5秒,超过5秒键失效]

4、setrange [从某个键的值的某个位置开始替换]

> setnx email "mlb0903@163.com"

> setrange email 9 "gmail.com"

5、 mset [批量设置Key-value键值对]

> mset key1 "test1" key2 "test2" key3 "test3" key4 "test4"

>get key1

"test1"

6、msetnx [同setnx,这里需要注意的是如果有其中一个键没有设置成功,则所有的键都不会设置成功]

7、msetex [同setex]

8、getset [对一个键设置新值返回久值]

>set name "old-value"

>get name

"old-value"

>getset name "new-value"

>get name

"new-value"

9、get [获得某个键对应的值,跟set命令相对]

>set name "set commond"

>get name

"set commond"

10、mget [批量获取]

>mset key1 "test1" key2 "test2" key3 "test3" key4 "test4"

>mget key1 key2 key3 key4

11、getrange [从某个键对应的值的某个位置开始获取]

>set name "0123456789"

>getrange name 0 5 [返回第0-5之间的字符]

12、incr [对某个键值对进行递增,自增1]

>set index 10

>incr index

11

13、incrby [对某个键值对进行递增,增加任意值]

>set index 10

>incrby index 5

15

14、decr [跟incr相反,自减1]

>set index 10

>decr index

15、decrby [跟incrby相反,自减任意值]

>set index 10

>decrby 5

2)、Hash类型

1、hset [设置hash]

>hset user:001 name "John"

2、hget [获得hash的某个field的值]

>hget user:001 name

"John"

3、hsetnx [其作用类似于String类型的setnx]

>hsetnx user:001 name "John-new" [左边的命令不会设置成功,原因是name属性已经存在]

4、hmset [批量设置]

>hmset user:002 name "Lyce" age 20 sex meal

5、hmget [批量取得]

>hmget user:002 name age sex

6、hincrby [对hash的某个字段自增]

>hincrby user:002 age 5

>hget user:002 age

25

7、hexists [判断hash中的每个字段是否存在]

>hexists user:001 age

0 [user:001中并没有age这个属性]

>hexists user:001 name

1

8、hlen [返回hash的长度]

>hlen user:002

9、hdel [删除hash中的每个字段]

>hdel user:002 age [删除hash user:002中的age字段]

10、hkeys [获得所有的key值]

>hkeys user:002

11、kvals [获得所有的key对应的value值]

>hvals user:002

12、hgetall [获得hash中的key值和value值]

>hgetall user:002

3)、List类型

List类型是redis中的链表,通过向链表头部和尾部插入数据或者删除数据,即使在一个链表中已经有几百万条数据,该操作也可以在常量时间内完成,但是如果向链表中间插入或者删除元素,其效率不高。

1、lpush [项链表的头部插入元素]

>lpush list1 "one"

2、lpushx [只有当链表存在时才会在链表的头部插入元素]

>lpushx list2 "one" [list2这个链表现在根本不存在所以不会有任何操作]

3、lrange key start stop [获得链表从start到stop之间的元素]

>lpush list1 "two"

>lpush list1 "three"

>lpush list1 0 -1 [从头到尾返回list1中的所有元素]

4、lpop [弹出链表中的头部元素]

>lpop list1

"three"

5、llen [返回链表的长度]

>llen list1

2

6、lrem list count value [表示从list链表中删除count个value元素]

>lpush list3 "one"

>lpush list3 "one"

>lpush list3 "one"

>lpush list3 "two"

>lpush list3 "three"

>lrem list3 3 "one"

[如果count>max-length就删除max-length个元素,如果count>max-length,就删除count个元素]。

7、lset list index value [设置链表某个位置的元素的值]

>lpush list4 "one"

>lpush list4 "one"

>lpush list4 "three"

>lset list4 1 "two"

8、lindex list index [返回请求的元素,如果请求的位置超出了链表的长度,则返回nil]

>lpush list5 "one"

>lpush list5 "two"

>lpush list5 "three"

>lpush list5 "four"

>lpush list5 "five"

>lindex list5 3

"two"

9、ltrim list start stop [保留链表中从start到stop之间的元素,删除除此以外的所有元素,如果start>stop,会返回一个空链表,同时该list会被删除,如果stop大于llen,会保留从start开始到链表尾部的所有元素]

>lpush list6 "one"

>lpush list6 "two"

>lpush list6 "three"

>lpush list6 "four"

>lpush list6 "five"

>ltrim list6 0 2

>lrange list6 0 -1

"five" "four" "three"

10、rpush [从链表的尾部插入]

11、rpushx [类似于lpushx]

12、rpop [弹出链表的尾部元素]

13、rpoplpush source destination [从第一source的尾部删除一个元素插入到destination的头部]

>lpush source "two"

>lpush source "one"

>lpush destination "one"

>rpoplpush source destination

>lrange destination 0 -1

"two" "one"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: