예제 #1
0
 /**
  * 取得数据,支持批量取
  *
  * @param string/array $key
  * @return mixed
  */
 public function get($key)
 {
     $this->_connect();
     $time = microtime(1);
     if (is_array($key)) {
         # redis多取
         if ($this->prefix) {
             foreach ($key as &$k) {
                 $k = $this->prefix . $k;
             }
         }
         $return = $this->_redis->mget($key);
         foreach ($return as &$item) {
             $this->_de_format_data($item);
         }
     } else {
         $return = $this->_redis->get($key);
         $this->_de_format_data($return);
     }
     $time = microtime(1) - $time;
     if (false === $return) {
         Core::debug()->warn($key, 'cache redis mis key');
         Core::debug()->info($time, 'use time');
         return false;
     } else {
         Core::debug()->info($key, 'cache redis hit key');
         Core::debug()->info($time, 'use time');
     }
     return $return;
 }
예제 #2
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));
 }
예제 #3
0
 /**
  * {@inheritdoc}
  */
 public function mget(array $keys)
 {
     $result = array();
     $keys = array_values($keys);
     $values = $this->redis->mget($keys);
     foreach ($values as $i => $value) {
         if ($value !== false) {
             $result[$keys[$i]] = $value;
         }
     }
     return $result;
 }
예제 #4
0
파일: TestRedis.php 프로젝트: 0/phpredis
 public function testMsetNX()
 {
     $this->redis->delete('x', 'y', 'z');
     // remove x y z
     $this->assertTrue(TRUE === $this->redis->msetnx(array('x' => 'a', 'y' => 'b', 'z' => 'c')));
     // set x y z
     $this->assertEquals($this->redis->mget(array('x', 'y', 'z')), array('a', 'b', 'c'));
     // check x y z
     $this->redis->delete('x');
     // delete just x
     $this->assertTrue(FALSE === $this->redis->msetnx(array('x' => 'A', 'y' => 'B', 'z' => 'C')));
     // set x y z
     $this->assertEquals($this->redis->mget(array('x', 'y', 'z')), array(FALSE, 'b', 'c'));
     // check x y z
     $this->assertFalse($this->redis->msetnx(array()));
     // set ø → FALSE
 }
예제 #5
0
 /**
  * @param string $keys
  *
  * @return bool
  */
 public function untag($keys)
 {
     if (!is_array($keys)) {
         $keys = array($keys);
     }
     $nsTagsKeys = $this->applyNamespace($keys, 'tags');
     $serializedTagsList = $this->client->mget($nsTagsKeys);
     $tagsList = array_map(array($this, 'deserialize'), $serializedTagsList);
     $tags = call_user_func_array('array_merge', $tagsList);
     $tags = array_unique($tags);
     $nsTags = $this->applyNamespace($tags, 'tag');
     foreach ($nsTags as $nsTag) {
         $this->client->sRem($nsTag, $keys);
         call_user_func_array(array($this->client, 'sRem'), array_merge(array($nsTag), $keys));
     }
     return $this->client->del($nsTagsKeys);
 }
예제 #6
0
 public function testMset()
 {
     $this->redis->delete('x', 'y', 'z');
     // remove x y z
     $this->assertTrue($this->redis->mset(array('x' => 'a', 'y' => 'b', 'z' => 'c')));
     // set x y z
     $this->assertEquals($this->redis->mget(array('x', 'y', 'z')), array('a', 'b', 'c'));
     // check x y z
     $this->redis->delete('x');
     // delete just x
     $this->assertTrue($this->redis->mset(array('x' => 'a', 'y' => 'b', 'z' => 'c')));
     // set x y z
     $this->assertEquals($this->redis->mget(array('x', 'y', 'z')), array('a', 'b', 'c'));
     // check x y z
     $this->assertFalse($this->redis->mset(array()));
     // set ø → FALSE
 }
 public function index()
 {
     \Redis::set('name', 'alegriaghost');
     $name = \Redis::get('name');
     \Redis::del('name');
     \Debugbar::info($name);
     \Debugbar::warning($name);
     \Debugbar::error($name);
     \Debugbar::addMessage($name, '$name');
     var_dump($name);
     \Redis::set('name1', 'alegriaghost1');
     \Redis::set('name2', 'alegriaghost2');
     \Redis::set('name3', 'alegriaghost3');
     $list = Redis::keys('*');
     $values = Redis::mget($list);
     var_dump($list);
     var_dump($values);
 }
예제 #8
0
 /**
  * 取得数据,支持批量取
  *
  * @param string/array $key
  * @return mixed
  */
 public function get($key)
 {
     $this->_connect();
     if (is_array($key)) {
         # redis多取
         $return = $this->_redis->mget($key);
         foreach ($return as &$item) {
             Cache_Driver_Redis::_de_format_data($item);
         }
     } else {
         $return = $this->_redis->get($key);
         Cache_Driver_Redis::_de_format_data($return);
     }
     if (false === $return) {
         Core::debug()->error($key, 'redis mis key');
         return false;
     } else {
         Core::debug()->info($key, 'redis hit key');
     }
     return $return;
 }
예제 #9
0
 /**
  * mGet cache
  *
  * @param	array	Cache ID Array
  * @return	mixed
  */
 public function mget($keys)
 {
     return $this->_redis->mget($keys);
 }
예제 #10
0
 public function test4Action()
 {
     $redis = new Redis();
     $redis->connect("127.0.0.1", "6379");
     //php客户端设置的ip及端口
     //存储一个 值
     $redis->set("say", "Hello World");
     echo $redis->get("say");
     //应输出Hello World
     //存储多个值
     $array = array('first_key' => 'first_val', 'second_key' => 'second_val', 'third_key' => 'third_val');
     $array_get = array('first_key', 'second_key', 'third_key');
     $redis->mset($array);
     var_dump($redis->mget($array_get));
     die;
 }