Exemplo n.º 1
0
 /**
  * 根据 category_id 获取顶级分类的信息
  * @param $categoriesId
  * @return array
  */
 public function getParentById($categoriesId)
 {
     if ($this->redis->hLen($this->redisParentChildKey) == 0) {
         $this->setRedisCategoryParentChild();
     }
     $parentId = $this->redis->hGet($this->redisParentChildKey, $categoriesId);
     return ['categories_id' => $parentId, 'categories_name' => $this->getCategoryNameById($parentId)];
 }
Exemplo n.º 2
0
 public function testHashes()
 {
     $this->redis->delete('h', 'key');
     $this->assertTrue(0 === $this->redis->hLen('h'));
     $this->assertTrue(TRUE === $this->redis->hSet('h', 'a', 'a-value'));
     $this->assertTrue(1 === $this->redis->hLen('h'));
     $this->assertTrue(TRUE === $this->redis->hSet('h', 'b', 'b-value'));
     $this->assertTrue(2 === $this->redis->hLen('h'));
     $this->assertTrue('a-value' === $this->redis->hGet('h', 'a'));
     // simple get
     $this->assertTrue('b-value' === $this->redis->hGet('h', 'b'));
     // simple get
     $this->assertTrue(FALSE === $this->redis->hSet('h', 'a', 'another-value'));
     // replacement
     $this->assertTrue('another-value' === $this->redis->hGet('h', 'a'));
     // get the new value
     $this->assertTrue('b-value' === $this->redis->hGet('h', 'b'));
     // simple get
     $this->assertTrue(FALSE === $this->redis->hGet('h', 'c'));
     // unknown hash member
     $this->assertTrue(FALSE === $this->redis->hGet('key', 'c'));
     // unknownkey
     // hDel
     $this->assertTrue(TRUE === $this->redis->hDel('h', 'a'));
     // TRUE on success
     $this->assertTrue(FALSE === $this->redis->hDel('h', 'a'));
     // FALSE on failure
     $this->redis->delete('h');
     $this->redis->hSet('h', 'x', 'a');
     $this->redis->hSet('h', 'y', 'b');
     // keys
     $keys = $this->redis->hKeys('h');
     $this->assertTrue($keys === array('x', 'y') || $keys === array('y', 'x'));
     // values
     $values = $this->redis->hVals('h');
     $this->assertTrue($values === array('a', 'b') || $values === array('b', 'a'));
     // keys + values
     $all = $this->redis->hGetAll('h');
     $this->assertTrue($all === array('x' => 'a', 'y' => 'b') || $all === array('y' => 'b', 'x' => 'a'));
     // hExists
     $this->assertTrue(TRUE === $this->redis->hExists('h', 'x'));
     $this->assertTrue(TRUE === $this->redis->hExists('h', 'y'));
     $this->assertTrue(FALSE === $this->redis->hExists('h', 'w'));
     $this->redis->delete('h');
     $this->assertTrue(FALSE === $this->redis->hExists('h', 'x'));
     // hIncrBy
     /*
     $this->redis->delete('h');
     $this->assertTrue(2.5 === $this->redis->hIncrBy('h', 2.5, 'x'));
     $this->assertTrue(3.5 === $this->redis->hIncrBy('h', 1, 'x'));
     
     $this->redis->hSet('h', 'y', 'not-a-number');
     $this->assertTrue(FALSE === $this->redis->hIncrBy('h', 1, 'y'));
     */
 }
Exemplo n.º 3
0
 /**
  * Execute the job.
  *
  * @return void
  */
 public function handle()
 {
     // 避免重复操作
     if ($this->attempts() > 3) {
         return;
     }
     $this->redis = PRedis::connection();
     if (is_object($this->sheet)) {
         $vote_dest = 'jk_upvote:' . $this->sheet->getAttributeValue('song_id') . ':userLIST';
         if (true == $this->redis->hGet($vote_dest, $this->user)) {
             return;
         }
         $this->redis->hSet($vote_dest, $this->user, 1);
         $vote_count = $this->redis->hLen($vote_dest);
         $stat = $this->sheet->storeUpVoteNumber($this->sheet->getAttributeValue('song_id'), $vote_count);
         if (!$stat) {
             $this->failed();
         }
     }
 }
