예제 #1
0
 function onPacket($serv, $data, $addr)
 {
     $req = unserialize($data);
     //错误的请求
     if (empty($req['cmd'])) {
         return;
     }
     $ipAddress = $addr['address'];
     //没有建立映射
     if (empty($this->ipMap[$ipAddress])) {
         //建立映射
         if ($req['cmd'] == 'putInfo' and !empty($req['info'])) {
             $nodeInfo = new NodeInfo();
             $nodeInfo->setInfo($req['info']);
             $nodeInfo->address = $ipAddress;
             $nodeInfo->port = $addr['port'];
             $this->ipMap[$ipAddress] = $nodeInfo;
             $this->log("new node, address={$ipAddress}, version=" . $nodeInfo->version);
             //存储hostname
             $this->redis->sAdd(self::KEY_NODE_LIST, $nodeInfo->hostname);
         } else {
             $this->serv->sendto($addr['address'], $addr['port'], serialize(['cmd' => 'getInfo']));
             //存储Socket用于强制升级
             $this->redis->hSet(self::KEY_NODE_SOCKET, $addr['address'], $addr['port']);
         }
     } else {
         $nodeInfo = $this->ipMap[$ipAddress];
         call_user_func([$this, '_udp_' . $req['cmd']], $nodeInfo, $req);
     }
 }
예제 #2
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;
    }
}
?>