Redis04_ five basic data types


String type

append appends a value to the specified key. If the current key does not exist, it is equivalent to set key
strlen to obtain the length of the string

127.0.0.1:6379> append name hello
(integer) 8
127.0.0.1:6379> get name
"BLUhello"
127.0.0.1:6379> strlen name
(integer) 8

incr adds 1 to the numeric value stored in the key i++
decr decreases the numeric value stored in the key by 1 i--
incrby increases the numeric value stored in the key by the specified value i+=10
decrby decreases the numeric value stored in the key by the specified value i -=7

127.0.0.1:6379> set views 0
OK
127.0.0.1:6379> get views
"0"
127.0.0.1:6379> incr views
(integer) 1
127.0.0.1:6379> get views
"1"
127.0.0.1:6379> decr views
(integer) 0
127.0.0.1:6379> get views
"0"
127.0.0.1:6379> incrby views 10
(integer) 10
127.0.0.1:6379> get views
"10"
127.0.0.1:6379> decrby views 7
(integer) 3
127.0.0.1:6379> get views
"3"

getrange intercepts the string (closed interval), 0 to -1 means get all the string
setrange replace the string

127.0.0.1:6379> set key1 hello,BLU!
OK
127.0.0.1:6379> getrange key1 0 3
"hell"
127.0.0.1:6379> getrange key1 0 -1
"hello,BLU!"
127.0.0.1:6379> setrange key1 2 xx
(integer) 10
127.0.0.1:6379> get key1
"hexxo,BLU!"

setex sets the key and sets the expiration time: set with expire
setnx creates the key (if it does not exist): set if not exist

127.0.0.1:6379> setex key2 30 Hello!!
OK
127.0.0.1:6379> ttl key2
(integer) 22

127.0.0.1:6379> setnx key3 BLUBLU
(integer) 1
127.0.0.1:6379> setnx key3 BLUBLUBLU
(integer) 0
127.0.0.1:6379> get key3
"BLUBLU"

Create keys in batches with mset
mget get keys in batches
msetnx create keys in batches (if it does not exist) (an atomic operation, as long as one exists, the creation will fail)

127.0.0.1:6379> mset key1 v1 key2 v2 key3 v3
OK
127.0.0.1:6379> keys *
1) "key3"
2) "key2"
3) "key1"
127.0.0.1:6379> mget key1 key2 key3
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> msetnx key1 v1 key2 v2 key3 v3 key4 v4
(integer) 0
127.0.0.1:6379> mget key1 key2 key3 key4
1) "v1"
2) "v2"
3) "v3"
4) (nil)

Two ways to save objects

set user:1 {
    
    name:zhangsan,age:3}
OK
127.0.0.1:6379> get user:1
"{name:zhangsan,age:3}"


127.0.0.1:6379> mset user:1:name zhangsan user:1:age 18
OK
127.0.0.1:6379> mget user:1:name user:1:age
1) "zhangsan"
2) "18"

getset creates/modifies the value while obtaining the key

127.0.0.1:6379> getset db redis
(nil)
127.0.0.1:6379> get db
"redis"
127.0.0.1:6379> getset db mysql
"redis"
127.0.0.1:6379> get db
"mysql"

List type

It is essentially a linked list. It is more efficient to insert or change values ​​on both sides, and the efficiency to update the middle element is low

Left and right removal deposit equivalent queue
left deposit left removal corresponds stack

lpush inserts the value into the list head
rpush inserts the value into the list tail
lrange gets the value of the specific interval in the list 0 to -1 means get all

127.0.0.1:6379> lpush list1 one
(integer) 1
127.0.0.1:6379> lpush list1 two
(integer) 2
127.0.0.1:6379> lpush list1 three
(integer) 3
127.0.0.1:6379> rpush list1 right
(integer) 4
127.0.0.1:6379> lrange list1 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
127.0.0.1:6379> lrange list1 0 1
1) "three"
2) "two"

lpop remove the head of the list value
rpush remove the tail of the value list

lindex obtains the value in the list by subscript (starting from 0)
llen obtains the length of the list

