예제 #1
0
 function testReadHash()
 {
     $scope = __FUNCTION__;
     Redis::config(array('format' => $scope));
     $this->assertTrue($this->redis->hMset("{$scope}:foo", array('name' => 'Joe', 'salary' => 2000)));
     $this->assertTrue($this->redis->hExists("{$scope}:foo", 'name'));
     $this->assertTrue($this->redis->hExists("{$scope}:foo", 'salary'));
     $expected = 'Joe';
     $this->assertEqual($expected, Redis::readHash('foo', 'name'));
     $expected = 'Joe';
     $this->assertEqual($expected, Redis::readHash('foo', array('name')));
     $expected = '2000';
     $this->assertEqual($expected, Redis::readHash('foo', 'salary'));
     $expected = array('name' => 'Joe', 'salary' => '2000');
     $this->assertEqual($expected, Redis::readHash('foo', array('name', 'salary')));
     $expected = array('name' => 'Joe', 'salary' => '2000', 'non-existent' => false);
     $this->assertEqual($expected, Redis::readHash('foo', array('name', 'salary', 'non-existent')));
     $expected = array('name' => 'Joe', 'salary' => '2000');
     $this->assertEqual($expected, Redis::readHash('foo'));
 }
예제 #2
0
 /**
  * Returns an array with all stats and their values in it
  *
  * If you want to retrieve stats for one specific bucket or dimension, you just pass in
  * the key to identify which stats to retrieve, i.e. `Stats::get('requests')`. If you omit
  * the bucket param at all, `global` will be used as a default.
  *
  * If you want to retrieve stats for requests but for a set of buckets, you can pass in
  * additional buckets as an array:
  *
  * {{{
  *	Stats::get('requests');
  *	Stats::get('requests', 'bucket1');
  *	Stats::get('requests', array('bucket1', 'bucket2'));
  *	Stats::get('requests', array('user' => 'foo'));
  *	Stats::get('requests', array('user' => 'foo', 'year' => date('Y')));
  * }}}
  *
  * Additionally, you can limit the result to certain fields, to limit the size of arrays, by
  * passing in a third parameter. That can be a string to retrieve only one field, or an array
  * of fields.
  *
  * If you pass in only one bucket, you will retrieve that stats with an array keyed off by
  * statname and the corresponding value. If you request more than one bucket at once, each
  * stats-array will be prefixed by the corresponding redis-prefix for that stat, e.g.
  * requesting stats for `user=foo` and `year=2013` you will get:
  *
  * {{{
  *    [global] => Array(
  *       [processed] => 1853
  *       [finished] => 253
  *    )
  *    [user:foo] => Array(
  *       [processed] => 53
  *       [finished] => 38
  *    )
  *    [year:2013] => Array(
  *       [processed] => 260
  *       [finished] => 111
  *   )
  * }}}
  *
  * @see li3_redis\storage\Stats::inc()
  * @see li3_redis\storage\Redis::getKey()
  * @param string $key redis key for which to retrieve stats for
  * @param string|array $buckets string or array with additional buckets, see Stats::inc()
  * @param string|array $fields a string or an array of fields to retrieve
  * @param array $options array with additional options, see Redis::getKey()
  * @return array stats name is key and value is the corresponding value
  * @filter
  */
 public static function get($key, $buckets = 'global', $fields = '', array $options = array())
 {
     $defaults = array('namespace' => static::$namespace);
     $options += $defaults;
     $params = compact('key', 'buckets', 'fields', 'options');
     return static::_filter(__METHOD__, $params, function ($self, $params) {
         extract($params);
         $result = array();
         $buckets = !is_array($buckets) ? array($buckets) : $buckets;
         foreach ($buckets as $prefix => $val) {
             $options['prefix'] = !is_numeric($prefix) ? Redis::addPrefix($val, $prefix) : $val;
             $result[$options['prefix']] = Redis::readHash($key, $fields, $options);
         }
         return count($result) > 1 ? $result : array_shift($result);
     });
 }