redis common commands

Table of contents

what is the general command

SET & GET

keys

EXISTS

OF THE

EXPIRE

TTL

redis expiration policy

timer policy

Priority-Based Queue Timer

Time wheel based timer

TYPE


Interact with the redis server through the redis client.

So you need to use redis commands, but there are many redis commands.

what is the general command

In redis, the commands for the operation of different data stored are different, but there are still some commands that are common, and these commands that are common to all data types are "common commands".

Before introducing common commands, we can actually go to the official website to learn something about redis.

[Redis](https://redis.io/) 

We can have a search on the main page that can be used to search for commands.

Here we can start the client, and then we can use the ping command to verify connectivity.

127.0.0.1:6379> ping
PONG

If the Unicom is successful, it will return pong. In fact, the commands of redis are generally in uppercase and the same as mysql, although lowercase can also be supported.

SET & GET

Redis is stored in the form of key-value pairs.

  • get: Get the corresponding value according to the key

  • set: set the key and value into it

The key and value here are both strings.

set key value
如果设置成功返回 OK

The above is the simple syntax of set, set followed by a key followed by a value.

Although it is said above that both key and value are strings, there is no need to add double quotes or single quotes when setting.

127.0.0.1:6379> SET key1 value1
OK

The value can be obtained by key below

get key
如果有 key 就会返回对应的 value,否则就会返回 nil,上面的 nil 就是表示空或者不存在的意思。
127.0.0.1:6379> get key1
"value1"
127.0.0.1:6379> get key2
(nil)

KEYS

  • keys can query the key in the current redis

KEYS pattern
  • pattern is the matching pattern

  • ?: means match any more than one character

  • *: match any number of characters

  • [abc..]: match the characters inside the square brackets

  • [^abc]: matches characters except those in square brackets

  • [ab]: Match the characters in a~b

You can try it below

Insert a batch of data

127.0.0.1:6379> set hello 1
OK
127.0.0.1:6379> set hallo 1
OK
127.0.0.1:6379> set hbllo 1
OK
127.0.0.1:6379> set heeeeello 1
OK
127.0.0.1:6379> keys *
1) "hallo"
2) "hbllo"
3) "heeeeello"
4) "hello"
5) "key1"
  • match any character

    127.0.0.1:6379> keys h?llo
    1) "hallo"
    2) "hbllo"
    3) "hello"

  • match any number of characters

    127.0.0.1:6379> keys h*llo
    1) "hallo"
    2) "hbllo"
    3) "heeeeello"
    4) "hello"

  • matches what's in parentheses

    127.0.0.1:6379> keys h[ab]llo
    1) "hallo"
    2) "hbllo"

  • Matches content that is not inside parentheses

    127.0.0.1:6379> keys h[^e]llo
    1) "hallo"
    2) "hbllo"

  • match the range in parentheses

    127.0.0.1:6379> keys h[ab]llo
    1) "hallo"
    2) "hbllo"

EXISTS

  • This command is used to query whether the key exists

exixts key [key...]
  • The command can be followed by multiple keys

  • Returns the number of keys found in the query

  • Time complexity bit O(N) N is the number of keys

  • Inquire

    127.0.0.1:6379> exists hello hallo
    (integer) 2

OF THE

  • Delete key and corresponding value

del key [key...]
  • Del can also be followed by multiple keys

  • The return value indicates the number of deleted keys

  • delete a data

    127.0.0.1:6379> del hello
    (integer) 1

  • Delete multiple data

    127.0.0.1:6379> del hallo hbllo
    (integer) 2

  • Delete non-existing data

    127.0.0.1:6379> del abc
    (integer) 0

EXPIRE

  • Set the expiration time, in seconds

expire key second
  • expire must be an existing key

  • A return value of 1 means that the setting is successful, and 0 means that it failed

  • But second is too long for the computer, in fact there is a pexpire in milliseconds

  • set expiration time

    127.0.0.1:6379> expire hello 10
    (integer) 1

  • Set expiration time for empty key

    127.0.0.1:6379> expire abc 10
    (integer) 0

Set the expiration time for an existing key and return 1, and set the expiration time for an existing key and return 0, indicating that the setting failed

And after the setting is successful, the key will be automatically deleted after the time expires

TTL

  • View the expiration time, which corresponds to expire

ttl key
  • The returned unit is seconds

  • If you want to view the millisecond level, it is pttl, which corresponds to pexpire

  • The return value is the remaining expiration time, if it is -1, it means no expiration time is set, if it is -2, it means there is no such key

  • set expiration time

    127.0.0.1:6379> expire hello 30
    (integer) 1

  • Check expiration time

    127.0.0.1:6379> ttl hello
    (integer) 25

  • View the expiration time of the expiration time key that has not been set

    127.0.0.1:6379> ttl key1
    (integer) -1

  • View the expiration time without key

    127.0.0.1:6379> ttl abc
    (integer) -2

redis expiration policy

The expiration strategy of redis mainly has two aspects:

  • regularly delete

  • lazy delete

  • First of all, let’s talk about lazy deletion. Lazy deletion means that whenever a user accesses the data, redis will perform a check. If it is expired, then redis will delete the key and return nil

  • Regular deletion means that every once in a while, redis will extract a part of the data to check the expiration time, and delete it if it expires

  • Why is it part of the data? Because redis is single-threaded, if the amount of data is too large, then redis will be blocked, causing other services to be unable to be processed, so redis extracts a part of the data for inspection each time

  • However, the combination of the two strategies is generally effective

timer policy

The redis expiration strategy can also be solved by a timer, but this timer redis has not been implemented, but you can introduce how to implement it if you use a timer

Priority-Based Queue Timer

  • All keys with expiration time can be put into a priority queue

  • The priority is the expiration time, the closer the expiration time is, the higher the priority

  • So the first element must be the first to expire, so we only need to check the first element

  • And the first element does not have to be checked all the time, you can check it every once in a while

  • Then you can divide a thread and let the thread check the expiration time. If you find that the expiration time has not yet arrived, you can let the thread hang up first.

Time wheel based timer

  • First of all, there can be a circular queue, and each grid in the circular queue represents an expiration time for a period of time

  • A linked list can be saved in each grid, and the expiration time of all events in the linked list is within the time range indicated by the grid

  • One of the pointers starts from a specific position in the circular queue, and goes backward every once in a while (the time indicated by the grid), and then checks the expiration time inside, and deletes it when it expires

  • When encountering a new event, check the expired event of the event and push it to the corresponding grid

Although redis does not use these two methods, these two methods are also more efficient ways to handle expired events

TYPE

  • Returns the type of the value corresponding to the key

type key
  • Returns the type of the value corresponding to the key

  • In redis, the keys are all string types

  • Returns none if there is no key

  • Insert different data

    127.0.0.1:6379> lpush key2 111 222 333
    (integer) 3
    127.0.0.1:6379> sadd key3 aaa bbb ccc
    (integer) 3
    127.0.0.1:6379> hset key4 field1 value1
    (integer) 1

  • View different data types

    127.0.0.1:6379> type key1
    string
    127.0.0.1:6379> type key2
    list
    127.0.0.1:6379> type key3
    set
    127.0.0.1:6379> type key4
    hash

Supongo que te gusta

Origin blog.csdn.net/m0_73455775/article/details/132678212
Recomendado
Clasificación