Exemplo n.º 4
0
 public function testDifferentTypeSortedSet()
 {
     $key = '{hash}sortedset';
     $dkey = '{hash}' . __FUNCTION__;
     $this->redis->del($key);
     $this->assertEquals(1, $this->redis->zAdd($key, 0, '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));
     // hash I/F
     $this->assertEquals(FALSE, $this->redis->hSet($key, 'key1', 'value1'));
     $this->assertEquals(FALSE, $this->redis->hGet($key, 'key1'));
     $this->assertEquals(FALSE, $this->redis->hMGet($key, array('key1')));
     $this->assertEquals(FALSE, $this->redis->hMSet($key, array('key1' => 'value1')));
     $this->assertEquals(FALSE, $this->redis->hIncrBy($key, 'key2', 1));
     $this->assertEquals(FALSE, $this->redis->hExists($key, 'key2'));
     $this->assertEquals(FALSE, $this->redis->hDel($key, 'key2'));
     $this->assertEquals(FALSE, $this->redis->hLen($key));
     $this->assertEquals(FALSE, $this->redis->hKeys($key));
     $this->assertEquals(FALSE, $this->redis->hVals($key));
     $this->assertEquals(FALSE, $this->redis->hGetAll($key));
 }
Exemplo n.º 5
0
 public function testHashes()
 {
     $this->redis->delete('h', 'key');
     $this->assertTrue(0 === $this->redis->hLen('h'));
     $this->assertTrue(1 === $this->redis->hSet('h', 'a', 'a-value'));
     $this->assertTrue(1 === $this->redis->hLen('h'));
     $this->assertTrue(1 === $this->redis->hSet('h', 'b', 'b-value'));
     $this->assertTrue(2 === $this->redis->hLen('h'));
     $this->assertTrue('a-value' === $this->redis->hGet('h', 'a'));
     // simple get
     $this->assertTrue('b-value' === $this->redis->hGet('h', 'b'));
     // simple get
     $this->assertTrue(0 === $this->redis->hSet('h', 'a', 'another-value'));
     // replacement
     $this->assertTrue('another-value' === $this->redis->hGet('h', 'a'));
     // get the new value
     $this->assertTrue('b-value' === $this->redis->hGet('h', 'b'));
     // simple get
     $this->assertTrue(FALSE === $this->redis->hGet('h', 'c'));
     // unknown hash member
     $this->assertTrue(FALSE === $this->redis->hGet('key', 'c'));
     // unknownkey
     // hDel
     $this->assertTrue(1 === $this->redis->hDel('h', 'a'));
     // 1 on success
     $this->assertTrue(0 === $this->redis->hDel('h', 'a'));
     // 0 on failure
     $this->redis->delete('h');
     $this->redis->hSet('h', 'x', 'a');
     $this->redis->hSet('h', 'y', 'b');
     $this->assertTrue(2 === $this->redis->hDel('h', 'x', 'y'));
     // variadic
     // hsetnx
     $this->redis->delete('h');
     $this->assertTrue(TRUE === $this->redis->hSetNx('h', 'x', 'a'));
     $this->assertTrue(TRUE === $this->redis->hSetNx('h', 'y', 'b'));
     $this->assertTrue(FALSE === $this->redis->hSetNx('h', 'x', '?'));
     $this->assertTrue(FALSE === $this->redis->hSetNx('h', 'y', '?'));
     $this->assertTrue('a' === $this->redis->hGet('h', 'x'));
     $this->assertTrue('b' === $this->redis->hGet('h', 'y'));
     // keys
     $keys = $this->redis->hKeys('h');
     $this->assertTrue($keys === array('x', 'y') || $keys === array('y', 'x'));
     // values
     $values = $this->redis->hVals('h');
     $this->assertTrue($values === array('a', 'b') || $values === array('b', 'a'));
     // keys + values
     $all = $this->redis->hGetAll('h');
     $this->assertTrue($all === array('x' => 'a', 'y' => 'b') || $all === array('y' => 'b', 'x' => 'a'));
     // hExists
     $this->assertTrue(TRUE === $this->redis->hExists('h', 'x'));
     $this->assertTrue(TRUE === $this->redis->hExists('h', 'y'));
     $this->assertTrue(FALSE === $this->redis->hExists('h', 'w'));
     $this->redis->delete('h');
     $this->assertTrue(FALSE === $this->redis->hExists('h', 'x'));
     // hIncrBy
     $this->redis->delete('h');
     $this->assertTrue(2 === $this->redis->hIncrBy('h', 'x', 2));
     $this->assertTrue(3 === $this->redis->hIncrBy('h', 'x', 1));
     $this->assertTrue(2 === $this->redis->hIncrBy('h', 'x', -1));
     $this->assertTrue(FALSE === $this->redis->hIncrBy('h', 'x', "not-a-number"));
     $this->assertTrue("2" === $this->redis->hGet('h', 'x'));
     $this->redis->hSet('h', 'y', 'not-a-number');
     $this->assertTrue(FALSE === $this->redis->hIncrBy('h', 'y', 1));
     // hIncrByFloat
     $this->redis->delete('h');
     $this->assertTrue(1.5 === $this->redis->hIncrByFloat('h', 'x', 1.5));
     $this->assertTrue(3.0 === $this->redis->hincrByFloat('h', 'x', 1.5));
     $this->assertTrue(1.5 === $this->redis->hincrByFloat('h', 'x', -1.5));
     $this->redis->hset('h', 'y', 'not-a-number');
     $this->assertTrue(FALSE === $this->redis->hIncrByFloat('h', 'y', 1.5));
     // hmset
     $this->redis->delete('h');
     $this->assertTrue(TRUE === $this->redis->hMset('h', array('x' => 123, 'y' => 456, 'z' => 'abc')));
     $this->assertTrue('123' === $this->redis->hGet('h', 'x'));
     $this->assertTrue('456' === $this->redis->hGet('h', 'y'));
     $this->assertTrue('abc' === $this->redis->hGet('h', 'z'));
     $this->assertTrue(FALSE === $this->redis->hGet('h', 't'));
     // hmget
     $this->assertTrue(array('x' => '123', 'y' => '456') === $this->redis->hMget('h', array('x', 'y')));
     $this->assertTrue(array('z' => 'abc') === $this->redis->hMget('h', array('z')));
     $this->assertTrue(array('x' => '123', 't' => FALSE, 'y' => '456') === $this->redis->hMget('h', array('x', 't', 'y')));
     $this->assertFalse(array(123 => 'x') === $this->redis->hMget('h', array(123)));
     $this->assertTrue(array(123 => FALSE) === $this->redis->hMget('h', array(123)));
     // hmget/hmset with numeric fields
     $this->redis->del('h');
     $this->assertTrue(TRUE === $this->redis->hMset('h', array(123 => 'x', 'y' => 456)));
     $this->assertTrue('x' === $this->redis->hGet('h', 123));
     $this->assertTrue('x' === $this->redis->hGet('h', '123'));
     $this->assertTrue('456' === $this->redis->hGet('h', 'y'));
     $this->assertTrue(array(123 => 'x', 'y' => '456') === $this->redis->hMget('h', array('123', 'y')));
     // check non-string types.
     $this->redis->delete('h1');
     $this->assertTrue(TRUE === $this->redis->hMSet('h1', array('x' => 0, 'y' => array(), 'z' => new stdclass(), 't' => NULL)));
     $h1 = $this->redis->hGetAll('h1');
     $this->assertTrue('0' === $h1['x']);
     $this->assertTrue('Array' === $h1['y']);
     $this->assertTrue('Object' === $h1['z']);
     $this->assertTrue('' === $h1['t']);
 }
Exemplo n.º 6
0
 /**
  * Count cache items
  *
  * @param	string	Cache key
  * @return	bool
  */
 public function cache_count($key)
 {
     $value = $this->_redis->hLen($key);
     return $value;
 }
Exemplo n.º 7
0
 public function mbr()
 {
     $this->pData = checkData($_POST);
     $this->gData = checkData($_GET);
     $this->pData['orderField'] = $orderField = isset($this->pData['orderField']) ? $this->pData['orderField'] : 'tdate';
     $this->pData['orderDirection'] = $orderDirection = isset($this->pData['orderDirection']) ? $this->pData['orderDirection'] : 'desc';
     $this->orderField = $orderField;
     $this->orderDirection = $orderDirection;
     $s_date = $this->pData['s_date'];
     $e_date = isset($this->pData['e_date']) ? $this->pData['e_date'] : date("Y-m-d");
     $this->pData['s_date'] = $s_date;
     $this->pData['e_date'] = $e_date;
     $date = date("Y-m-d", time());
     $hash = "mbr_" . $date;
     $redis = new Redis();
     $redis->connect('192.168.0.15', 6379);
     $install_all = $redis->hLen("install");
     //总安装量
     //print_r($install_all);die;
     $data[] = $redis->hgetall("mbr");
     $value = array();
     foreach ($data as $k => $v) {
         foreach ($v as $key => $val) {
             $value[] = json_decode($val);
         }
     }
     if ($e_date) {
         for ($i = 0; $i < count($value); $i++) {
             if ($value[$i][3] <= $e_date) {
                 $values[] = $value[$i];
             }
         }
     } else {
         if ($s_date) {
             for ($i = 0; $i < count($value); $i++) {
                 if ($value[$i][3] >= $s_date) {
                     $values[] = $value[$i];
                 }
             }
         } else {
             if (isset($e_date) && isset($s_date)) {
                 for ($i = 0; $i < count($value); $i++) {
                     if ($value[$i][3] >= $s_date && $value[$i][3] <= $e_date) {
                         $values[] = $value[$i];
                     }
                 }
             } else {
                 for ($i = 0; $i < count($value); $i++) {
                     $values[] = $value[$i];
                 }
             }
         }
     }
     for ($i = 0; $i < count($values); $i++) {
         unset($values[$i][2]);
         $value_str[] = implode(",", $values[$i]);
     }
     $arr = array_unique($value_str);
     foreach ($arr as $k => $v) {
         $value_arr[] = $v;
     }
     for ($i = 0; $i < count($value_arr); $i++) {
         $value_a[] = explode(",", $value_arr[$i]);
     }
     //print_r($value_a);die;
     $this->s->assign('count', 5);
     $this->s->assign('install_all', $install_all);
     $this->s->assign('data', $value_a);
     $this->s->assign('total', count($value_a));
     $this->s->assign('search', $this->pData);
 }
Exemplo n.º 8
0
 public function testHashes()
 {
     $this->redis->delete('h', 'key');
     $this->assertTrue(0 === $this->redis->hLen('h'));
     $this->assertTrue(TRUE === $this->redis->hSet('h', 'a', 'a-value'));
     $this->assertTrue(1 === $this->redis->hLen('h'));
     $this->assertTrue(TRUE === $this->redis->hSet('h', 'b', 'b-value'));
     $this->assertTrue(2 === $this->redis->hLen('h'));
     $this->assertTrue('a-value' === $this->redis->hGet('h', 'a'));
     // simple get
     $this->assertTrue('b-value' === $this->redis->hGet('h', 'b'));
     // simple get
     $this->assertTrue(FALSE === $this->redis->hSet('h', 'a', 'another-value'));
     // replacement
     $this->assertTrue('another-value' === $this->redis->hGet('h', 'a'));
     // get the new value
     $this->assertTrue('b-value' === $this->redis->hGet('h', 'b'));
     // simple get
     $this->assertTrue(FALSE === $this->redis->hGet('h', 'c'));
     // unknown hash member
     $this->assertTrue(FALSE === $this->redis->hGet('key', 'c'));
     // unknownkey
     // hDel
     $this->assertTrue(TRUE === $this->redis->hDel('h', 'a'));
     // TRUE on success
     $this->assertTrue(FALSE === $this->redis->hDel('h', 'a'));
     // FALSE on failure
     $this->redis->delete('h');
     $this->redis->hSet('h', 'x', 'a');
     $this->redis->hSet('h', 'y', 'b');
     // keys
     $keys = $this->redis->hKeys('h');
     $this->assertTrue($keys === array('x', 'y') || $keys === array('y', 'x'));
     // values
     $values = $this->redis->hVals('h');
     $this->assertTrue($values === array('a', 'b') || $values === array('b', 'a'));
     // keys + values
     $all = $this->redis->hGetAll('h');
     $this->assertTrue($all === array('x' => 'a', 'y' => 'b') || $all === array('y' => 'b', 'x' => 'a'));
     // hExists
     $this->assertTrue(TRUE === $this->redis->hExists('h', 'x'));
     $this->assertTrue(TRUE === $this->redis->hExists('h', 'y'));
     $this->assertTrue(FALSE === $this->redis->hExists('h', 'w'));
     $this->redis->delete('h');
     $this->assertTrue(FALSE === $this->redis->hExists('h', 'x'));
     // hIncrBy
     $this->redis->delete('h');
     $this->assertTrue(25 === $this->redis->hIncrBy('h', 'x', 25));
     $this->assertTrue(35 === $this->redis->hIncrBy('h', 'x', 10));
     $this->redis->hSet('h', 'y', 'not-a-number');
     $this->assertTrue(FALSE === $this->redis->hIncrBy('h', 'y', 1));
     $this->redis->delete('h1');
     //hMGet, hMSet
     $this->redis->hset("h1", "field1", "value1");
     $this->redis->hset("h1", "field2", "value2");
     $this->assertTrue(array('field1' => 'value1', 'field2' => 'value2') === $this->redis->hGetAll('h1'));
     $this->assertTrue(array('field1' => 'value1', 'field2' => 'value2') === $this->redis->hMGet('h1', array('field1', 'field2')));
     $this->assertTrue(array('field1' => 'value1') === $this->redis->hMGet('h1', array('field1')));
     $this->assertTrue(FALSE === $this->redis->hMGet('h1', array()));
     $this->assertTrue(TRUE === $this->redis->hMSet('h1', array('field3' => 'value3')));
     $this->assertTrue(array('field1' => 'value1', 'field2' => 'value2', 'field3' => 'value3') === $this->redis->hGetAll('h1'));
     $this->assertTrue(TRUE === $this->redis->hMSet('h1', array('field3' => 'value4')));
     $this->assertTrue(array('field1' => 'value1', 'field2' => 'value2', 'field3' => 'value4') === $this->redis->hGetAll('h1'));
     $this->assertTrue(TRUE === $this->redis->hMSet('h1', array('x' => 0, 'y' => array(), 'z' => new stdclass())));
     $h1 = $this->redis->hGetAll('h1');
     $this->assertTrue('0' === $h1['x']);
     $this->assertTrue('Array' === $h1['y']);
     $this->assertTrue('Object' === $h1['z']);
 }
Exemplo n.º 9
0
 /**
  * 获取hash表中元素个数
  * @param $hash string 哈希表名
  */
 public static function hashLen($hash)
 {
     $redis = new \Redis();
     $redis->connect(self::_HOST, self::_PORT);
     $return = null;
     $return = $redis->hLen($hash);
     $redis->close();
     $redis = null;
     return $return;
 }