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

How does redis lock it?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article mainly introduces the method of redis locking, which has certain reference value and can be used for reference by friends who need it. The following information is about the detailed steps of redis locking.

Redis lock classification

The subtables of lock commands that can be used by redis are INCR, SETNX, SET

The first lock command INCR

The idea of this locking is that if key does not exist, the value of key will be initialized to 0 before performing an INCR operation to add one.

Then other users are performing INCR operations to add a period of time, and if the number returned is greater than 1, the lock is in use.

1. Client A requests the server to acquire the key. A value of 1 indicates that the lock has been acquired.

2. Client B also requests the server to obtain the key. A value of 2 indicates that the acquisition of the lock failed.

3. Client An executes the code and deletes the lock

4. Client B waits for a period of time to obtain a value of 1 for key when it requests, indicating that the lock has been acquired successfully.

5. Client B executes the code and deletes the lock

$redis- > incr ($key); $redis- > expire ($key, $ttl); / / set the generation time to 1 second

The second lock SETNX

The idea of locking is to set key to value if key does not exist

If key already exists, SETNX does not take any action

1. Client A requests the server to set the value of key. If the setting is successful, it means that the lock is successful.

2. Client B also requests the server to set the value of key. If it fails, it means that locking failed.

3. Client An executes the code and deletes the lock

4. Client B requests to set the value of key after waiting for a period of time. The setting is successful.

5. Client B executes the code and deletes the lock

$redis- > setNX ($key, $value); $redis- > expire ($key, $ttl)

The third lock SET

There is a problem with both of the above methods, and you will find that you need to set key expiration. So why set key expiration? If the request execution exits unexpectedly for some reason, resulting in a lock created but not deleted, the lock will always exist so that the cache will never be updated in the future. So we need to add an expiration time to the lock in case something happens.

But setting it with Expire is not an atomic operation. So atomicity can also be ensured through transactions, but there are still some problems, so officials have cited another one, and the use of the SET command itself has included the ability to set the expiration time since version 2.6.12.

1. Client A requests the server to set the value of key. If the setting is successful, it means that the lock is successful.

2. Client B also requests the server to set the value of key. If it fails, it means that locking failed.

3. Client An executes the code and deletes the lock

4. Client B requests to set the value of key after waiting for a period of time. The setting is successful.

5. Client B executes the code and deletes the lock

$redis- > set ($key, $value, array ('nx',' ex' = > $ttl)); / / ex represents seconds

What is described above is the details of redis locking, and the specific usage needs to be used in hands-on experiments in order to understand. If you want to know more about it, welcome to follow the industry information channel!

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

Database

Wechat

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

12
Report