You have to work really hard to look effortless!

Wechat search public number [long Coding road], together From Zero To Hero!

preface

Before we introduced the basic data structure of Redis and related to the derived structure, this article we briefly introduced the related operations of the database.

DBSIZE

Available version: >= 1.0.0

Time complexity: O(1)

The command format

DBSIZE
Copy the code

Command description

  • Returns the number of current database keys

The return value

An integer value

The sample

127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> mset name jack age 10 gender male
OK
127.0.0.1:6379> dbsize
(integer) 3
Copy the code

KEYS

Available version: >= 1.0.0

Time complexity: O(N), where N is the number of keys in the database

The command format

KEYS pattern
Copy the code

Command description

  • Returns the key in the database that matches pattern

  • Pattern supports regular expressions, for example:

    • h? Llo matches Hello, HallO, and HXlLO

    • H * LLO matches HLLO and Heeeello

    • H [ae] lLO matches Hello and Hallo, but not Hillo

    • H [^e] lLO matches hallo, hbllo does not match Hello

    • H [A-B] LLO matches HallO and HBLLO

    Warning ⚠ ️

    The keys command is applicable to debugging in the test environment and cannot be used in the production environment. If the database is particularly large, the keys command can seriously affect performance.

    If you want to find a key that matches your requirements, consider using the SCAN command or the set structure to store the data.

The return value

List: List of keys matching pattern

The sample

127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> mset hello 1 hella 2 hetta 3
OK
127.0.0.1:6379> keys hell*
1) "hello"
2) "hella"
Copy the code

FLUSHDB

Available version: >= 1.0.0

Time complexity: O(N), where N is the number of keys in the database

The command format

FLUSHDB [ASYNC|SYNC]
Copy the code

Command description

  • deleteCurrent databaseFor all data, the command does not fail
  • This command is used by defaultsynchronousWay to delete all data from Redis 6.2 can be configuredlazyfree-lazy-user-flush = yesTo set the default deletion mode to asynchronous
  • In asynchronous deletion, only the keys that exist when the command is executed are deleted. The keys that are added between receiving the command and executing the command are not deleted
  • Alternatively, you can specify synchronous or asynchronous deletion directly in the command
    • ASYNC: Asynchronous deletion
    • SYNC: Synchronous deletion

The return value

string

The sample

127.0.0.1:6379> set name jack
OK
127.0.0.1:6379> get name
"jack"
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> get name
(nil)
Copy the code

FLUSHALL

Available version: >= 1.0.0

Time complexity: O(N), where N is the number of keys in the database

The command format

FLUSHALL [ASYNC|SYNC]
Copy the code

Command description

  • deleteAll databasesThe command does not fail for all keys in.
  • This command is used by defaultsynchronousWay to delete all data from Redis 6.2 can be configuredlazyfree-lazy-user-flush = yesTo set the default deletion mode to asynchronous
  • In asynchronous deletion, only the keys that exist when the command is executed are deleted. The keys that are added between receiving the command and executing the command are not deleted
  • Alternatively, you can specify synchronous or asynchronous deletion directly in the command
    • ASYNC: Asynchronous deletion
    • SYNC: Synchronous deletion

The return value

string

The sample

127.0.0.1:6379 > flushall OKCopy the code

SELECT

Available version: >= 1.0.0

Time complexity: O(N), where N is the number of keys in the database

The command format

SELECT index
Copy the code

Command description

  • Select a logical database, default is0
  • RedisThe configuration file contains the following configuration: Databases 16. The database is similar to a namespace. All databases are persisted to the same RDB/AOF file. But different databases can have the same key. likeFLUSHDB,SWAPDB,RANDOMKEYWorks only on specific databases
  • In practice, a database should hold different keys for the same application, rather than using Redis instances for different applications
  • When usingRedis ClusterMode,SELECTCommand is not available becauseRedis ClusterOnly database 0 is supported.

The return value

string

The sample

127.0.0.1:6379> set age 10
OK
127.0.0.1:6379> get age
"10"

#Switching database 1
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get age
(nil)
Copy the code

SWAPDB

Available version: >= 1.0.0

Time complexity: O(N), where N is the number of clients monitoring or blocking on both databases

The command format

SWAPDB index1 index2
Copy the code

Command description

  • Swap two specified databases, causing the data of the two databases to be immediately interchanged.

    For example, SWAPDB 0 1, clients connected to database 0 can immediately see data in database 1 and vice versa.

