public function __construct() { $this->_timeStart = microtime(true); $host = (string) (Mage::getConfig()->getNode(self::XML_PATH_HOST) ?: '127.0.0.1'); $port = (int) (Mage::getConfig()->getNode(self::XML_PATH_PORT) ?: '6379'); $pass = (string) (Mage::getConfig()->getNode(self::XML_PATH_PASS) ?: ''); $timeout = (double) (Mage::getConfig()->getNode(self::XML_PATH_TIMEOUT) ?: self::DEFAULT_TIMEOUT); $persistent = (string) (Mage::getConfig()->getNode(self::XML_PATH_PERSISTENT) ?: ''); $this->_dbNum = (int) (Mage::getConfig()->getNode(self::XML_PATH_DB) ?: 0); $this->_compressionThreshold = (int) (Mage::getConfig()->getNode(self::XML_PATH_COMPRESSION_THRESHOLD) ?: self::DEFAULT_COMPRESSION_THRESHOLD); $this->_compressionLib = (string) (Mage::getConfig()->getNode(self::XML_PATH_COMPRESSION_LIB) ?: self::DEFAULT_COMPRESSION_LIB); $this->_logLevel = (int) (Mage::getConfig()->getNode(self::XML_PATH_LOG_LEVEL) ?: self::DEFAULT_LOG_LEVEL); $this->_maxConcurrency = (int) (Mage::getConfig()->getNode(self::XML_PATH_MAX_CONCURRENCY) ?: self::DEFAULT_MAX_CONCURRENCY); $this->_breakAfter = (int) (Mage::getConfig()->getNode(sprintf(self::XML_PATH_BREAK_AFTER, session_name())) ?: self::DEFAULT_BREAK_AFTER); $this->_botLifetime = (int) (Mage::getConfig()->getNode(self::XML_PATH_BOT_LIFETIME) ?: self::DEFAULT_BOT_LIFETIME); if ($this->_botLifetime) { $userAgent = empty($_SERVER['HTTP_USER_AGENT']) ? FALSE : $_SERVER['HTTP_USER_AGENT']; $this->_isBot = !$userAgent || preg_match(self::BOT_REGEX, $userAgent); } $this->_redis = new Credis_Client($host, $port, $timeout, $persistent); if (!empty($pass)) { $this->_redis->auth($pass) or Zend_Cache::throwException('Unable to authenticate with the redis server.'); } $this->_redis->setCloseOnDestruct(FALSE); // Destructor order cannot be predicted $this->_useRedis = TRUE; if ($this->_logLevel >= 7) { Mage::log(sprintf("%s: %s initialized for connection to %s:%s after %.5f seconds", $this->_getPid(), get_class($this), $host, $port, microtime(true) - $this->_timeStart), Zend_Log::DEBUG, self::LOG_FILE); if ($this->_isBot) { Mage::log(sprintf("%s: Bot detected for user agent: %s", $this->_getPid(), $userAgent), Zend_Log::DEBUG, self::LOG_FILE); } } }
public function testPassword() { $this->tearDown(); $this->assertObjectHasAttribute('password', $this->config[4]); $this->credis = new Credis_Client($this->config[4]->host, $this->config[4]->port, $this->config[4]->timeout, false, 0, $this->config[4]->password); $this->assertInstanceOf('Credis_Client', $this->credis->connect()); $this->assertTrue($this->credis->set('key', 'value')); $this->credis->close(); $this->credis = new Credis_Client($this->config[4]->host, $this->config[4]->port, $this->config[4]->timeout, false, 0, 'wrongpassword'); $this->credis->connect(); $this->assertFalse($this->credis->set('key', 'value')); $this->assertFalse($this->credis->auth('anotherwrongpassword')); $this->assertTrue($this->credis->auth('thepassword')); $this->assertTrue($this->credis->set('key', 'value')); }
public function _load($session_id) { $host = (string) (Mage::getConfig()->getNode(self::XML_PATH_HOST) ?: '127.0.0.1'); $port = (int) (Mage::getConfig()->getNode(self::XML_PATH_PORT) ?: '6379'); $pass = (string) (Mage::getConfig()->getNode(self::XML_PATH_PASS) ?: ''); $timeout = (double) (Mage::getConfig()->getNode(self::XML_PATH_TIMEOUT) ?: '2.5'); $_redis = new Credis_Client($host, $port, $timeout); if (!empty($pass)) { $_redis->auth($pass); } $_redis->connect(); // connect to redis // replace sess_session_id with session id you want to read. $sessionData = $_redis->hGet('sess_' . $session_id, 'data'); // only data field is relevant to us, uncompress the data return $sessionData ? $this->_decodeData($sessionData) : false; }
/** * @param Handler\ConfigInterface $config * @param Handler\LoggerInterface $logger * @param Handler\ProfilerInterface|null $profiler */ public function __construct(\Cm\RedisSession\Handler\ConfigInterface $config, \Cm\RedisSession\Handler\LoggerInterface $logger, \Cm\RedisSession\Handler\ProfilerInterface $profiler = null) { $this->_config = $config; $this->logger = $logger; $this->profiler = $profiler; $this->_logLevel = (int) ($config->descend('log_level') ?: self::DEFAULT_LOG_LEVEL); if ($this->_logLevel >= \Zend_Log::DEBUG) { $timeStart = microtime(true); } // Database config $host = "{$config->descend('host')}" ?: '127.0.0.1'; $port = (int) $config->descend('port') ?: 6379; $pass = (string) ($config->descend('password') ?: ''); $timeout = (double) ($config->descend('timeout') ?: self::DEFAULT_TIMEOUT); $persistent = (string) ($config->descend('persistent') ?: ''); $this->_dbNum = (int) ($config->descend('db') ?: 0); // General config $this->_compressionThreshold = (int) ($config->descend('compression_threshold') ?: self::DEFAULT_COMPRESSION_THRESHOLD); $this->_compressionLib = (string) ($config->descend('compression_lib') ?: self::DEFAULT_COMPRESSION_LIB); $this->_maxConcurrency = (int) ($config->descend('max_concurrency') ?: self::DEFAULT_MAX_CONCURRENCY); $this->_maxLifetime = (int) ($config->descend('max_lifetime') ?: self::DEFAULT_MAX_LIFETIME); $this->_minLifetime = (int) ($config->descend('min_lifetime') ?: self::DEFAULT_MIN_LIFETIME); $this->_useLocking = defined('CM_REDISSESSION_LOCKING_ENABLED') ? CM_REDISSESSION_LOCKING_ENABLED : !(strlen("{$config->descend('disable_locking')}") ? (bool) "{$config->descend('disable_locking')}" : 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); if (!empty($pass)) { $this->_redis->auth($pass) or \Zend_Cache::throwException('Unable to authenticate with the redis server.'); } $this->_redis->setCloseOnDestruct(FALSE); // Destructor order cannot be predicted if ($this->_logLevel >= \Zend_Log::DEBUG) { $this->_log(sprintf("%s initialized for connection to %s:%s after %.5f seconds", get_class($this), $host, $port, microtime(true) - $timeStart)); } }
public function __construct() { $this->_config = $config = Mage::getConfig()->getNode('global/redis_session'); if (!$config) { $this->_useRedis = FALSE; Mage::log('Redis configuration does not exist, falling back to MySQL handler.', Zend_Log::EMERG); parent::__construct(); return; } $this->_logLevel = (int) ($config->descend('log_level') ?: self::DEFAULT_LOG_LEVEL); if ($this->_logLevel >= Zend_Log::DEBUG) { $timeStart = microtime(true); } // Database config $host = "{$config->descend('host')}" ?: '127.0.0.1'; $port = (int) $config->descend('port') ?: 6379; $pass = (string) ($config->descend('password') ?: ''); $timeout = (double) ($config->descend('timeout') ?: self::DEFAULT_TIMEOUT); $persistent = (string) ($config->descend('persistent') ?: ''); $this->_dbNum = (int) ($config->descend('db') ?: 0); // General config $this->_compressionThreshold = (int) ($config->descend('compression_threshold') ?: self::DEFAULT_COMPRESSION_THRESHOLD); $this->_compressionLib = (string) ($config->descend('compression_lib') ?: self::DEFAULT_COMPRESSION_LIB); $this->_maxConcurrency = (int) ($config->descend('max_concurrency') ?: self::DEFAULT_MAX_CONCURRENCY); $this->_maxLifetime = (int) ($config->descend('max_lifetime') ?: self::DEFAULT_MAX_LIFETIME); $this->_minLifetime = (int) ($config->descend('min_lifetime') ?: self::DEFAULT_MIN_LIFETIME); $this->_useLocking = defined('CM_REDISSESSION_LOCKING_ENABLED') ? CM_REDISSESSION_LOCKING_ENABLED : !(strlen("{$config->descend('disable_locking')}") ? (bool) "{$config->descend('disable_locking')}" : 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); if (!empty($pass)) { $this->_redis->auth($pass) or Zend_Cache::throwException('Unable to authenticate with the redis server.'); } $this->_redis->setCloseOnDestruct(FALSE); // Destructor order cannot be predicted $this->_useRedis = TRUE; if ($this->_logLevel >= Zend_Log::DEBUG) { $this->_log(sprintf("%s initialized for connection to %s:%s after %.5f seconds", get_class($this), $host, $port, microtime(true) - $timeStart)); } }
/** * 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']; } }