Exemple #1
0
 function testIncrementHash()
 {
     $scope = __FUNCTION__;
     Redis::config(array('format' => $scope));
     $expected = array('bar' => '1', 'baz' => '2');
     $this->assertEqual($expected, Redis::incrementHash('foo', array('bar' => 1, 'baz' => 2)));
     $this->assertEqual($expected, $this->redis->hGetAll("{$scope}:foo"));
     $expected = array('bar' => 1, 'baz' => 2, 'uncountable' => 'foo');
     $this->assertEqual($expected, Redis::incrementHash('withString', array('bar' => 1, 'baz' => 2, 'uncountable' => 'foo')));
     $this->assertEqual($expected, $this->redis->hGetAll("{$scope}:withString"));
     $expected = array('bar' => 2, 'baz' => 4, 'uncountable' => 'foo');
     $this->assertEqual($expected, Redis::incrementHash('withString', array('bar' => 1, 'baz' => 2, 'uncountable' => 'foo')));
     $this->assertEqual($expected, $this->redis->hGetAll("{$scope}:withString"));
     $this->assertEqual(1, Redis::incrementHash('onlyString', 'count'));
     $this->assertEqual(array('count' => 1), $this->redis->hGetAll("{$scope}:onlyString"));
     $this->assertEqual(2, Redis::incrementHash('onlyString', 'count'));
     $this->assertEqual(array('count' => 2), $this->redis->hGetAll("{$scope}:onlyString"));
 }
Exemple #2
0
 /**
  * increases the amount of given value(s)
  *
  * The usage is for bucketed stats, e.g. you want to count how many api-requests are made,
  * you can count that gor a global bucket (default) and with the same call you can count
  * api-calls for a specific range, e.g. user, year or resource.
  *
  * examples:
  *
  * {{{
  *	Stats::inc('api', 'calls');
  *	Stats::inc('api', array('calls' => 1, 'successful' => 1));
  *	Stats::inc('api', array('calls' => 1, 'successful' => 1), array('user' => 'foo'));
  *	Stats::inc('api', array('calls' => 1), array('user' => 'foo', 'year' => date('Y')));
  *	Stats::inc('api', array('calls' => 1), 'prefix');
  *	Stats::inc('api', array('calls' => 1), array('prefix'));
  *	Stats::inc('api', array('calls' => 1), array('prefix1', 'prefix2'));
  *	Stats::inc('api', array('calls' => 1), array('prefix1'), array('namespace' => 'foo'));
  * }}}
  *
  * Please note, if you pass in buckets, the global bucket will always be incremented as well.
  *
  * @see li3_redis\storage\Redis::getKey()
  * @param string $key redis key in which to store the hash
  * @param string|array $values an array of values to increase, the format is
  *        array($name => $value) and can contain an unlimited number of stats to increase.
  *        Can also be a plain string - in that case, we assume you want to increment by 1.
  * @param string|array $buckets an array of additional prefixes, can be a numerical indexed
  *        array with strings, or an associated array, in which the key and value will be glued
  *        together by a separater or just a string, for one additional prefix.
  * @param array $options array with additional options, see Redis::getKey()
  * @return mixed the value that has been incremented or an array of values
  * @filter
  */
 public static function inc($key, $values, $buckets = 'global', array $options = array())
 {
     $defaults = array('namespace' => static::$namespace);
     $options += $defaults;
     $params = compact('key', 'values', 'buckets', 'options');
     return static::_filter(__METHOD__, $params, function ($self, $params) {
         extract($params);
         $result = array();
         $values = is_string($values) ? array($values => 1) : $values;
         $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::incrementHash($key, $values, $options);
         }
         return count($result) > 1 ? $result : array_shift($result);
     });
 }