Five data structures of redis
Redis is an open source (BSD license), the data structure stored in the memory system, it may be used database , cache and messaging middleware .
It supports multiple types of data structures such as strings, hashes, lists, sets, sorted sets and range queries, bitmaps, hyperloglogs and geospatial (geospatial) Index radius query .
Redis has built-in replication (replication), LUA scripting (Lua scripting), LRU driven events (LRU eviction), transactions (transactions) and different levels of disk persistence (persistence), and through Redis sentinel (Sentinel) and automatic partition (Cluster) ) to provide high availability (high availability)
Basic commands for redis key
- ping
Check if the client is connected successfully
- set key value
set key and value
- get key
get the value of key
- keys *
get all keys
- move key 1
delete key
- expire key number
Set expiration time on key
- ttl key
Check the remaining valid time of the key
- type key
Check the type of key
- EXISTS key
Check if the key exists
root@iZuf66y3tuzn4wp3h02t7pZ:/# redis-cli -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> set name xiaomotong
OK
127.0.0.1:6379> get name
"xiaomotong"
127.0.0.1:6379> type name
string
127.0.0.1:6379> EXISTS name
(integer) 1
127.0.0.1:6379> EXISTS xiaozhu
(integer) 0
127.0.0.1:6379> set age 18
OK
127.0.0.1:6379> keys *
1) "name"
2) "age"
127.0.0.1:6379> set hobby sports
OK
127.0.0.1:6379> EXPIRE hobby 20
(integer) 1
127.0.0.1:6379> ttl hobby
(integer) 14
127.0.0.1:6379> ttl hobby
(integer) 13
127.0.0.1:6379> get hobby
(nil)
127.0.0.1:6379> move name 1
(integer) 1
127.0.0.1:6379> keys *
1) "age"
127.0.0.1:6379>
string string
sets a single value
- APPEND key value
append string after string
- STRLEN key
Calculate the length of the key corresponding to the value
- incr key
+1 to the value of key
- decr key
value -1 for key
- INCRBY key number
pair key + number
- DECRBY key number
pair key-number
- GETRANGE key start end
Get the character range, get the string from start to end of the key corresponding to the value
GETRANGE key 0 -1
and get key
are an effect
- setrange key offset value
Replace the string corresponding to the value of key with offset offsets from the left to the back
127.0.0.1:6379> set name xiaozhu
OK
127.0.0.1:6379> get name
"xiaozhu"
127.0.0.1:6379> APPEND name xiaopang
(integer) 15
127.0.0.1:6379> get name
"xiaozhuxiaopang"
127.0.0.1:6379> STRLEN name
(integer) 15
127.0.0.1:6379> set views 0
OK
127.0.0.1:6379> incr views
(integer) 1
127.0.0.1:6379> incr views
(integer) 2
127.0.0.1:6379> incr views
(integer) 3
127.0.0.1:6379> decr views
(integer) 2
127.0.0.1:6379> get views
"2"
127.0.0.1:6379> INCRBY views 20
(integer) 22
127.0.0.1:6379> get views
"22"
127.0.0.1:6379> DECRBY views 10
(integer) 12
127.0.0.1:6379> get views
"12"
127.0.0.1:6379> set words "hello wrold"
OK
127.0.0.1:6379> GETRANGE words 2 5
"llo "
127.0.0.1:6379> set key1 xiaozhupeiqi
OK
127.0.0.1:6379> GETRANGE key1 0 -1
"xiaozhupeiqi"
127.0.0.1:6379>
127.0.0.1:6379> setrange key1 4 pangziyo
(integer) 12
127.0.0.1:6379> get key1
"xiaopangziyo"
- setex key second value (set with expire)
Set the expiration time corresponding to the key,
- ttl key
Check the validity time of the key
- setnx key value (if not exist)
If the key does not exist, set it, This instruction is usually used for distributed locks
127.0.0.1:6379> setex call 30 xiaomotong
OK
127.0.0.1:6379> ttl call
(integer) 24
127.0.0.1:6379> ttl call
(integer) 21
127.0.0.1:6379> setnx call xiaozhu
(integer) 1
127.0.0.1:6379> get call
"xiaozhu"
127.0.0.1:6379> setnx call xiaoming
(integer) 0
127.0.0.1:6379> get call
"xiaozhu"
Batch set multiple values
- mset key value [key value ...]
Set multiple key values
- mget key [key ...]
Get the values corresponding to multiple keys
- MSETNX key value [key value ...]
Set multiple values, if the key does not exist, set the value, This is an atomic operation, either all succeed or all fail
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 k4 v4
OK
127.0.0.1:6379> keys *
1) "k2"
2) "k4"
3) "k3"
4) "k1"
127.0.0.1:6379> mget k1 k2 k3 k4
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379> msetnx k4 44
(integer) 0
127.0.0.1:6379> msetnx k4 44 k5 55
(integer) 0
set object
Set the object, we can design the key of redis as the character key required by our business, such as the following example
127.0.0.1:6379> set student:1:name xiaozhuy
OK
127.0.0.1:6379> set student:2:name xiaopangzi
OK
127.0.0.1:6379> mset student:3:name xiaopeiqi student:3:age 18 student:3:hobby basketball
OK
127.0.0.1:6379> keys student:3*
1) "student:3:hobby"
2) "student:3:age"
3) "student:3:name"
127.0.0.1:6379> mget student:3:hobby student:3:age student:3:name
1) "basketball"
2) "18"
3) "xiaopeiqi"
- getset
First get the value, then set the value
127.0.0.1:6379> getset location beijing
(nil)
127.0.0.1:6379> get location
"beijing"
127.0.0.1:6379> getset location changsha
"beijing"
127.0.0.1:6379> get location
"changsha"
string usage scenarios
There are many usage scenarios of the string type, some of which are listed below:
- counter
- Count the number of multiple units
- Object cache storage
- Scores, followers, likes, etc.
List
List is a basic data type i.e. list
In the List of redis, we can simulate stacks, queues, blocking queues, etc.
- LPUSH key element [element ...]
Insert data into the key from the left, this key has a list, and the list type instructions all start with l
RPUSH key element [element ...]
Insert data into key from the right
- LRANGE key start stop
Check the scope of the list,
LRANGE key 0 1
View all the values of the current list
127.0.0.1:6379> LPUSH mylist k1
(integer) 1
127.0.0.1:6379> LPUSH mylist k2
(integer) 2
127.0.0.1:6379> LPUSH mylist k3
(integer) 3
127.0.0.1:6379> LRANGE 0 -1
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379> LRANGE mylist 0 -1
1) "k3"
2) "k2"
3) "k1"
127.0.0.1:6379> LRANGE mylist 0 1
1) "k3"
2) "k2"
127.0.0.1:6379> RPUSH mylist 4
(integer) 4
127.0.0.1:6379> RPUSH mylist 5
(integer) 5
127.0.0.1:6379> lrange mylist 0 -1
1) "k3"
2) "k2"
3) "k1"
4) "4"
5) "5"
- LPOP key [count]
Remove data from the left side of the list, the default is 1, which is to remove the first element of the list
- RPOP key [count]
Remove data from the right side of the list, that is, remove the last element of the list
127.0.0.1:6379> LPOP mylist
"k3"
127.0.0.1:6379> LPOP mylist 2
1) "k2"
2) "k1"
127.0.0.1:6379> lrange mylist 0 -1
1) "4"
2) "5"
127.0.0.1:6379> RPOP mylist
"5"
127.0.0.1:6379> lrange mylist 0 -1
1) "4"
- LINDEX key index
Look at the index-th value in the list, starting at 0
127.0.0.1:6379> keys *
1) "mylist"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "4"
127.0.0.1:6379> LINDEX mylist 1
(nil)
127.0.0.1:6379> LINDEX mylist 0
"4"
- LLEN key
Check the length of the list
127.0.0.1:6379> LLEN mylist
(integer) 1
127.0.0.1:6379> LPUSH mylist k6
(integer) 2
127.0.0.1:6379> LPUSH mylist k7
(integer) 3
127.0.0.1:6379> LLEN mylist
(integer) 3
- LREM key count element
Delete the specified element in the list, you can specify how many to delete
127.0.0.1:6379> lpush mylist one two three four five
(integer) 5
127.0.0.1:6379> lrange mylist 0 -1
1) "five"
2) "four"
3) "three"
4) "two"
5) "one"
127.0.0.1:6379> LREM mylist 1 five
(integer) 1
127.0.0.1:6379> LRANGE mylist 0 -1
1) "four"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379> lpush mylist one
(integer) 5
127.0.0.1:6379> LRANGE mylist 0 -1
1) "one"
2) "four"
3) "three"
4) "two"
5) "one"
127.0.0.1:6379> LREM mylist 2 one
(integer) 2
127.0.0.1:6379> LRANGE mylist 0 -1
1) "four"
2) "three"
3) "two"
127.0.0.1:6379> LREM mylist 4 ll
(integer) 0
LREM deletes data that does not exist in the list, returns 0, 0 is failure,
LREM deletes the data that exists in the list. If it is expected to delete 5, but there are only 2, then redis will return 2, and the deletion is successful
- LTRIM key start stop
Cut, prune, get a section of the list, and cut it out
127.0.0.1:6379> LRANGE mylist 0 -1
1) "k5"
2) "k4"
3) "k3"
4) "k2"
5) "k1"
6) "four"
7) "three"
8) "two"
127.0.0.1:6379> LTRIM mylist 3 5
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "k2"
2) "k1"
3) "four"
- RPOPLPUSH source destination
Take data from the right side of the source list and add data from the left side of the destination list
127.0.0.1:6379> LRANGE mylist 0 -1
1) "k2"
2) "k1"
3) "four"
127.0.0.1:6379> RPOPLPUSH mylist newlist
"four"
127.0.0.1:6379>
127.0.0.1:6379> lrange newlist 0 -1
1) "four"
- LSET key index element
Replace the data of the corresponding index in the list with element. If there is no data in the index, an error will be reported
127.0.0.1:6379> lrange mylist 0 -1
1) "k2"
2) "k1"
127.0.0.1:6379> LSET mylist 1 hello
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "k2"
2) "hello"
127.0.0.1:6379> LSET mylist 10 world
(error) ERR index out of range
LINSERT key BEFORE|AFTER pivot element
Add data before or after the specified element in the list
127.0.0.1:6379> lrange mylist 0 -1
1) "k2"
2) "hello"
127.0.0.1:6379> LINSERT mylist before hello xiaozhu
(integer) 3
127.0.0.1:6379> lrange mylist 0 -1
1) "k2"
2) "xiaozhu"
3) "hello"
127.0.0.1:6379> LINSERT mylist after hello bottom
(integer) 4
127.0.0.1:6379> lrange mylist 0 -1
1) "k2"
2) "xiaozhu"
3) "hello"
4) "bottom"
- list is actually a linked list, before node, after node, left, right can insert data
- If the key does not exist, a new key will be created, that is, a new linked list will be created
- If the key exists, add data normally
- If all values are removed, the key does not exist
- Inserting and deleting data on both sides of the list is the most efficient, and operating data from the middle is relatively inefficient
Application scenarios of list list:
list can be a message queue (FIFO) or a stack (FILO)
Welcome to like, follow, favorite
Friends, your support and encouragement are the motivation for me to persist in sharing and improve quality
Okay, here it is this time
Technology is open, and our mentality should be open. Embrace change, live in the sun, and move forward.
I'm little devil boy Nezha , welcome to like, follow and favorite, see you next time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。