Example #1
0
 /**
  * @param ConfigInterface $config
  * @param LoggerInterface $logger
  * @throws ConnectionFailedException
  */
 public function __construct(ConfigInterface $config, LoggerInterface $logger)
 {
     $this->config = $config;
     $this->logger = $logger;
     $this->logger->setLogLevel($this->config->getLogLevel() ?: self::DEFAULT_LOG_LEVEL);
     $timeStart = microtime(true);
     // Database config
     $host = $this->config->getHost() ?: self::DEFAULT_HOST;
     $port = $this->config->getPort() ?: self::DEFAULT_PORT;
     $pass = $this->config->getPassword() ?: null;
     $timeout = $this->config->getTimeout() ?: self::DEFAULT_TIMEOUT;
     $persistent = $this->config->getPersistentIdentifier() ?: '';
     $this->_dbNum = $this->config->getDatabase() ?: self::DEFAULT_DATABASE;
     // General config
     $this->_compressionThreshold = $this->config->getCompressionThreshold() ?: self::DEFAULT_COMPRESSION_THRESHOLD;
     $this->_compressionLibrary = $this->config->getCompressionLibrary() ?: self::DEFAULT_COMPRESSION_LIBRARY;
     $this->_maxConcurrency = $this->config->getMaxConcurrency() ?: self::DEFAULT_MAX_CONCURRENCY;
     $this->_maxLifetime = $this->config->getMaxLifetime() ?: self::DEFAULT_MAX_LIFETIME;
     $this->_minLifetime = $this->config->getMinLifetime() ?: self::DEFAULT_MIN_LIFETIME;
     $this->_useLocking = $this->config->getDisableLocking() ?: self::DEFAULT_DISABLE_LOCKING;
     // Use sleep time multiplier so break time is in seconds
     $this->_failAfter = (int) round(1000000 / self::SLEEP_TIME * self::FAIL_AFTER);
     // Connect and authenticate
     $this->_redis = new \Credis_Client($host, $port, $timeout, $persistent, 0, $pass);
     if ($this->hasConnection() == false) {
         throw new ConnectionFailedException('Unable to connect to Redis');
     }
     // Destructor order cannot be predicted
     $this->_redis->setCloseOnDestruct(false);
     $this->_log(sprintf("%s initialized for connection to %s:%s after %.5f seconds", get_class($this), $host, $port, microtime(true) - $timeStart));
 }