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

What are the redis features?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Today, I would like to talk to you about what the features of redis are, many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

1. The lifetime of the bond expire

Redis can use the expire command to set the lifetime of a key, and redis will delete it automatically after the time.

Command:

① expire sets time to Live (in / sec) expire a 10

② pexpire sets time to Live (in milliseconds)

③ ttl/pttl view the remaining lifetime of the key ttl a

④ persist cancels time to Live

⑤ expireat [key] unix timestamp 1351858600

⑥ pexpireat [key] unix timestamp (milliseconds) 1351858700000

Application scenarios:

① time-limited offer information

② website data cache (for some data that needs to be updated regularly, such as points ranking)

③ restricts the frequency of website visitors (for example, up to 10 visits per minute)

II. Redis transactions (transaction)

A redis transaction is a collection of commands, and a transaction, like a command, is the smallest unit of execution. The set of commands of a transaction is executed, but not executed; the principle of redis transaction implementation is to cache the commands first, and then let redis execute them sequentially.

Note: redis transactions do not support rollback, and if they fail, the collection of transaction commands must be re-executed, and it is precisely because redis transactions do not support rollback that redis transactions can be kept concise and fast. You can use the watch celebrity to monitor the status of the specified key during the execution of the 10th five-year Plan.

Monitor one or more keys, when the monitored key value is modified to prevent the execution of a subsequent transaction, but there is no guarantee that other clients will not modify this key value, so we need to re-execute the command in the transaction after the transaction execution fails.

Note: after executing the exec command of the transaction, watch will cancel the monitoring of all key values.

Command:

Multi transaction start

.

The exec transaction ends and starts executing the commands in the transaction

Discard abandons transaction

Error handling:

1: syntax error: fatal error, all commands in the transaction will not be executed

2: run error: does not affect the execution of other commands in the transaction

Application scenarios:

A set of commands must be executed at the same time or none at all.

We want to ensure that a set of commands are not inserted by other commands during execution.

3. Sorting of data (sort)

The sort command sorts list types, collection types, and ordered collection types.

Command:

Sort key [desc] [limit offset count]

Sort list desc

Sort by sort list limit 0 2, take 2 pieces of data from 0

Sort list store tmp_list is sorted and saved to tmplist

The above is the sort of numbers.

The characters are sorted as follows:

Sort list alpha is sorted by dictionary

By reference key (the reference key can be a string type or a field of type hash. The format of hash type is: key name-> field name)

If there is no * sign in the reference key, it is not sorted.

If the reference key for an element does not exist, the default reference key value is 0

For example:

List (1, 2, 3, 4)

Set score:1 20 score:2 68 score:3 56 score:4 88

Sort list by score:*

Lpush list 2 1 4 3

Hmset post:1 title one time 13332190

Hmset post:2 title two time 13332190

Hmset post:3 title three time 13332190

Hmset post:4 title four time 13332190

Sort list by post:*- > time desc sort by time

Sort list by post:*- > time get post:*- > title get post:*- > time get # is also returned together with the sort field

Extend the get parameter (already used above)

The rules for the get parameter are the same as those for the by parameter

Get # (returns the value of the element itself)

Extended store parameter

Use the store parameter to save the sort results of sort to a specified list

Performance optimization

1: minimize the number of elements in the key to be sorted

2: use the limit parameter to get only the data you need

3: if the amount of data to be sorted is large, use the store parameter to cache the results as much as possible.

4. Config command in redis

Using config set, you can set the parameter information dynamically, and the server will fail after restart.

Config set appendonly yes

Config set save "90 1 30 10 60 100"

Use config get to view all the parameters that can be set using the config set command

Config get *

Use the config rewrite command to rewrite the redis.conf file specified when starting the Redis server (Redis 2.8 or above can only be used), mainly to save the commands dynamically specified by config set to the configuration file.

Config rewrite

Note: the rewriting of the redis.conf file by the config rewrite command is atomic and consistent: if an error occurs or the server crashes during the rewrite, the rewrite fails and the original redis.conf file is not modified. If the rewrite is successful, the redis.conf file is the new file after the rewrite.

After reading the above, do you have any further understanding of what redis features are? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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

Servers

Wechat

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

12
Report