Set the read timeout for the connection. Use 0 to disable timeouts entirely (or use a very long timeout
if not supported).
public setReadTimeout ( integer $timeout ) : Credis_Client | ||
$timeout | integer | 0 (or -1) for no timeout, otherwise number of seconds |
return | Credis_Client |
public function testReadTimeout() { $this->credis->setReadTimeout(0.0001); try { $this->credis->save(); $this->fail('Expected exception (read should timeout since disk sync should take longer than 0.0001 seconds).'); } catch (CredisException $e) { } $this->credis->setReadTimeout(10); }
public function init() { $this->_host = $this->_options['host']; $this->_port = $this->_options['port']; $this->_timeout = $this->_options['timeout']; $this->_prefix = $this->_options['prefix']; try { $this->_client = new \Credis_Client($this->_host, $this->_port, $this->_timeout); $this->_client->setReadTimeout(0); } catch (Exception $e) { print_r($e->getMessage()); } }
public function testPubsub() { $timeout = 2; $time = time(); $this->credis->setReadTimeout($timeout); try { $testCase = $this; $this->credis->pSubscribe(array('foobar', 'test*'), function ($credis, $pattern, $channel, $message) use($testCase, &$time) { $time = time(); // Reset timeout // Test using: redis-cli publish foobar blah $testCase->assertEquals('blah', $message); }); $this->fail('pSubscribe should not return.'); } catch (CredisException $e) { $this->assertEquals($timeout, time() - $time); if ($this->useStandalone) { // phpredis does not distinguish between timed out and disconnected $this->assertEquals($e->getCode(), CredisException::CODE_TIMED_OUT); } else { $this->assertEquals($e->getCode(), CredisException::CODE_DISCONNECTED); } } // Perform a new subscription. Client should have either unsubscribed or disconnected $timeout = 2; $time = time(); $this->credis->setReadTimeout($timeout); try { $testCase = $this; $this->credis->subscribe('foobar', function ($credis, $channel, $message) use($testCase, &$time) { $time = time(); // Reset timeout // Test using: redis-cli publish foobar blah $testCase->assertEquals('blah', $message); }); $this->fail('subscribe should not return.'); } catch (CredisException $e) { $this->assertEquals($timeout, time() - $time); if ($this->useStandalone) { // phpredis does not distinguish between timed out and disconnected $this->assertEquals($e->getCode(), CredisException::CODE_TIMED_OUT); } else { $this->assertEquals($e->getCode(), CredisException::CODE_DISCONNECTED); } } }
/** * Contruct Zend_Cache Redis backend * @param array $options * @return \Cm_Cache_Backend_Redis */ public function __construct($options = array()) { if (empty($options['server'])) { Zend_Cache::throwException('Redis \'server\' not specified.'); } if (empty($options['port']) && substr($options['server'], 0, 1) != '/') { Zend_Cache::throwException('Redis \'port\' not specified.'); } $port = isset($options['port']) ? $options['port'] : NULL; $timeout = isset($options['timeout']) ? $options['timeout'] : self::DEFAULT_CONNECT_TIMEOUT; $persistent = isset($options['persistent']) ? $options['persistent'] : ''; $this->_redis = new Credis_Client($options['server'], $port, $timeout, $persistent); if (isset($options['force_standalone']) && $options['force_standalone']) { $this->_redis->forceStandalone(); } $connectRetries = isset($options['connect_retries']) ? (int) $options['connect_retries'] : self::DEFAULT_CONNECT_RETRIES; $this->_redis->setMaxConnectRetries($connectRetries); if (!empty($options['read_timeout']) && $options['read_timeout'] > 0) { $this->_redis->setReadTimeout((double) $options['read_timeout']); } if (!empty($options['password'])) { $this->_redis->auth($options['password']) or Zend_Cache::throwException('Unable to authenticate with the redis server.'); } // Always select database on startup in case persistent connection is re-used by other code if (empty($options['database'])) { $options['database'] = 0; } $this->_redis->select((int) $options['database']) or Zend_Cache::throwException('The redis database could not be selected.'); if (isset($options['notMatchingTags'])) { $this->_notMatchingTags = (bool) $options['notMatchingTags']; } if (isset($options['compress_tags'])) { $this->_compressTags = (int) $options['compress_tags']; } if (isset($options['compress_data'])) { $this->_compressData = (int) $options['compress_data']; } if (isset($options['lifetimelimit'])) { $this->_lifetimelimit = (int) min($options['lifetimelimit'], self::MAX_LIFETIME); } if (isset($options['compress_threshold'])) { $this->_compressThreshold = (int) $options['compress_threshold']; } if (isset($options['automatic_cleaning_factor'])) { $this->_options['automatic_cleaning_factor'] = (int) $options['automatic_cleaning_factor']; } else { $this->_options['automatic_cleaning_factor'] = 0; } if (isset($options['compression_lib'])) { $this->_compressionLib = $options['compression_lib']; } else { if (function_exists('snappy_compress')) { $this->_compressionLib = 'snappy'; } else { if (function_exists('lzf_compress')) { $this->_compressionLib = 'lzf'; } else { $this->_compressionLib = 'gzip'; } } } $this->_compressPrefix = substr($this->_compressionLib, 0, 2) . self::COMPRESS_PREFIX; if (isset($options['use_lua'])) { $this->_useLua = (bool) $options['use_lua']; } }