Exemplo n.º 1
0
 /**
  * get value from the queue
  *
  * @param bool $block if block when the queue is empty
  * @return bool|string
  */
 public function get($block = false)
 {
     if (!$block) {
         return $this->redis->rPop($this->channel);
     } else {
         while (true) {
             $record = $this->redis->rPop($this->channel);
             if ($record === false) {
                 usleep(1000);
                 continue;
             }
             return $record;
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Remove and return a value from a list
  *
  * @param string $key
  * @return string|null
  */
 public function rPop($key)
 {
     $result = $this->_redis->rPop($key);
     if (false === $result) {
         $result = null;
     }
     return $result;
 }
Exemplo n.º 3
0
 public function testDifferentTypeHash()
 {
     $key = '{hash}hash';
     $dkey = '{hash}hash';
     $this->redis->del($key);
     $this->assertEquals(1, $this->redis->hSet($key, 'key', 'value'));
     // string I/F
     $this->assertEquals(FALSE, $this->redis->get($key));
     $this->assertEquals(FALSE, $this->redis->getset($key, 'value2'));
     $this->assertEquals(FALSE, $this->redis->append($key, 'append'));
     $this->assertEquals(FALSE, $this->redis->getRange($key, 0, 8));
     $this->assertEquals(array(FALSE), $this->redis->mget(array($key)));
     $this->assertEquals(FALSE, $this->redis->incr($key));
     $this->assertEquals(FALSE, $this->redis->incrBy($key, 1));
     $this->assertEquals(FALSE, $this->redis->decr($key));
     $this->assertEquals(FALSE, $this->redis->decrBy($key, 1));
     // lists I/F
     $this->assertEquals(FALSE, $this->redis->rPush($key, 'lvalue'));
     $this->assertEquals(FALSE, $this->redis->lPush($key, 'lvalue'));
     $this->assertEquals(FALSE, $this->redis->lLen($key));
     $this->assertEquals(FALSE, $this->redis->lPop($key));
     $this->assertEquals(FALSE, $this->redis->lrange($key, 0, -1));
     $this->assertEquals(FALSE, $this->redis->lTrim($key, 0, 1));
     $this->assertEquals(FALSE, $this->redis->lGet($key, 0));
     $this->assertEquals(FALSE, $this->redis->lSet($key, 0, "newValue"));
     $this->assertEquals(FALSE, $this->redis->lrem($key, 'lvalue', 1));
     $this->assertEquals(FALSE, $this->redis->lPop($key));
     $this->assertEquals(FALSE, $this->redis->rPop($key));
     $this->assertEquals(FALSE, $this->redis->rPoplPush($key, $dkey . 'lkey1'));
     // sets I/F
     $this->assertEquals(FALSE, $this->redis->sAdd($key, 'sValue1'));
     $this->assertEquals(FALSE, $this->redis->srem($key, 'sValue1'));
     $this->assertEquals(FALSE, $this->redis->sPop($key));
     $this->assertEquals(FALSE, $this->redis->sMove($key, $dkey . 'skey1', 'sValue1'));
     $this->assertEquals(FALSE, $this->redis->scard($key));
     $this->assertEquals(FALSE, $this->redis->sismember($key, 'sValue1'));
     $this->assertEquals(FALSE, $this->redis->sInter($key, $dkey . 'skey2'));
     $this->assertEquals(FALSE, $this->redis->sUnion($key, $dkey . 'skey4'));
     $this->assertEquals(FALSE, $this->redis->sDiff($key, $dkey . 'skey7'));
     $this->assertEquals(FALSE, $this->redis->sMembers($key));
     $this->assertEquals(FALSE, $this->redis->sRandMember($key));
     // sorted sets I/F
     $this->assertEquals(FALSE, $this->redis->zAdd($key, 1, 'zValue1'));
     $this->assertEquals(FALSE, $this->redis->zRem($key, 'zValue1'));
     $this->assertEquals(FALSE, $this->redis->zIncrBy($key, 1, 'zValue1'));
     $this->assertEquals(FALSE, $this->redis->zRank($key, 'zValue1'));
     $this->assertEquals(FALSE, $this->redis->zRevRank($key, 'zValue1'));
     $this->assertEquals(FALSE, $this->redis->zRange($key, 0, -1));
     $this->assertEquals(FALSE, $this->redis->zRevRange($key, 0, -1));
     $this->assertEquals(FALSE, $this->redis->zRangeByScore($key, 1, 2));
     $this->assertEquals(FALSE, $this->redis->zCount($key, 0, -1));
     $this->assertEquals(FALSE, $this->redis->zCard($key));
     $this->assertEquals(FALSE, $this->redis->zScore($key, 'zValue1'));
     $this->assertEquals(FALSE, $this->redis->zRemRangeByRank($key, 1, 2));
     $this->assertEquals(FALSE, $this->redis->zRemRangeByScore($key, 1, 2));
 }
Exemplo n.º 4
0
 /**
  * 返回队列中的第一个元素(从底部)
  * @param bool $bak 当bak为TRUE时,则会自动将队列数据备份至kbak
  * @return string $v
  */
 public function pop($bak = TRUE)
 {
     try {
         if ($bak) {
             return $this->rd->rpoplpush($this->k, $this->kbak);
         } else {
             return $this->rd->rPop($this->k);
         }
     } catch (\RedisException $e) {
         if ($this->reconnRedis()) {
             if ($bak) {
                 return $this->rd->rpoplpush($this->k, $this->kbak);
             } else {
                 return $this->rd->rPop($this->k);
             }
         }
     }
     return FALSE;
 }
Exemplo n.º 5
0
 public function testrPop()
 {
     //	rpush  => tail
     //	lpush => head
     $this->redis->delete('list');
     $this->redis->rPush('list', 'val');
     $this->redis->rPush('list', 'val2');
     $this->redis->lPush('list', 'val3');
     // 'list' = [ 'val3', 'val', 'val2']
     $this->assertEquals('val2', $this->redis->rPop('list'));
     $this->assertEquals('val', $this->redis->rPop('list'));
     $this->assertEquals('val3', $this->redis->rPop('list'));
     $this->assertEquals(FALSE, $this->redis->rPop('list'));
     // testing binary data
     $this->redis->delete('list');
     $this->assertEquals(1, $this->redis->rPush('list', gzcompress('val1')));
     $this->assertEquals(2, $this->redis->rPush('list', gzcompress('val2')));
     $this->assertEquals(3, $this->redis->rPush('list', gzcompress('val3')));
     $this->assertEquals('val3', gzuncompress($this->redis->rPop('list')));
     $this->assertEquals('val2', gzuncompress($this->redis->rPop('list')));
     $this->assertEquals('val1', gzuncompress($this->redis->rPop('list')));
 }
Exemplo n.º 6
0
Arquivo: Redis.php Projeto: BD-ES/Q
 /**
  * Remove and return the last element of the list.
  * 
  * @return Bdes\Q\Item the last element of the list
  */
 public function popLast()
 {
     return $this->redis->rPop($this->listName);
 }
Exemplo n.º 7
0
 /**
  * rpop a raw value
  *
  * @param	string	$key	Cache ID
  * @param	string	$value	value
  * @return	mixed	New value on success or FALSE on failure
  */
 public function rpop($key)
 {
     return $this->_redis->rPop($key);
 }
Exemplo n.º 8
0
 /**
  * 从Redis队列中读取一条任务
  *
  * @param string $queue Redis队列名
  *
  * @return string JSON格式字符串
  */
 public function dequeue($queue)
 {
     return $this->redis->rPop($queue);
 }
Exemplo n.º 9
0
 /**
  * 数据出队列
  * @param string $key KEY名称
  * @param bool $left 是否从左边开始出数据
  * @return array
  */
 public function pop($key, $left = true)
 {
     $val = $left ? parent::lPop($key) : parent::rPop($key);
     return json_decode($val, true);
 }
Exemplo n.º 10
0
 /**
  * 出队列
  * @param $list1 string 队列名
  * @param $deriction int 0:数据入队列头(左) 1:数据入队列尾(右) 默认为0
  * @param $list2 string 第二个队列名 默认null
  * @param $timeout int timeout为0:只获取list1队列的数据 
  *        timeout>0:如果队列list1为空 则等待timeout秒 如果还是未获取到数据 则对list2队列执行pop操作
  */
 public static function listPop($list1, $deriction = 0, $list2 = null, $timeout = 0)
 {
     $redis = new \Redis();
     $redis->connect(self::_HOST, self::_PORT);
     $return = null;
     switch ($direction) {
         case 0:
             if ($timeout && $list2) {
                 $return = $redis->blPop($list1, $list2, $timeout);
             } else {
                 $return = $redis->lPop($list1);
             }
             break;
         case 1:
             if ($timeout && $list2) {
                 $return = $redis->brPop($list1, $list2, $timeout);
             } else {
                 $return = $redis->rPop($list1);
             }
             break;
         default:
             $return = false;
             break;
     }
     $redis->close();
     $redis = null;
     return $return;
 }
Exemplo n.º 11
0
 /**
  * get value from the queue of channel
  * @param $channel
  * @return mixed
  */
 public function get($channel)
 {
     return $this->redis->rPop($channel);
 }
Exemplo n.º 12
0
 /**
  * 数据出队列(对应redis的list数据结构)
  * @param string $key KEY名称
  * @param bool $left 是否从左边开始出数据
  * @return mixed
  */
 public function pop($key)
 {
     $val = $this->redis->rPop($key);
     return json_decode($val);
 }
Exemplo n.º 13
0
            } else {
                continue;
            }
        }
        file_put_contents(__DIR__ . '/log/import_error_' . date('Ymd') . '.log', '[' . date('H:i:s') . ']' . "Process import sleep...\n", FILE_APPEND);
        sleep(1);
    }
}
if ('worker' === $_GET['step']) {
    // list num
    $list_id = $_GET['list'];
    // save worker pid
    $pidfile = __DIR__ . '/log/import_workers/LOG_LIST_' . $list_id . '.pid';
    file_put_contents($pidfile, posix_getpid());
    // redis cluster
    $redis_cluster = new Redis();
    if (!$redis_cluster->connect('192.168.4.105', 8002)) {
        throw new Exception("Ubhvrcache redis(192.168.4.105:8002) unavailable.");
    }
    // log redis
    $redis = new Redis();
    $redis->connect('127.0.0.1', 5379);
    // consump redis list
    $key = 'LOG_LIST_' . $list_id;
    while ($message = $redis->rPop($key)) {
        // parse message...
        // save cache...
    }
    // delete pid file when worker ends
    unlink($pidfile);
}
Exemplo n.º 14
0
 public function rpop($channel)
 {
     return $this->client->rPop($channel);
 }