function testAddMember() { $scope = __FUNCTION__; Redis::config(array('format' => $scope)); $leaderboard = new Leaderboard('leaderboard'); $leaderboard->removeMember('david'); $this->assertEqual(1, $leaderboard->addMember('david', 69)); $this->assertEqual(1, $this->redis->zSize($scope . ':' . $this->prefix . 'leaderboard')); }
function testSum() { $scope = __FUNCTION__; Redis::config(array('format' => $scope)); $data1 = array('calls' => 140, 'fails' => 22); $data2 = array('calls' => 35, 'fails' => 3); $data3 = array('calls' => 17, 'fails' => 29); $this->assertTrue($this->redis->hMset("{$scope}:stats:global:foo", $data1)); $this->assertTrue($this->redis->hMset("{$scope}:stats:prefix1:foo", $data2)); $this->assertTrue($this->redis->hMset("{$scope}:stats:prefix2:foo", $data3)); $this->assertEqual($data1, $this->redis->hGetAll("{$scope}:stats:global:foo")); $this->assertEqual($data2, $this->redis->hGetAll("{$scope}:stats:prefix1:foo")); $this->assertEqual($data3, $this->redis->hGetAll("{$scope}:stats:prefix2:foo")); // simplest call $this->assertEqual(array_sum(array_values($data1)), Stats::sum('foo')); // with one prefix $this->assertEqual(array_sum(array_values($data2)), Stats::sum('foo', 'prefix1')); // with one prefix as array $this->assertEqual(array_sum(array_values($data3)), Stats::sum('foo', array('prefix2'))); // with two prefixes as flat array $expected = array('global' => array_sum(array_values($data1)), 'prefix2' => array_sum(array_values($data3))); $this->assertEqual($expected, Stats::sum('foo', array('global', 'prefix2'))); }
function testGetKeyArray() { $expected = array('app:foo', 'app:bar'); $this->assertEqual($expected, Redis::getKey(array('foo', 'bar'), array('format' => 'app'))); }
public function getKey($name = null) { $name = $name ?: $this->name; $namespace = $this->getNamespace(); return Redis::getKey($name, compact('namespace')); }
function testCount() { $scope = __FUNCTION__; Redis::config(array('format' => $scope)); $this->redis->rPush("{$scope}:lists:foo", 'bar'); $this->redis->rPush("{$scope}:lists:foo", 'baz'); $this->assertEqual(2, Lists::count('foo')); $this->redis->rPush("{$scope}:lists:foo", 'bam'); $this->assertEqual(3, Lists::count('foo')); $this->assertEqual(3, Lists::size('foo')); $this->assertEqual(3, Lists::length('foo')); }
/** * get amount of items within a list from redis * * {{{ * Lists::count('key'); * Lists::count('key', array('user' => 'foo', 'global')); // for multiple buckets at once * }}} * * @see li3_redis\storage\Redis::listLength() * @see li3_redis\storage\Redis::getKey() * @param string $key The key to uniquely identify the item in redis * @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|boolean the number of values in that list, false if key does not exist * @filter */ public static function count($key, $buckets = null, array $options = array()) { $defaults = array('namespace' => static::$namespace); $options += $defaults; $params = compact('key', 'buckets', 'options'); return static::_filter(__METHOD__, $params, function ($self, $params) { extract($params); if (is_null($buckets)) { return Redis::listLength($key, $options); } $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::listLength($key, $options); } return count($result) > 1 ? $result : array_shift($result); }); }
<?php use lithium\action\Dispatcher; use lithium\console\Dispatcher as ConsoleDispatcher; use li3_redis\storage\Redis; /** * Apply filter to Dispatcher, to initialize Redis configuration */ Dispatcher::applyFilter('_call', function ($self, $params, $chain) { Redis::config(); return $chain->next($self, $params, $chain); }); ConsoleDispatcher::applyFilter('_call', function ($self, $params, $chain) { Redis::config(); return $chain->next($self, $params, $chain); });