Beispiel #1
0
 /**
  * returns sum of all values as flat array
  *
  * examples:
  *
  * {{{
  *	Stats::sum('requests');
  *	Stats::sum('requests', 'bucket1');
  *	Stats::sum('requests', array('bucket1', 'bucket2'));
  *	Stats::sum('requests', array('user' => 'foo'));
  *	Stats::sum('requests', array('user' => 'foo', 'year' => date('Y')));
  * }}}
  *
  * @see li3_redis\storage\Stats::get()
  * @see li3_redis\storage\Redis::getKey()
  * @param string $key redis key which identifies the hash
  * @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 integer|array sum of all values for given key
  * @filter
  */
 public static function sum($key, $buckets = 'global', array $options = array())
 {
     $defaults = array('prefix' => 'global', 'namespace' => static::$namespace);
     $options += $defaults;
     $params = compact('key', 'buckets', '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']] = array_sum(Redis::hashValues($key, $options));
         }
         return count($result) > 1 ? $result : array_shift($result);
     });
 }