/** * Lazy connection * * Get the connection to Redis server and return it * * @throws \Predis\Connection\ConnectionException */ public function getRedisClient() { if (empty($this->redisHandler)) { $this->redisHandler = new RedisHandler(); } return $this->redisHandler->getConnection(); }
/** * Lazy connection * * Get the connection to Redis server and return it * * @throws \Predis\Connection\ConnectionException */ public function getRedisClient() { $resource = null; if ($this->redisHandler != null) { $reflectorClass = new ReflectionClass($this->redisHandler->getConnection()); $reflectorProperty = $reflectorClass->getParentClass()->getProperty('resource'); $reflectorProperty->setAccessible(true); $resource = $reflectorProperty->getValue($this->redisHandler->getConnection()); } if ($this->redisHandler === null || !$this->redisHandler->getConnection()->isConnected() || !is_resource($resource)) { $this->redisHandler = new Predis\Client(INIT::$REDIS_SERVERS); } return $this->redisHandler; }
table.insert(hashes, redis.call('hgetall', key)) end return hashes LUA; public function getScript() { return self::BODY; } } // ------------------------------------------------------------------------- // $parameters = array('tcp://127.0.0.1:6379/?alias=master', 'tcp://127.0.0.1:6380/?alias=slave'); $options = array('profile' => function ($options, $option) { $profile = $options->getDefault($option); $profile->defineCommand('hmgetall', 'HashMultipleGetAll'); return $profile; }, 'replication' => function () { $strategy = new ReplicationStrategy(); $strategy->setScriptReadOnly(HashMultipleGetAll::BODY); $replication = new MasterSlaveReplication($strategy); return $replication; }); // ------------------------------------------------------------------------- // $client = new Predis\Client($parameters, $options); // Execute the following commands on the master server using redis-cli: // $ ./redis-cli HMSET metavars foo bar hoge piyo // $ ./redis-cli HMSET servers master host1 slave host2 $hashes = $client->hmgetall('metavars', 'servers'); $replication = $client->getConnection(); $stillOnSlave = $replication->getCurrent() === $replication->getConnectionById('slave'); echo "Is still on slave? ", $stillOnSlave ? 'YES!' : 'NO!', PHP_EOL; var_export($hashes);
$this->storeDebug($command, '->'); } public function readResponse(CommandInterface $command) { $response = parent::readResponse($command); $this->storeDebug($command, '<-'); return $response; } public function getDebugBuffer() { return $this->debugBuffer; } } $options = array('connections' => array('tcp' => 'SimpleDebuggableConnection')); $client = new Predis\Client($single_server, $options); $client->set('foo', 'bar'); $client->get('foo'); $client->info(); var_export($client->getConnection()->getDebugBuffer()); /* OUTPUT: array ( 0 => 'SELECT 15 -> 127.0.0.1:6379 [0.0008s]', 1 => 'SELECT 15 <- 127.0.0.1:6379 [0.001s]', 2 => 'SET foo -> 127.0.0.1:6379 [0.001s]', 3 => 'SET foo <- 127.0.0.1:6379 [0.0011s]', 4 => 'GET foo -> 127.0.0.1:6379 [0.0013s]', 5 => 'GET foo <- 127.0.0.1:6379 [0.0015s]', 6 => 'INFO -> 127.0.0.1:6379 [0.0019s]', 7 => 'INFO <- 127.0.0.1:6379 [0.0022s]', ) */
// scenarios. The only but relevant difference with a basic replication scenario // is that sentinel servers can manage the master server and its slaves based on // their state, which means that they are able to provide an authoritative and // updated configuration to clients thus avoiding static configurations for the // replication servers and their roles. // Instead of connection parameters pointing to redis nodes, we provide a list // of instances of redis-sentinel. Users should always provide a timeout value // low enough to not hinder operations just in case a sentinel is unreachable // but Predis uses a default value of 100 milliseconds for sentinel parameters // without an explicit timeout value. // // NOTE: in real-world scenarios sentinels should be running on different hosts! $sentinels = array('tcp://127.0.0.1:5380?timeout=0.100', 'tcp://127.0.0.1:5381?timeout=0.100', 'tcp://127.0.0.1:5382?timeout=0.100'); $client = new Predis\Client($sentinels, array('replication' => 'sentinel', 'service' => 'mymaster')); // Read operation. $exists = $client->exists('foo') ? 'yes' : 'no'; $current = $client->getConnection()->getCurrent()->getParameters(); echo "Does 'foo' exist on {$current->role}? {$exists}.", PHP_EOL; // Write operation. $client->set('foo', 'bar'); $current = $client->getConnection()->getCurrent()->getParameters(); echo "Now 'foo' has been set to 'bar' on {$current->role}!", PHP_EOL; // Read operation. $bar = $client->get('foo'); $current = $client->getConnection()->getCurrent()->getParameters(); echo "We fetched 'foo' from {$current->role} and its value is '{$bar}'.", PHP_EOL; /* OUTPUT: Does 'foo' exist on slave-127.0.0.1:6381? yes. Now 'foo' has been set to 'bar' on master! We fetched 'foo' from master and its value is 'bar'. */
/** * auto-connect to the available cache-system on the server * * @return iAdapter */ protected function autoConnectToAvailableCacheSystem() { static $adapterCache; if (is_object($adapterCache) && $adapterCache instanceof iAdapter) { return $adapterCache; } else { $memcached = null; $isMemcachedAvailable = false; if (extension_loaded('memcached')) { $memcached = new \Memcached(); $isMemcachedAvailable = $memcached->addServer('127.0.0.1', '11211'); } if ($isMemcachedAvailable === false) { $memcached = null; } $adapterMemcached = new AdapterMemcached($memcached); if ($adapterMemcached->installed() === true) { // fallback to Memcached $adapter = $adapterMemcached; } else { $memcache = null; $isMemcacheAvailable = false; if (class_exists('\\Memcache')) { $memcache = new \Memcache(); /** @noinspection PhpUsageOfSilenceOperatorInspection */ $isMemcacheAvailable = @$memcache->connect('127.0.0.1', 11211); } if ($isMemcacheAvailable === false) { $memcache = null; } $adapterMemcache = new AdapterMemcache($memcache); if ($adapterMemcache->installed() === true) { // fallback to Memcache $adapter = $adapterMemcache; } else { $redis = null; $isRedisAvailable = false; if (extension_loaded('redis')) { if (class_exists('\\Predis\\Client')) { $redis = new \Predis\Client(array('scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379, 'timeout' => '2.0')); try { $redis->connect(); $isRedisAvailable = $redis->getConnection()->isConnected(); } catch (\Exception $e) { // nothing } } } if ($isRedisAvailable === false) { $redis = null; } $adapterRedis = new AdapterPredis($redis); if ($adapterRedis->installed() === true) { // fallback to Redis $adapter = $adapterRedis; } else { $adapterXcache = new AdapterXcache(); if ($adapterXcache->installed() === true) { // fallback to Xcache $adapter = $adapterXcache; } else { $adapterApc = new AdapterApc(); if ($adapterApc->installed() === true) { // fallback to APC || APCu $adapter = $adapterApc; } else { // no cache-adapter available -> use a array $adapter = new AdapterArray(); } } } } } // save to static cache $adapterCache = $adapter; } return $adapter; }
function testClientInitialization_ClusterConnectionParameters() { $params1 = array_merge(RC::getConnectionArguments(), array('connection_timeout' => 10, 'read_write_timeout' => 30)); $params2 = RC::getConnectionParametersArgumentsString($params1); $params3 = new Predis\ConnectionParameters($params1); $params4 = new Predis\Network\StreamConnection($params3); $client1 = new Predis\Client(array($params1, $params2, $params3, $params4)); foreach ($client1->getConnection() as $connection) { $parameters = $connection->getParameters(); $this->assertEquals($params1['host'], $parameters->host); $this->assertEquals($params1['port'], $parameters->port); $this->assertEquals($params1['connection_timeout'], $parameters->connection_timeout); $this->assertEquals($params1['read_write_timeout'], $parameters->read_write_timeout); $this->assertNull($parameters->password); } $connectionCluster = $client1->getConnection(); $client2 = new Predis\Client($connectionCluster); $this->assertSame($connectionCluster, $client2->getConnection()); }
} public function readResponse(CommandInterface $command) { $reply = parent::readResponse($command); $this->storeDebug($command, '<-'); return $reply; } public function getDebugBuffer() { return $this->debugBuffer; } } $options = array('connections' => array('tcp' => 'SimpleDebuggableConnection')); $client = new Predis\Client($single_server, $options); $client->set('foo', 'bar'); $client->get('foo'); $client->info(); print_r($client->getConnection()->getDebugBuffer()); /* OUTPUT: Array ( [0] => SELECT 15 -> 127.0.0.1:6379 [0.0008s] [1] => SELECT 15 <- 127.0.0.1:6379 [0.0012s] [2] => SET foo -> 127.0.0.1:6379 [0.0014s] [3] => SET foo <- 127.0.0.1:6379 [0.0014s] [4] => GET foo -> 127.0.0.1:6379 [0.0016s] [5] => GET foo <- 127.0.0.1:6379 [0.0018s] [6] => INFO -> 127.0.0.1:6379 [0.002s] [7] => INFO <- 127.0.0.1:6379 [0.0025s] ) */
} public function readResponse(ICommand $command) { $reply = parent::readResponse($command); $this->storeDebug($command, '<-'); return $reply; } public function getDebugBuffer() { return $this->_debugBuffer; } } $options = array('connections' => array('tcp' => 'SimpleDebuggableConnection')); $redis = new Predis\Client($single_server, $options); $redis->set('foo', 'bar'); $redis->get('foo'); $redis->info(); print_r($redis->getConnection()->getDebugBuffer()); /* OUTPUT: Array ( [0] => SELECT 15 -> 127.0.0.1:6379 [0.0008s] [1] => SELECT 15 <- 127.0.0.1:6379 [0.0012s] [2] => SET foo -> 127.0.0.1:6379 [0.0014s] [3] => SET foo <- 127.0.0.1:6379 [0.0014s] [4] => GET foo -> 127.0.0.1:6379 [0.0016s] [5] => GET foo <- 127.0.0.1:6379 [0.0018s] [6] => INFO -> 127.0.0.1:6379 [0.002s] [7] => INFO <- 127.0.0.1:6379 [0.0025s] ) */