Redis commonly used five data types (detailed) are being updated

One, key

1, View all keys in the current library:

key *

Type in a few values ​​first before testing

 set k1 mary
 set k2 jack
 set k3 frank
 

  keys *      

70260e3cea044357a186ab22823e1cce.png

 2, determine whether a key is included

exists key

d42a3deaa1804f11a2776ef6fa1709bc.png

3, check the type of the key

type key

 24cf98bf9b7847c1b32e6bdf3ed558a3.png

 4, delete the data of the specified key

del key      删除指定key的数据
unlink key   根据value选择非阻塞删除 

65768107a6e84f4999c4796b82e24aae.png

539738b77aa0434c8219f19ad6c574ec.png

The effect is the same, but the del key is directly deleted and the unlink key is a non-blocking delete, which will not be deleted directly. Only the keys are deleted from the keyspace metadata, and the real deletion will be performed asynchronously later.

5. Set the expiration time for the key

expire key 10 指定key10秒删除

 46668c7cbbee4678a798c967ef763464.png

 6. Check how many seconds the key will expire

-1 means never expires, -2 means expired

35a7691d0da042dfa50d778a2732b8a5.png

7, switch database

Redis has 16 databases by default, 0-15 databases, and the default selection is 0 database

 c3722bd58637425ebe5351452adcced2.png

8, Check how many keys the current library has

Because our previous operation deleted the key, now add a few more

8c998d7f891f44b5b298a29e5f9eca03.png

View the number of keys in the current library 

 ffa5042669914eeaae850c30aebf1b51.png

 9. Clear the current library

flushdb

9d6cf5a84fa742be8000ead5ec1bef33.png

10, kill all libraries

flushall

e58286957d784b5888792fb4c91b7c58.png

Two, Redis string type (String)

1 Introduction

String is the most basic type of Redis, you can understand it as exactly the same type as Memcached, a key corresponds to a value.

The String type is binary safe . Means that the Redis string can contain any data. Such as jpg images or serialized objects.

The String type is the most basic data type of Redis, and a string value in Redis can be up to 512M.

2. Common commands

1. Add key-value pairs

set <key> <value>

 2, get the value

get key

3. Append characters 

append <key> <value>

 4, get the length of the value

strlen key

5, only set the value when the key does not exist

setnx key

6, +1, -1 the digital value stored in the key

incr <key> +1
decr <key> -1

Only valid for numeric values, incr, if it is empty, the new value is 1; decr, if it is empty, the new value is -1

 7, the digital value stored in the key + - specify the step size

incrby key 步长
decrby key 步长

atomicity 

 The so-called atomic operation refers to the operation that will not be interrupted by the thread scheduling mechanism; 

Once this operation starts, it will run until the end without any context switch (switching to another thread) in the middle.

 (1) In a single thread, operations that can be completed in a single instruction can be considered "atomic operations" because interrupts can only occur between instructions.

(2) In multithreading, operations that cannot be interrupted by other processes (threads) are called atomic operations.

The atomicity of Redis single command is mainly derived from the single thread of Redis.

8, mset, mget, set, take multiple keys and values

mset k1 v1 k2 v2 k3 v3
mget k1 k2 k3

9, msetnx sets the key that does not exist, if the key exists, the setting is unsuccessful 

10, getrange <key> start end intercepts the string of key from start to end

 11, setrange <key> start position value

12, setex key expiration time value, set the expiration time when setting the key value

13, getset kv replace the old value with the new value

3, data structure

The data structure of String is Simple Dynamic String (SDS for short). It is a string that can be modified. Its internal structure is similar to Java's ArrayList. It uses pre-allocated redundant space to reduce frequent allocation of memory.

As shown in the figure, the capacity actually allocated internally for the current string is generally higher than the actual string length len. When the length of the string is less than 1M, the expansion will double the existing space. If it exceeds 1M, the expansion will only expand the space by 1M at a time. It should be noted that the maximum length of the string is 512M.

Three, Redis list (List)

1 Introduction

single key multiple value

The redis list is a simple list of strings, sorted in insertion order, and an element can be added to the head (left) or tail (right) of the list.

Its bottom layer is actually a doubly linked list , which has high performance on both ends of the operation, and the performance of nodes in the middle through index subscript operations will be poor.

 2. Common commands

1, lpush/rpush <key> <v1><v2>.... Insert one or more values ​​from left/right.

Insert left

insert right

 Note: The insertion process is as shown in the figure below.

Insert left:

a
b a
c b a
d c b a

Insert right:

a
a

b

a b c
a b c d

2, lpop/rpop <key> spits out a value from left/right. The value is in the key, and the value is in the key .

3. rpoplpush <k1><k2> spits out a value from the right of the k1 list and inserts it to the left of the k2 list

Let's take left insertion as an example:

 4, lrange key start end Get elements according to the index subscript (from left to right)

Slightly, shown above

Note: lrange key 0 -1 can get all values

5, lindex key index Get elements according to the index subscript (from left to right)

6, llen key to get the length of the list

7, linsert key before value newvalue Insert newvalue after value to insert value

8, lrem key n value delete n values ​​​​from the left (from left to right)

9. lset key index value Change the value of the list key subscripted as index to value

3, data structure

The underlying data structure of List is the quick linked list quickList

First of all, a continuous memory storage will be used when there are fewer elements in the list. This structure is ziplist, which is a compressed list. He stores all the elements next to each other and allocates a continuous piece of memory.

When the amount of data is large, it will be changed to quickList.

Because the additional pointer space required by the ordinary linked list is too large, it will waste space. For example, only int type data is stored in this list, and two additional pointers prev and next are needed in the structure.

Redis combines linked list and zipList to form quickList. That is, multiple zipLists are used in series using two-way pointers. This not only satisfies the fast insertion and deletion performance, but also does not cause too much space redundancy.

Four, Redis collection (Set)

1 Introduction

The function provided by Redis set is similar to that of list. It is a list function. The special feature is that set can be automatically retaken . When you need to store a list of data and do not want duplicate data, set is a good choice.

Updating.......

Guess you like

Origin blog.csdn.net/Yajyaj123/article/details/126918559