In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the methods of adding and unlocking php and redis. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Business background: locks are needed in room board games to prevent dirty reading of redis data caused by concurrent operations, such as adding a user to enter a room:
In the case of concurrency, there will be dirty reading in get RoomUsers.
Solution: lock the room to allow only one client operation in a room at a time, while other concurrent clients wait; that is, blocking locks
Locking: there are several ways to lock redis: incr, set, setnx, hSetnx. You can refer to this article: several implementations of redis locking.
I use set this way here.
$roomId = $_ GET ['roomId']; $user = $GET [' user']; / / Zhang San'$key = "LockRoom: {$roomId}"; $value = $roomId.uniqid (); $ex = 3X / if $key does not exist, set the value of $key to $value with a validity period of 3s; / / return TRUE / FALSEwhile (true) {$res = $this- > redis- > set ($key, $value, ['nx',' ex' = > $ex]) If ($res) {break;} usleep (5000);} / / add users to the room $roomUsers = $this- > redis- > get ("Room: {$roomId}: Users"); / / [Li Si, Wang Wu] $roomUsers [] = $user;$this- > redis- > set ("Room: {$roomId}: Users", $roomUsers); / / [Li Si, Wang Wu, Zhang San]
Unlock: of course you have to unlock it after operation, but you have to wait at least 3 seconds if you don't unlock it.
Unlock use delete to delete key;, but there is a pit, so you cannot directly use delete, because if client01 acquires the lock and takes more than 3 seconds to add a user to enter the room, client02 will also acquire the lock and set 3s, and then delete key will delete the lock set by client02 when the client01 operation is finished.
It is recommended to use lua code to perform deletions because lua execution is atomic.
/ / add users to the room $roomUsers = $this- > redis- > get ("Room: {$roomId}: Users"); / / ['Li Si', 'Wang Wu'] $roomUsers [] = $user;$this- > redis- > set ("Room: {$roomId}: Users", $roomUsers) / / ['Li Si', 'Wang Wu', 'Zhang San'] / / lua script unlocks / / determines whether the value of key is value before TRUE is deleted, so the design of $value must be randomly unique $script ='if redis.call ("get", KEYS [1]) = ARGV [1] then return redis.call ("del", KEYS [1]) else return 0end'; $this- > redis- > eval ($script, array ($key, $value), 1) On the php and redis implementation to add and unlock methods to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.