關(guān)于“php讀寫redis”的問(wèn)題,小編就整理了【1】個(gè)相關(guān)介紹“php讀寫redis”的解答:
php使用redis怎么解決秒殺中的超賣問(wèn)題?使用redis 的隊(duì)列+watch解決,把秒殺商品放入隊(duì)列,搶到則pop商品,隊(duì)列用完,則停止搶購(gòu)
<?php
header("content-type:text/html;charset=utf-8");
$redis = new redis();
$result = $redis->connect('127.0.0.1', 6379);
$mywatchkey = $redis->get("mywatchkey");
$rob_total = 100; //搶購(gòu)數(shù)量
if($mywatchkey<$rob_total){
$redis->watch("mywatchkey");
$redis->multi();
//設(shè)置延遲,方便測(cè)試效果。
sleep(5);
//插入搶購(gòu)數(shù)據(jù)
$redis->hSet("mywatchlist","user_id_".mt_rand(1, 9999),time());
$redis->set("mywatchkey",$mywatchkey+1);
到此,以上就是小編對(duì)于“php讀寫redis”的問(wèn)題就介紹到這了,希望介紹關(guān)于“php讀寫redis”的【1】點(diǎn)解答對(duì)大家有用。