This is the 18th day of my participation in the August Genwen Challenge.More challenges in August

【Redis series 】 Redis data structure string and list basic use and familiar with

Redis five data structures

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

It supports many types of data structures, such as strings (hashes), lists (Lists), sets (sets), sorted sets (sorted sets) and range queries, bitmaps, Hyperloglogs and Geospatial index radius query.

Redis is built with replication, LUA scripting, LRU eviction, transactions and different levels of disk persistence, And high availability through Redis Sentinel and Automated Cluster

Basic commands for redis key

  • ping

Check whether the client is successfully connected

  • set key value

Set key and value

  • get key

Gets the value of key

  • keys *

Get all keys

  • move key 1

Remove the key

  • expire key number

Set the expiration time for the key

  • ttl key

Check the remaining validity period of the key

  • type key

Check the key type

  • 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>Copy the code

String string

Setting a single value

  • APPEND key value

Appends a string to a string

  • STRLEN key

Calculate the length of the value corresponding to the key

  • incr key

+1 for key

  • decr key

Value -1 for key

  • INCRBY key number

The key + number

  • DECRBY key number

The key – number

  • GETRANGE key start end

Get the start to end string for key corresponding to value

GETRANGE key 0-1 and get key are the same effect

  • setrange key offset value

Replace the string following the left offset of value corresponding to key

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 word "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"Copy the code
  • Setex Key Second Value (set with expire)

Set the expiration time of the key.

  • ttl key

Check the validity period 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"
Copy the code

Set multiple values in batches

  • mset key value [key value …]

Set multiple key values

  • mget key [key …]

Gets the values of multiple keys

  • MSETNX key value [key value …]

Set multiple values, or if key does not exist, it is an atomic operation that either succeeds or fails at all

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) "k4" 4) "k1" 127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 k4 V4 OK 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) 0Copy the code

Set the object

To 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"Copy the code
  • getset

Get values, then set values

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"
Copy the code

String Indicates the usage scenario of the string

There are many different scenarios for using string. Here are a few:

  • counter
  • Count the number of units
  • Object cache storage
  • Scores, followers, likes, etc

List

List is the basic data type, that is, a List

In the List of Redis, we can simulate stacks, queues, blocking queues and so on

  • LPUSH key element [element …]

Insert data from the left into the key, which has a list. List directives start with l

  • RPUSH key element [element …]

    Insert data into the key from the right

  • LRANGE key start stop

Look at the range of the list,

LRANGE key 0 1 Displays 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"Copy the code
  • LPOP key [count]

Removes data from the left side of the list. The default value is 1, which removes the first element of the list

  • RPOP key [count]

Removing data from the right side of the list removes 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> LPOP 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"Copy the code
  • LINDEX key index

Look at the index 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"Copy the code
  • 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
Copy the code
  • LREM key count element

Delete 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" 1) "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) 0Copy the code

LREM deletes data that does not exist in the list and returns 0, which means 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

Crop, prune, get a paragraph of the list, and crop 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"Copy the code
  • RPOPLPUSH source destination

Fetch 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"Copy the code
  • LSET key index element

Replace the index data in the list with element. If there is no data in the guide, 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 rangeCopy the code
  • LINSERT key BEFORE|AFTER pivot element

List adds data before or after the specified element

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"Copy the code
  • List is actually a linked list, before node, after node, left, and right can all insert data
  • If the key does not exist, a new key is created, that is, a new linked list is created
  • If the key exists, add data normally
  • If all values are removed, the key is gone
  • Inserting and deleting data is most efficient on both sides of a list, while manipulating data in the middle is less efficient

List Is used in the following scenarios:

A list can be a message queue (FIFO) or a stack (FILO)

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

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

I am Nezha, welcome to like, see you next time ~