public function it_can_smembers() { $this->redis->sMembers('test')->shouldBeCalled()->willReturn(['test']); $this->sMembers('test')->shouldReturn(['test']); $this->redis->sMembers('test')->willThrow(new \RedisException()); $this->shouldThrow(DriverException::class)->during('sMembers', ['test']); }
/** * Class constructor * * Setup Redis * * Loads Redis config file if present. Will halt execution * if a Redis connection can't be established. * * @return void * @see Redis::connect() */ public function __construct() { $config = array(); $CI =& get_instance(); if ($CI->config->load('redis', TRUE, TRUE)) { $config = $CI->config->item('redis'); } $config = array_merge(self::$_default_config, $config); $this->_redis = new Redis(); try { if ($config['socket_type'] === 'unix') { $success = $this->_redis->connect($config['socket']); } else { $success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']); } if (!$success) { log_message('error', 'Cache: Redis connection failed. Check your configuration.'); } if (isset($config['password']) && !$this->_redis->auth($config['password'])) { log_message('error', 'Cache: Redis authentication failed.'); } } catch (RedisException $e) { log_message('error', 'Cache: Redis connection refused (' . $e->getMessage() . ')'); } // Initialize the index of serialized values. $serialized = $this->_redis->sMembers('_ci_redis_serialized'); empty($serialized) or $this->_serialized = array_flip($serialized); }
/** * Setup Redis config and connection * * Loads Redis config file if present. Will halt execution * if a Redis connection can't be established. * * @return bool * @see Redis::connect() */ protected function _setup_redis() { $config = array(); $CI =& get_instance(); if ($CI->config->load('redis', TRUE, TRUE)) { $config += $CI->config->item('redis'); } $config = array_merge(self::$_default_config, $config); $this->_redis = new Redis(); try { if ($config['socket_type'] === 'unix') { $success = $this->_redis->connect($config['socket']); } else { $success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']); } if (!$success) { log_message('debug', 'Cache: Redis connection refused. Check the config.'); return FALSE; } } catch (RedisException $e) { log_message('debug', 'Cache: Redis connection refused (' . $e->getMessage() . ')'); return FALSE; } if (isset($config['password'])) { $this->_redis->auth($config['password']); } // Initialize the index of serialized values. $serialized = $this->_redis->sMembers('_ci_redis_serialized'); if (!empty($serialized)) { $this->_serialized = array_flip($serialized); } return TRUE; }
public function testDifferentTypeHash() { $key = '{hash}hash'; $dkey = '{hash}hash'; $this->redis->del($key); $this->assertEquals(1, $this->redis->hSet($key, 'key', 'value')); // string I/F $this->assertEquals(FALSE, $this->redis->get($key)); $this->assertEquals(FALSE, $this->redis->getset($key, 'value2')); $this->assertEquals(FALSE, $this->redis->append($key, 'append')); $this->assertEquals(FALSE, $this->redis->getRange($key, 0, 8)); $this->assertEquals(array(FALSE), $this->redis->mget(array($key))); $this->assertEquals(FALSE, $this->redis->incr($key)); $this->assertEquals(FALSE, $this->redis->incrBy($key, 1)); $this->assertEquals(FALSE, $this->redis->decr($key)); $this->assertEquals(FALSE, $this->redis->decrBy($key, 1)); // lists I/F $this->assertEquals(FALSE, $this->redis->rPush($key, 'lvalue')); $this->assertEquals(FALSE, $this->redis->lPush($key, 'lvalue')); $this->assertEquals(FALSE, $this->redis->lLen($key)); $this->assertEquals(FALSE, $this->redis->lPop($key)); $this->assertEquals(FALSE, $this->redis->lrange($key, 0, -1)); $this->assertEquals(FALSE, $this->redis->lTrim($key, 0, 1)); $this->assertEquals(FALSE, $this->redis->lGet($key, 0)); $this->assertEquals(FALSE, $this->redis->lSet($key, 0, "newValue")); $this->assertEquals(FALSE, $this->redis->lrem($key, 'lvalue', 1)); $this->assertEquals(FALSE, $this->redis->lPop($key)); $this->assertEquals(FALSE, $this->redis->rPop($key)); $this->assertEquals(FALSE, $this->redis->rPoplPush($key, $dkey . 'lkey1')); // sets I/F $this->assertEquals(FALSE, $this->redis->sAdd($key, 'sValue1')); $this->assertEquals(FALSE, $this->redis->srem($key, 'sValue1')); $this->assertEquals(FALSE, $this->redis->sPop($key)); $this->assertEquals(FALSE, $this->redis->sMove($key, $dkey . 'skey1', 'sValue1')); $this->assertEquals(FALSE, $this->redis->scard($key)); $this->assertEquals(FALSE, $this->redis->sismember($key, 'sValue1')); $this->assertEquals(FALSE, $this->redis->sInter($key, $dkey . 'skey2')); $this->assertEquals(FALSE, $this->redis->sUnion($key, $dkey . 'skey4')); $this->assertEquals(FALSE, $this->redis->sDiff($key, $dkey . 'skey7')); $this->assertEquals(FALSE, $this->redis->sMembers($key)); $this->assertEquals(FALSE, $this->redis->sRandMember($key)); // sorted sets I/F $this->assertEquals(FALSE, $this->redis->zAdd($key, 1, 'zValue1')); $this->assertEquals(FALSE, $this->redis->zRem($key, 'zValue1')); $this->assertEquals(FALSE, $this->redis->zIncrBy($key, 1, 'zValue1')); $this->assertEquals(FALSE, $this->redis->zRank($key, 'zValue1')); $this->assertEquals(FALSE, $this->redis->zRevRank($key, 'zValue1')); $this->assertEquals(FALSE, $this->redis->zRange($key, 0, -1)); $this->assertEquals(FALSE, $this->redis->zRevRange($key, 0, -1)); $this->assertEquals(FALSE, $this->redis->zRangeByScore($key, 1, 2)); $this->assertEquals(FALSE, $this->redis->zCount($key, 0, -1)); $this->assertEquals(FALSE, $this->redis->zCard($key)); $this->assertEquals(FALSE, $this->redis->zScore($key, 'zValue1')); $this->assertEquals(FALSE, $this->redis->zRemRangeByRank($key, 1, 2)); $this->assertEquals(FALSE, $this->redis->zRemRangeByScore($key, 1, 2)); }
public function testsGetMembers() { $this->redis->delete('set'); $this->redis->sAdd('set', 'val'); $this->redis->sAdd('set', 'val2'); $this->redis->sAdd('set', 'val3'); $array = array('val', 'val2', 'val3'); $this->assertEquals($array, $this->redis->sGetMembers('set')); $this->assertEquals($array, $this->redis->sMembers('set')); // test alias }
/** * @test Implementation */ public function collectGarbageRemovesExpiredIdentifierFromTagsToIdentifierSet() { $this->setUpBackend(); $this->setUpRedis(); $identifier = 'identifier' . uniqid(); $this->backend->set($identifier . 'A', 'data', array('tag1', 'tag2')); $this->backend->set($identifier . 'B', 'data', array('tag2')); $this->redis->delete('identData:' . $identifier . 'A'); $this->backend->collectGarbage(); $expectedResult = array(array(), array($identifier . 'B')); $actualResult = array($this->redis->sMembers('tagIdents:tag1'), $this->redis->sMembers('tagIdents:tag2')); $this->assertSame($expectedResult, $actualResult); }
/** * Resets the Temporal object to its initial value * * @return int Returns the initial value */ public function reset() { Assertion::notNull(self::$redis, "Redis connection hasn't been set"); // Remove each individual key $items = self::$redis->sMembers($this->identifier); foreach ($items as $i) { self::$redis->delete($i); } // Remove the set and check its emptiness self::$redis->delete($this->identifier); Assertion::count(self::$redis->sMembers($this->identifier), 0); return $this->initialNumber; }
/** * With the current internal structure, only the identifier to data entries * have a redis internal lifetime. If an entry expires, attached * identifier to tags and tag to identifiers entries will be left over. * This methods finds those entries and cleans them up. * * Scales O(n*m) with number of cache entries (n) and number of tags (m) * * @return void * @api */ public function collectGarbage() { $identifierToTagsKeys = $this->redis->getKeys(self::IDENTIFIER_TAGS_PREFIX . '*'); foreach ($identifierToTagsKeys as $identifierToTagsKey) { list(, $identifier) = explode(':', $identifierToTagsKey); // Check if the data entry still exists if (!$this->redis->exists(self::IDENTIFIER_DATA_PREFIX . $identifier)) { $tagsToRemoveIdentifierFrom = $this->redis->sMembers($identifierToTagsKey); $queue = $this->redis->multi(\Redis::PIPELINE); $queue->delete($identifierToTagsKey); foreach ($tagsToRemoveIdentifierFrom as $tag) { $queue->sRemove(self::TAG_IDENTIFIERS_PREFIX . $tag, $identifier); } $queue->exec(); } } }
/** * Finds and returns all cache entry identifiers which are tagged by the * specified tag. * * @param string $tag The tag to search for * @return array An array with identifiers of all matching entries. An empty array if no entries matched * @api */ public function findIdentifiersByTag($tag) { return $this->redis->sMembers($this->buildKey('tag:' . $tag)); }
/** * @param $siteUrl * @return array */ private function getSitesKeys($siteUrl) { return $this->redis->sMembers($this->getSiteKey($siteUrl)); }
if ($i % 10000 == 0) { echo '-'; } $i++; } $result->free(); $pipeline->exec(); } } $db->close(); echo $i, PHP_EOL; $te = microtime(true); $cost = $te - $tb; echo 'import data cost: ' . $cost, PHP_EOL; //$len = $redis->sCard('user_set'); $userset = $redis->sMembers('user_set'); $len = count($userset); echo 'user count: ' . $len, PHP_EOL; //generate 1-dim ran $i = 0; $tb = microtime(true); foreach ($userset as $uid) { $pipeline->sCard($uid); if ($i % 10000 == 0) { echo '*'; } $i++; } $tempArray = $pipeline->exec(); $i = 0; foreach ($userset as $index => $uid) {
function generate_3dim_rank($worker) { $redis = new Redis(); //$redis->connect('/tmp/redis.sock'); $redis->connect($this->remote_redis_ip); $local_redis = new Redis(); $local_redis->connect($this->local_redis_ip, $this->remote_redis_port + $worker->id + 1); /* $userlist = $redis->sMembers('user_set'); $len = count($userlist); //$len = $redis->sCard('user_set'); $num = ceil((float)$len/$this->worker_num); $user_list = array_slice($userlist, $worker->id*$num, $num); */ $user_list = $local_redis->sMembers('user_set'); $num = count($user_list); $step = 50000; for ($j = 0; $j * $step < $num; $j++) { $userlist = array_slice($user_list, $j * $step, $step); $local_pipeline = $local_redis->pipeline(); $i = 0; foreach ($userlist as $uid) { $local_pipeline->sMembers($uid . '-2dim'); if ($i % 10000 == 0) { echo '*'; } $i++; } $tempArray = $local_pipeline->exec(); $local_pipeline = $local_redis->pipeline(); $i = 0; foreach ($userlist as $index => $uid) { $par = implode('","', $tempArray[$index]); $str = '$local_pipeline->sUnionStore("' . $uid . '-3dim", "' . $par . '");'; eval($str); $local_pipeline->sDiffStore($uid . '-3dim', $uid . '-3dim', $uid . '-2dim', $uid); $local_pipeline->sRem($uid . '-3dim', $uid); if ($i % 10000 == 0) { echo '*'; } $i++; } $local_pipeline->exec(); $local_pipeline = $local_redis->pipeline(); $i = 0; foreach ($userlist as $index => $uid) { $local_pipeline->sCard($uid . '-3dim'); if ($i % 10000 == 0) { echo '*'; } $i++; } $tempArray = $local_pipeline->exec(); $pipeline = $redis->pipeline(); $local_pipeline = $local_redis->pipeline(); $i = 0; foreach ($userlist as $index => $uid) { $pipeline->zAdd('rank-3', $tempArray[$index], $uid); //free memory $local_pipeline->del($uid . '-3dim'); if ($i % 10000 == 0) { echo '.'; } $i++; } $pipeline->exec(); $local_pipeline->exec(); } $local_redis->close(); $redis->close(); }
/** * @inheritdoc */ public function types($bucket) { return (array) $this->redis->sMembers('types:' . $bucket); }
/** * 返回set中所有元素 * @param $set string 集合名 */ public static function setMembers($set) { $redis = new \Redis(); $redis->connect(self::_HOST, self::_PORT); $return = null; $return = $redis->sMembers($set); $redis->close(); $redis = null; return $return; }
public static function getBlacklist() { return self::$redis->sMembers(self::$prefix . self::BLACKLIST_KEY); }