[redis series] redis learning 3. Basic use and familiarity of string and list of redis data structures

Five data structures of redis

Redis is an open source (BSD licensed) in memory data structure storage system, which can be used as database, cache and message middleware.

It supports many types of data structures, such as strings, hashes, lists, sets, sorted sets and range queries, bitmaps, hyperlogs and geospatial index radius queries.

Redis has built-in replication, Lua scripting, LRU events, transactions and different levels of disk persistence, and provides high availability through redis Sentinel and Cluster

Basic commands of redis key

  • ping

Check whether the client is connected successfully

  • set key value

Set key and value

  • get key

Get the value of key

  • keys *

Get all key s

  • move key 1

Delete key

  • expire key number

Set expiration time for key

  • ttl key

View the remaining valid time of the key

  • type key

View the type of key

  • EXISTS key

Check whether 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

Set a single value

  • APPEND key value

Append string after string

  • STRLEN key

Calculate the length of value corresponding to key

  • incr key

Value for key + 1

  • decr key

Value for key - 1

  • INCRBY key number

Pair key +number

  • DECRBY key number

For key -number

  • GETRANGE key start end

Get the character range, and get the string from start to end of value corresponding to key

GETRANGE key 0 -1 and get key are one effect

  • setrange key offset value

Replace the string after offset of value corresponding to key from left to right

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

View valid time of 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 setting multiple values

  • mset key value [key value ...]

Set multiple key value s

  • mget key [key ...]

Get values corresponding to multiple key s

  • 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

By setting the object, we can design the redis key 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"

Usage scenario of string

There are many usage scenarios for string types, as listed below:

  • Counter
  • Count the quantity of multiple units
  • Object cache storage
  • Score, number of fans, number of likes, etc

List

List is the basic data type, that is, list

In the redis List, we can simulate stacks, queues, blocking queues, etc

  • LPUSH key element [element ...]

Insert data into the key from the left. The key has a list, and the list type instructions start with l

  • RPUSH key element [element ...]

    Insert data into the key from the right

  • LRANGE key start stop

View the scope of the list,

LRANGE key 0 1 view all 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 value is 1, which is the first element of the list

  • RPOP key [count]

To remove data from the right side of the list is to 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

View the index value in the list, starting from 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

View 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 elements specified in the list. You can specify how many elements 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 and returns 0. 0 is failure,

LREM deletes the data in the list. If 5 data are expected to be deleted, but only 2 data are actually deleted, redis will return 2 and the deletion is successful

  • LTRIM key start stop

Cut, prune, get a section of the list, and cut it down

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 book, 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 and 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 scenario of list:

list can be used as message queue (FIFO) or stack (FILO)

Welcome to like, follow and collect

My friends, your support and encouragement are the driving force for me to insist on sharing and improve quality

Well, that's all for this time

Technology is open, and our mentality should be open. Embrace change, live in the sun and strive to move forward.

I'm Nezha, the Little Devil boy. Welcome to praise and pay attention to the collection. See you next time~

Keywords: Database

Added by benson on Sun, 16 Jan 2022 20:20:33 +0200