/** * decreases 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::dec('api', 'calls'); * Stats::dec('api', array('calls' => 1, 'successful' => 1)); * Stats::dec('api', array('calls' => 1, 'successful' => 1), array('user' => 'foo')); * Stats::dec('api', array('calls' => 1), array('user' => 'foo', 'year' => date('Y'))); * Stats::dec('api', array('calls' => 1), 'prefix'); * Stats::dec('api', array('calls' => 1), array('prefix')); * Stats::dec('api', array('calls' => 1), array('prefix1', 'prefix2')); * Stats::dec('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 decrease, the format is * array($name => $value) and can contain an unlimited number of stats to decrease. * 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 dec($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::decrementHash($key, $values, $options); } return count($result) > 1 ? $result : array_shift($result); }); }