127.0.0.1:6379> lpop list1
"three"
127.0.0.1:6379> lrange list1 0 -1
1) "two"
2) "one"
3) "right"
127.0.0.1:6379> rpop list1
"right"
127.0.0.1:6379> lrange list1 0 -1
1) "two"
2) "one"
127.0.0.1:6379> lindex list1 0
"two"
127.0.0.1:6379> llen list1
(integer) 2

There can be duplicate values ​​in the list.
lrem removes the specified number of specified values.
ltrim truncates the list by subscript.
rpoplpush removes the value at the end of the specified list and saves it to the head of the new list.

127.0.0.1:6379> lpush list1 three
(integer) 3
127.0.0.1:6379> lpush list1 three
(integer) 4
127.0.0.1:6379> lpush list1 three
(integer) 5
127.0.0.1:6379> lrange list1 0 -1
1) "three"
2) "three"
3) "three"
4) "two"
5) "one"
127.0.0.1:6379> lrem list1 2 three
(integer) 2
127.0.0.1:6379> lrange list1 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> ltrim list1 0 1
OK
127.0.0.1:6379> lrange list1 0 -1
1) "three"
2) "two"
127.0.0.1:6379> rpoplpush list1 list2
"two"
127.0.0.1:6379> lrange list1 0 -1
1) "three"
127.0.0.1:6379> lrange list2 0 -1
1) "two"

exists to determine whether the specified list exists.
lset modifies the value of the specified subscript in the list (if the list does not exist or the specified subscript does not exist, an error will be reported)

127.0.0.1:6379> exists list1
(integer) 0
127.0.0.1:6379> lset list1 0 item
(error) ERR no such key
127.0.0.1:6379> lpush list1 v1
(integer) 1
127.0.0.1:6379> lrange list1 0 -1
1) "v1"
127.0.0.1:6379> lset list1 0 item1
OK
127.0.0.1:6379> lset list1 1 item2
(error) ERR index out of range

linsert inserts the value before and after the specified value in the list (before after)

127.0.0.1:6379> rpush mylist hello
(integer) 1
127.0.0.1:6379> rpush mylist world
(integer) 2
127.0.0.1:6379> lrange mylist 0 -1
1) "hello"
2) "world"
127.0.0.1:6379> linsert mylist before world beautiful
(integer) 3
127.0.0.1:6379> lrange mylist 0 -1
1) "hello"
2) "beautiful"
3) "world"
127.0.0.1:6379> linsert mylist after world everyday
(integer) 4
127.0.0.1:6379> lrange mylist 0 -1
1) "hello"
2) "beautiful"
3) "world"
4) "everyday"

Set type

The elements in the set are unordered and cannot be repeated
sadd to add values ​​to the set
smembers to get the value in the set
sismember to determine whether the specified value exists in the set, return 1 if it exists, return 0
if it does not exist scard get the number of elements in the set
srem remove the set element specified
srandmember taken randomly from the set number of designated elements
spop randomly removes an element from the set
smove the elements set out in designated set to another

127.0.0.1:6379> sadd myset hello
(integer) 1
127.0.0.1:6379> sadd myset BLU
(integer) 1
127.0.0.1:6379> sadd myset happy
(integer) 1
127.0.0.1:6379> sadd myset everyday
(integer) 1
127.0.0.1:6379> smembers myset
1) "happy"
2) "BLU"
3) "everyday"
4) "hello"
127.0.0.1:6379> sismember myset BLU
(integer) 1
127.0.0.1:6379> scard myset
(integer) 4
127.0.0.1:6379> srem myset hello
(integer) 1
127.0.0.1:6379> srandmember myset
"happy"
127.0.0.1:6379> srandmember myset 2
1) "happy"
2) "everyday"
127.0.0.1:6379> spop myset
"everyday"
127.0.0.1:6379> smembers myset
1) "happy"
2) "BLU"
127.0.0.1:6379> smove myset myset2 BLU
(integer) 1
127.0.0.1:6379> smembers myset
1) "happy"
127.0.0.1:6379> smembers myset2
1) "BLU"

sdiff compares a set with b set, and returns the unique elements of a set (difference set)
sinter compares a set with b set, and returns the elements common to the two sets (intersection)
Example: query two users' common concerns and common friends

Sunion compares a set with b set, and returns all the elements of the two sets (union)

