Five basic types
string
string |
Binary safe, can contain any data, a key can store up to 512MB |
Examples :
redis 127.0.0.1:6379> SET name "runoob" #设置key
OK
redis 127.0.0.1:6379> GET name #Get key
"runoob"
append name ddd |
Add a string after key = name |
strlen name |
Calculate the length of key = name |
incr views |
key=name加1 |
decr views |
key=name减1 |
incrby views n |
key=name加n |
decrby views n |
key=name减n |
getrange name 0 7 |
Get the range of key = name 0 -1 means all |
setrange name 7 jjj |
Character replacement, starting from index = 7 |
setex key3 5 llll |
Set key3's expiration time |
setnx name 111 |
No name creation, otherwise fail |
mset k1 v1 k2 v2 k3 v3 |
Batch setting, if there is an error, all will fail |
mget k1 k2 k3 |
Bulk acquisition |
getset name 4444 |
Get the old value and set the new value |
hash
hash |
Is a string type field and value mapping table, hash is particularly suitable for storing objects |
Each hash can store (2 ^ 32) -1 key-value pairs (over 4 billion)
Examples :
127.0.0.1:6330[2]> hset myhash f1 aaa #设置键值
127.0.0.1:6330[2]> hget myhash f1 #获取键值
"aaa"
127.0.0.1:6330[2]> hmset myhash f1 aa f2 bb f3 cc #批量设置
OK
127.0.0.1:6330[2]> hmget myhash f1 f2 f3 #批量获取
1) "aa"
2) "bb"
3) "cc"
127.0.0.1:6330[2]> hgetall myhash #获取myhash中信息
1) "f1"
2) "aa"
3) "f2"
4) "bb"
5) "f3"
6) "cc"
127.0.0.1:6330[2]> hlen myhash #获取myhash中元素个数
(integer) 3
127.0.0.1:6330[2]> hincrby myhash f5 4 #将f5字段值加4,返回结果,可以为负数
(integer) 7
127.0.0.1:6330[2]> hsetnx myhash f4 ii #如果存在不设置
(integer) 1
application:
1. Save user information
2. Frequently changing information
list
list |
It is a simple list of strings, sorted in order of insertion. You can add an element to the head (left) or tail (right) of the list |
More than 4 billion in capacity as hash
Examples:
redis 127.0.0.1:6379> lpush runoob redis #insert element
(integer) 1
redis 127.0.0.1:6379> lpush runoob mongodb
(integer) 2
redis 127.0.0.1:6379> lpush runoob rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange runoob 0 10 #View the elements of a range in the key
1) "rabitmq"
2) "mongodb"
3) "redis"
rpush list four |
Put on the other side, contrary to lpush, that is, insert an element before lpush inserts the element for the first time |
lpop list |
Remove one from the insertion end of lpush, that is, delete the last inserted element of lpush |
rpop list |
Remove one from the other end, that is, delete the first inserted element of lpush |
LINDEX 1 |
Get the element with index = 1, starting from the last insertion of lpush as 0. Does not delete elements |
FULL list |
Return list length |
LREM list 1 one |
Remove 1 from the list |
ltrim list 1 2 |
Intercept the elements in the list ltrim key start stop, the index here is the same as lindex |
rpoplpush list mylist |
Remove elements from list to new list |
exists list |
Determine if the list exists |
lset list 2 sss |
Starting from the last element inserted by lpush, set the value of index = 2 |
linsert key BEFORE|AFTER pivot value |
Insert a value before / after a value. before is the index of the inserted element lindex is small, after is the index of lindex is large |
Application range:
Message queue, queue, stack
set (the only unordered set of elements)
set |
It is an unordered collection of type string. The element is unique, if it exists, the addition fails |
The collection is realized by a hash table, the complexity is constant
Insert the element into the set collection, successfully return 1, if the element already returns 0 in the collection, the set corresponding to the key does not exist returns an error
Examples:
127.0.0.1:6379> sadd key a 2 3 4 5 6 #添加元素到key中
(integer) 6
127.0.0.1:6379> SMEMBERS key #查看key中的元素
1) "a"
2) "3"
3) "2"
4) "6"
5) "5"
6) "4"
127.0.0.1:6379> SISMEMBER key 4 #判断key中是否有4
(integer) 1
127.0.0.1:6379> SCARD key #计算key中元素个数
(integer) 6
127.0.0.1:6379> srem key a #删除key中元素a
(integer) 1
127.0.0.1:6379> SMEMBERS key
1) "6"
2) "5"
3) "2"
4) "3"
5) "4"
127.0.0.1:6379> SRANDMEMBER key 2 ##随机返回n个key中的元素,不删除
1) "5"
2) "3"
127.0.0.1:6379> SMEMBERS key
1) "6"
2) "5"
3) "2"
4) "3"
5) "4"
127.0.0.1:6379> spop key #随机删除一个元素
"4"
127.0.0.1:6379> smove key key2 3 #将key中的元素3移动到key2
(integer) 1
127.0.0.1:6379> SMEMBERS key
1) "6"
2) "5"
3) "2"
127.0.0.1:6379> SMEMBERS key2
1) "3"
Set operation
127.0.0.1:6330[2]> smembers set1
1) "1"
2) "2"
3) "3"
4) "4"
127.0.0.1:6330[2]> smembers set2
1) "3"
2) "4"
3) "5"
4) "6"
127.0.0.1:6330[2]> sdiff set1 set2 #差集
1) "1"
2) "2"
127.0.0.1:6330[2]> sinter set1 set2 #交集
1) "3"
2) "4"
127.0.0.1:6330[2]> sunion set1 set2 #并集
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
sunionstore set3 set1 set2 #Calculate the union of set1 and set2 and save in set3
sdiffstore
sinterstore
Application range:
Focus on Weibo and put it in the set
zset (the only ordered set)
zset (ordered set) |
Each element is associated with a double type score for sorting, the members are unique, and the score can be repeated |
Examples:
####添加元素
127.0.0.1:6330[9]> zadd myset 300 ccc
(integer) 1
127.0.0.1:6330[9]> zadd myset 100 fff
(integer) 1
127.0.0.1:6330[9]> zadd myset 700 aaa
(integer) 1
####
127.0.0.1:6330[9]> zrange myset 0 -1 #查看所有元素,从小到大
1) "fff"
2) "ccc"
3) "aaa"
127.0.0.1:6330[9]> zrangebyscore myset -inf inf #查看所有元素,从小到大
1) "fff"
2) "ccc"
3) "aaa"
127.0.0.1:6330[9]> zrevrange myset 0 -1 #查看所有元素,从大到小
1) "aaa"
2) "ccc"
3) "fff"
127.0.0.1:6330[9]> zrangebyscore myset -inf inf withscores ##查看所有元素,从小到大,并打印分数
1) "fff"
2) "100"
3) "ccc"
4) "300"
5) "aaa"
6) "700"
127.0.0.1:6330[9]> zrangebyscore myset -inf 600 withscores #查看某一区间元素,从小到大
1) "fff"
2) "100"
3) "ccc"
4) "300"
127.0.0.1:6330[9]> zrem myset aaa #删除元素
(integer) 1
127.0.0.1:6330[9]> zrange myset 0 -1
1) "fff"
2) "ccc"
127.0.0.1:6330[9]> zadd myset 700 aaa
(integer) 1
127.0.0.1:6330[9]> zcount myset 200 600 #计算某个区间元素个数
(integer) 1
127.0.0.1:6330[9]> zcard myset #返回所有元素个数
(integer) 3
application:
1. Class results
2. Wages
3. Message priority
4. Leaderboard realization
The above command can also go to the official website to see examples :