redisカウンタ、redis同時ロック


 
  
/**
 *               
 * @return string
 */
public function incrPacketNumAction($key)
{
    $redis = $this->getRedis();
    $key="packet_".$key;
    $num=$redis->incr($key);
    $time=strtotime(date("Y-m-d",strtotime("+1 days")))-1-time();//         0 
    $redis->expire($key,$time);
    return $num;
}
/**
 *         
 * @return string
 */
public function PacketLockAction($key,$expire)
{
    $redis = $this->getRedis();
    $key="open_packet_".$key;
    $is_lock = $redis->setnx($key, time()+$expire);
    //      
    if(!$is_lock){
        //        
        $lock_time = $redis->get($key);
        //     ,   ,    
        if(time()>$lock_time){
            $this->PacketUnLockAction($key);
            $is_lock = $redis->setnx($key, time()+$expire);
        }
    }
    return $is_lock? true : false;
}

/**
 *           
 * @return string
 */
public function PacketUnLockAction($key)
{
    $redis = $this->getRedis();
    $key="open_packet_".$key;
    return $redis->del($key);
}