sadd set1 a
sadd set1 b
sadd set1 c
sadd set1 d
sadd set2 c
sadd set2 d
sadd set2 e
127.0.0.1:6379> sdiff set1 set2
1) "a"
2) "b"
127.0.0.1:6379> sinter set1 set2
1) "c"
2) "d"
127.0.0.1:6379> sunion set1 set2
1) "e"
2) "c"
3) "d"
4) "b"
5) "a"

Hash (hash) type

hset storage value
hget value
hmset batch storage value
hmget batch value
hgetall remove all keys and values ​​in the hash
hdel delete the specified field
hlen to obtain the length of the hash (number of fields)
hexists judge whether the specified field exists
hkeys obtain all keys
hvals obtain All values

127.0.0.1:6379> hset myhash field1 BLU
(integer) 1
127.0.0.1:6379> hget myhash field1
"BLU"
127.0.0.1:6379> hmset myhash field2 hello field3 world
OK
127.0.0.1:6379> hmget myhash field1 field2 field3
1) "BLU"
2) "hello"
3) "world"
127.0.0.1:6379> hgetall myhash
1) "field1"
2) "BLU"
3) "field2"
4) "hello"
5) "field3"
6) "world"
127.0.0.1:6379> hdel myhash field3
(integer) 1
127.0.0.1:6379> hlen myhash
(integer) 2
127.0.0.1:6379> hexists myhash field1
(integer) 1
127.0.0.1:6379> hkeys myhash
1) "field1"
2) "field2"
127.0.0.1:6379> hvals myhash
1) "BLU"
2) "hello"

hincrby increases the numeric value of the specified field by the specified size
hsetnx stored value (if the key does not exist), if the key already exists, the stored value fails

127.0.0.1:6379> hset myhash field3 7
(integer) 1
127.0.0.1:6379> hincrby myhash field3 3
(integer) 10
127.0.0.1:6379> hsetnx myhash field4 hahaha
(integer) 1
127.0.0.1:6379> hsetnx myhash field4 lollollol
(integer) 0

Hash is more suitable for storing objects than String

127.0.0.1:6379> hset user:1 name zhangsan
(integer) 1
127.0.0.1:6379> hset user:1 age 26
(integer) 1
127.0.0.1:6379> hget user:1 name
"zhangsan"
127.0.0.1:6379> hget user:1 age
"26"

Zset ordered set type

Application: Leaderboard

zadd adds a value to Zset
zrange to get the value of a specific interval in Zset (by default, it is sorted from low to high by score)

127.0.0.1:6379> zadd myzset 1 one
(integer) 1
127.0.0.1:6379> zadd myzset 2 two
(integer) 1
127.0.0.1:6379> zadd myzset 3 three 4 four
(integer) 2
127.0.0.1:6379> zrange myzset 0 -1
1) "one"
2) "two"
3) "three"
4) "four"

zrangebyscore sorts according to score, -inf means negative infinity +inf means positive infinity, withscores means display score value,
zrevrange sorts in reverse order

127.0.0.1:6379> zadd salary 2500 zhangsan 5000 lisi 1200 BLU
(integer) 3
127.0.0.1:6379> zrangebyscore salary -inf +inf
1) "BLU"
2) "zhangsan"
3) "lisi"
127.0.0.1:6379> zrangebyscore salary -inf +inf withscores
1) "BLU"
2) "1200"
3) "zhangsan"
4) "2500"
5) "lisi"
6) "5000"
127.0.0.1:6379> zrangebyscore salary -inf 2500 withscores
1) "BLU"
2) "1200"
3) "zhangsan"
4) "2500"
127.0.0.1:6379> zrevrange salary 0 -1 withscores
1) "lisi"
2) "5000"
3) "zhangsan"
4) "2500"
5) "BLU"
6) "1200"

zrem removes the specified value in
Zset zcard counts the number of values ​​in
Zset zcount counts the number of values ​​in the specified interval

127.0.0.1:6379> zrem salary lisi
(integer) 1
127.0.0.1:6379> zcard salary
(integer) 2

127.0.0.1:6379> zadd myset 1 hello 2 world 3 BLU
(integer) 3
127.0.0.1:6379> zcount myset 1 3
(integer) 3
127.0.0.1:6379> zcount myset 2 3
(integer) 2

Guess you like

Origin blog.csdn.net/BLU_111/article/details/108277124