Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Collation of common commands in Redis

2025-03-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "sorting out the common commands of Redis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Collation of Redis common commands

Business

The principle of a transaction is to send commands that belong to a transaction to redis, and then let redis execute these commands in turn.

Error handling

(1) grammatical errors. Syntax error means that the command does not exist or the number of command parameters is incorrect. In this case, as long as there is a syntax error in a command in the transaction, redis will directly return an error after executing the exec command, and even the command with correct syntax will not be executed.

(2) running error. A run error refers to an error that occurs when a command is executed, such as using a hash command to manipulate keys of a collection type. This error cannot be found by redis before it is actually executed, so such a command will be accepted and executed by redis in a transaction. If a command in the transaction has a run error, other commands in the transaction will continue to execute (including commands after the error command).

Reids transactions do not have the rollback function provided by relational database transactions, so developers must clean up the rest after the transaction execution error.

MULTI

Marks the beginning of a transaction block.

Multiple commands within the transaction block are placed in a queue in order and are finally executed atomic by the EXEC command.

EXEC

Execute commands within all transaction blocks.

If a key (or some) is being monitored by the WATCH command and there are commands related to this (or these) key in the transaction block, the EXEC command is executed and takes effect only if the key (or these) is not altered by other commands, otherwise the transaction is interrupted (abort).

Watch command

Watch key [key...]

Monitor one or more key. If this or these key is altered by other commands before the transaction is executed, the transaction will be interrupted and the monitoring continues until the exec command

Unwatch

Unmonitor all key from the watch command

Survival time

Expire

The expire command is used as expire key seconds, where the seconds parameter represents the survival time of the key in seconds, and the parameter must be an integer

If the command returns 1, the setting is successful, and 0 indicates that the key does not exist or the setting failed.

If you want to know how long a key will be deleted, you can use the ttl command. The return value is the remaining time of the key (in seconds)

If you want to cancel the time-to-live setting of the key (that is, restore the key to permanent), you can use the persist command. Returns 1 if the time to live is successfully cleared. Otherwise, 0 is returned.

In addition to the persist command, using the set and getset commands to assign values to keys will also know the lifetime of the keys.

Note: incr, lpush, hset, zrem commands do not know the survival time of image keys.

The pexpire command should be used to precisely control the lifetime of keys. The unit of the command is millisecond

You can use the pttl command to return the remaining time of the key in milliseconds

Another less commonly used command: expireat and pexpireat, the second parameter of this command represents the expiration time of the key's lifetime, expireat unit second pexpireat unit millisecond

Sort

This command sorts list types, collection types, and ordered collection type keys

List type:

When sorting an ordered collection type, it ignores the score of the element and sorts only on the value of the element itself.

In addition to arranging numbers, the sort command can also arrange non-numeric elements in dictionary order through the alpha parameter

The desc parameter of the sort command enables elements to be arranged in order from largest to smallest.

The sort command also supports the limit parameter to return the specified range of results, using the same limit offset count as the sql statement, which means to skip the first offset element and get the next count element

The by parameter of the sort command. By default, sort uid is sorted directly by the values in uid. Through the by parameter, you can let uid sort by the elements of other keys.

User_level_* is a placeholder. He first takes out the value in uid, and then uses this value to find the corresponding key. For example, when sorting the uid list, the program will first take out the values of uid 1, 2, 3, 4, and then use user_level_1, user_level_2, user_level_3.

And user_level_4 are used as the weight of the sort uid.

Using the get option, you can extract the corresponding key values according to the sorted results

There can be multiple get parameters in a sort command (while there can be only one by parameter)

By default, the sort command returns the sort result directly. If you want to save the sort result, you can use the store parameter. The key type after saving is list type.

This is the end of the content of "sorting out the Common commands of Redis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report