示例#1
0
<?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 = 10;
//抢购数量
if ($mywatchkey < $rob_total) {
    $redis->watch("mywatchkey");
    $redis->multi();
    //设置延迟,方便测试效果。
    sleep(5);
    //插入抢购数据
    $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 "抢购成功!<br/>";
        echo "剩余数量:" . ($rob_total - $mywatchkey - 1) . "<br/>";
        echo "用户列表:<pre>";
    } else {
        echo "手气不好,再抢购!";
        exit;
    }
}
?>
  
示例#2
0
<?php

header('Access-Control-Allow-Origin:*');
$IM = 'Im.malu.me';
$IM_ROOM = array('C1' => 0, 'C2' => 0, 'M1' => 0, 'M2' => 0, 'AI' => 0);
$redis = new redis();
$redis->connect('localhost', 6379);
$result = $redis->auth('REDIS_PASS');
if ($result) {
    $pipe = $redis->multi(Redis::PIPELINE);
    foreach ($IM_ROOM as $key => $value) {
        $pipe->lrange($IM . '_' . $key, 0, 0);
    }
    $result = $pipe->exec();
    //var_dump($result);
}
if ($result) {
    $i = 0;
    foreach ($IM_ROOM as $key => $value) {
        if ($result[$i]) {
            $r = explode('-', $result[$i][0]);
            $IM_ROOM[$key] = $r[0];
        }
        $i++;
    }
    //var_dump($IM_ROOM);
}
$json_result = json_encode($IM_ROOM);
echo $json_result;
//$rlocal->setex($IM,2,$json_result);