The return value

String: “OK”

The sample

SWAPDB 0 1
Copy the code

SCAN

Available version: >= 1.0.0

Time complexity: the time complexity of single invocation is O(1), and the time complexity of complete iterative database is O(N), where N is the number of elements in the data set.

The command format

SCAN cursor [MATCH pattern] [COUNT count] [TYPE type]
Copy the code

Command description

  • SCANOf or relating to commandsSSCANThe command,HSCANCommands andZSCANCommands are used to incrementally iterate over collection elements
    • SCANThe command is used to iterate over database keys in the current database.
    • SSCANThe Set command iterates over elements in a Set structure.
    • HSCANThe Hash command is used to iterate over key-value pairs in a Hash structure.
    • ZSCANThe Sorted Set command is used to iterate over the elements and their scores in a Sorted Set.
  • These commands can incrementally iterate through the entire database, returning only a small amount of data at a time, so they can be used in a production environment without worrying about imagesKEYS,SMEMBERSBlock the database as command.
  • likeKEYS,SMEMBERSAnd block commands that return all data at once;SCANRelated commands only return a limited amount of data at a time, and can be called through multiple iterations to finally get the full amount of data.
  • SCAN,SSCAN,HSCAN,ZSCANMultiple commands function similarly, and parsing can cover up to four commands. The only difference is,SSCAN,HSCAN,ZSCANThe first argument to the command is the data structure to iterate overkeyAnd theSCANCommand not requiredkeyBecause theSCANThe command is to traverse the current database.

Basic Usage of SCAN

SCAN is an iterator-based cursor, and each time a command is called, the server returns an updated cursor for the next call. The cursor needs to be set to 0 when the iteration begins, and the end of the iteration when Redis returns 0. Here is an example of a SCAN iteration:

First, set the cursor to 0, indicating the beginning of iteration. In the second iteration, 17 returned from the first iteration is used as the cursor, and the first return value of the second iteration is 0, indicating the end of the entire iteration process.

As can be seen from the example, SCAN returns two values, the first is the cursor value of the next iteration, and the second is the element array.

Redis 127.0.0.1:6379 > scan 0 1) 2) "17" 1) "key: 12" 2) "key: 8" 3) "key: 4" 4) "key: 14" 5) "is the key: 16" 6) "key: 17" 7) Key: 15 8) of "key: 10" 9) "key: 3" 10) "key: 7" 11) "key: 1" redis 127.0.0.1:6379 > 17 (1) "0" (2) scan 1) "key: 5" 2) "key: 18" 3) "key:0" 4) "key:2" 5) "key:19" 6) "key:13" 7) "key:6" 8) "key:9" 9) "key:11"Copy the code

Guarantee of the SCAN command

The SCAN command, as well as other commands in the SCAN family, guarantees the user the following after completing the iteration process:

  • A complete iteration returns all elements in the collection from the beginning to the end of the iteration. That is, if an element is in the collection at the beginning of the iteration and is still in the collection at the end of the iteration, the element must be in the return value of one of the SCAN iterations.
  • A complete iteration does not return elements from start to finish that do not exist in the collection. That is, if an element is removed before the iteration begins and is not added again throughout the iteration, SCAN guarantees that the element will not be returned.

Because SCAN uses cursors to represent traversal status, it has the following disadvantages:

  • An element may be returned multiple times
  • If an element does not always exist in the collection during iteration, it is uncertain whether the element can be returned.

Return the number of elements

The SCAN command family does not guarantee that the number of elements returned each time is within a given range. It is also possible for the command to return zero elements, but the iteration is not considered to be over as long as the return value is not zero.

However, the number of elements returned by iteration conforms to certain rules:

  • If iterating over a large data set, each iteration returns about 10 elements.
  • If iterating over a small dataset (a small Set, Hash, Sorted Set), the iteration might return all elements;

However, the user can use the COUNT option to adjust the order of magnitude of the element returned by each call.

The COUNT option

Although the SCAN command does not guarantee the number of elements returned, we can use the COUNT parameter to adjust the behavior of the command to some extent.

