How to realize optimistic Lock with php+redis
This article introduces the relevant knowledge of "how to use php+redis to achieve optimistic locks". 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!
Header ("content-type:text/html;charset=utf-8")
$redis = new redis ()
$result = $redis- > connect ('127.0.0.1, 6379)
$mywatchkey = $redis- > get ("")
$rob_total = 10
/ / snapped up quantity
If ($mywatchkeywatch ("mywatchkey")
$redis- > multi ()
/ / set delay to facilitate testing effect.
Sleep (5)
/ / insert panic buying data
$redis- > hSet ("mywatchlist", "user_id_" .mt _ rand (1, 9999), time ())
$redis- > set ("mywatchkey", $mywatchkey+1)
$rob_result = $redis- > exec ()
If ($rob_result) {
$mywatchlist = $redis- > hGetAll ("mywatchlist")
Echo "snap up success!"
Echo "remaining quantity:" ($rob_total-$mywatchkey-1). ""
Echo "user list:"
Var_dump ($mywatchlist)
} else {
Echo "if you are unlucky, buy it again!"
Exit
}
}
This is the end of the content of "how to use php+redis to achieve optimistic locks". 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!