With the COUNT parameter, the user specifies the number of elements that should be returned in each iteration.

  • althoughCOUNTThe parameter is just a hint to the generation command, but in most cases it is valid.
  • When iterating through a larger collection, the number of elements returned each time is the COUNT value, or a little more;
  • When iterating over a small collection, the command ignores the COUNT argument and returns all elements;
  • The COUNT value used for each iteration can be different

MATCh options

Like the KEYS command above, the SCAN command can iterate over only the elements that meet the specified pattern. Such as:

Redis 127.0.0.1:6379> sadd myset 12 3 foo foobar feelsgood (INTEGER) 6 redis 127.0.0.1:6379> sscan myset 0 match f* 1) "0" 2) 1) "foo" 2) "feelsgood" 3) "foobar"Copy the code

Note that the MATCH parameter is filtered after the data is retrieved from the collection and then returned to the client. That is, if there are few elements that can be matched in the set, the data returned by each iteration will be small or even empty:

Redis 127.0.0.1:6379> scan 0 MATCH *11* 1) "288" 2) 1) "key:911" redis 127.0.0.1:6379> Scan 288 MATCH *11* 1) "224" 2) (empty list or set) redis 127.0.0.1:6379> scan 224 MATCH *11* 1) "80" 2) (empty list or set) redis 127.0.0.1:6379> scan 80 MATCH *11* 1) "176" 2) (empty list or set) redis 127.0.0.1:6379> Scan 176 MATCH *11* COUNT 1000 1) "0" 2) 1) "key:611" 2) "key:711" 3) "key:118" 4) "key:117" 5) "key:311" 6) "key:112" 7) "key:111" 8) "key:110" 9) "key:113" 10) "key:211" 11) "key:411" 12) "key:115" 13) "key:116" 14) "key:114" 15) "key:119" 16) "key:811" 17) "key:511" 18) "key:11"Copy the code

As you can see from the above example, the previous calls returned little or no data, and the last one returned more matches by forcing the iteration of more elements by specifying COUNT=1000.

The TYPE parameter

Since Redis 6.0, you can return a specific TYPE of key by specifying the TYPE parameter. The TYPE parameter applies only to the SCAN command, not the SSCAN, HSCAN, and ZSCAN commands.

TYPE is the underlying data TYPE. For Geo, HyperLogLog, and Bitmap, the TYPE field cannot be distinguished, because the underlying implementation of these several are based on other basic types. For example, Geo is based on ZSET.

Redis 127.0.0.1:6379> GEOADD geokey 00 value (integer) 1 redis 127.0.0.1:6379> ZADD zkey 1000 value (integer) 1 redis 127.0.0.1:6379> TYPE geokey zset redis 127.0.0.1:6379> SCAN 0 TYPE zset 1) "0" 2) 1) "geokey" 2) "zkey"Copy the code

Like the MATCH option, the TYPE parameter is filtered after the data is retrieved and then returned to the client, so using this parameter does not reduce the number of elements that need to be traversed.

Execute multiple iterations concurrently

Because the iteration state is identified using a cursor, and the cursor state client saves it, multiple clients can iterate over a collection at the same time.

Stop iterating

Because iteration cursors are saved by the client, the server does not save state, and the client can stop iteration at will without affecting the server.

Iterating with the wrong cursor

Iterating with interrupts, negative values, out-of-range, or other illegal cursors does not crash the server, but can cause commands to behave in an unknown way. Valid bid values are as follows:

  • The 0 value given at the beginning of the iteration
  • The cursor value returned by the last iteration

A guarantee that the iteration is over

Iteration stops only if the size of the iterated dataset is within a given range, otherwise iterating over an ever-growing dataset may never stop.

It makes sense that a collection keeps growing, and if the iteration ends, you need to make sure that the iteration is faster than the growth, or you never stop iterating.

The return value

SCAN, SSCAN, HSCAN, and ZSCAN all return two values. The first value is the updated cursor value and the second value is the array of elements.

  • The array returned by SCAN is a list of keys
  • The array returned by SSCAN is a list of Set elements
  • The element array returned by HSCAN contains two elements: filed field and Value value
  • The array of elements returned by ZSCAN contains two types of elements: elements and score values

The sample

Redis 127.0.0.1:6379> hmset hash name Jack age 33 OK redis 127.0.0.1:6379> hscan hash 0 1) "0" 2) 1) "name" 2) "Jack" 3)  "age" 4) "33"Copy the code

More and more

Personal blog: lifelmy.github. IO /

Wechat official account: Long Coding road