function Redis_Connect($xmlFile) { if (!is_readable($xmlFile)) { throw new Exception(sprintf('File "%s" does not exits or is not readable.', $xmlFile)); } $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA); /** @noinspection PhpUndefinedFieldInspection */ $host = strval($xml->global->cache->backend_options->server); /** @noinspection PhpUndefinedFieldInspection */ $port = strval($xml->global->cache->backend_options->port); /** @noinspection PhpUndefinedFieldInspection */ $db = strval($xml->global->cache->backend_options->database); if (empty($host)) { throw new Exception(sprintf('Redis server hostname is not found in "%s".', $xmlFile)); } if (empty($port)) { throw new Exception(sprintf('Redis server port is not found in "%s".', $xmlFile)); } if (!strlen($db)) { throw new Exception(sprintf('Redis database number is not found in "%s".', $xmlFile)); } if ('/' == substr($host, 0, 1)) { // Socket $server = $host; } else { // TCP $server = sprintf('tcp://%s:%d', $host, $port); } $client = new Credis_Client($server); $client->select($db); return $client; }
/** * @param string $id * @return array * @throws Exception */ public function _inspectSession($id) { if (!$this->_useRedis) { throw new Exception('Not connected to redis!'); } $sessionId = 'sess_' . $id; $this->_redis->select($this->_dbNum); $data = $this->_redis->hGetAll($sessionId); if ($data && isset($data['data'])) { $data['data'] = $this->_decodeData($data['data']); } return $data; }
public function testGettersAndSetters() { $this->assertEquals($this->credis->getHost(), $this->config[0]->host); $this->assertEquals($this->credis->getPort(), $this->config[0]->port); $this->assertEquals($this->credis->getSelectedDb(), 0); $this->assertTrue($this->credis->select(2)); $this->assertEquals($this->credis->getSelectedDb(), 2); $this->assertTrue($this->credis->isConnected()); $this->credis->close(); $this->assertFalse($this->credis->isConnected()); $this->credis = new Credis_Client($this->config[0]->host, $this->config[0]->port, null, 'persistenceId'); $this->assertEquals('persistenceId', $this->credis->getPersistence()); $this->credis = new Credis_Client('localhost', 12345); $this->credis->setMaxConnectRetries(1); $this->setExpectedException('CredisException', 'Connection to Redis failed after 2 failures.'); $this->credis->connect(); }
/** * Log a hit or a miss to Redis. * * @param string $fullActionName * @param bool $hit */ public function logRequest($fullActionName, $hit) { try { $redis = new Credis_Client($this->config->server, NULL, 0.1); $redis->pipeline(); if ($this->config->db) { $redis->select($this->config->db); } $redis->incr('diehard:' . ($hit ? 'hit' : 'miss')); if ($fullActionName && $this->config->is('full_action_name')) { $redis->incr('diehard:' . $fullActionName . ':' . ($hit ? 'hit' : 'miss')); } $redis->exec(); $redis->close(); } catch (Exception $e) { Mage::log($e->getMessage(), Zend_Log::ERR, 'diehard_counter.log'); } }
/** * Public for testing/inspection purposes only. * * @param $forceStandalone * @return \Credis_Client */ public function _redisClient($forceStandalone) { if ($forceStandalone) { $this->_redis->forceStandalone(); } if ($this->_dbNum) { $this->_redis->select($this->_dbNum); } return $this->_redis; }
/** * Destroy session * * @param string $sessionId * @return boolean */ public function destroy($sessionId) { $this->_log(sprintf("Destroying ID %s", $sessionId)); $this->_redis->pipeline(); if ($this->_dbNum) { $this->_redis->select($this->_dbNum); } $this->_redis->del(self::SESSION_PREFIX . $sessionId); $this->_redis->exec(); return true; }
<?php //version 0.0.1 $mageFilename = '../app/Mage.php'; require_once $mageFilename; Mage::setIsDeveloperMode(true); ini_set('display_errors', 1); umask(0); Mage::app(); /** * Set up the redis client */ $_redis; $_redis = new Credis_Client('localhost', 6379, 90, True); $_redis->select(0) || Zend_Cache::throwException('The redis database could not be selected.'); /** * Set up the Session Model to help with compression and other misc items. */ $session = Mage::getModel('Cm_RedisSession_Model_Session '); /** * Get the resource model */ $resource = Mage::getSingleton('core/resource'); /** * Retrieve the read connection */ $readConnection = $resource->getConnection('core_read'); // 1. query to order by session_expires, limit N, save last expire time, and session_id // 2. modify query with where session_expires >= last expire time, and session_id != session_id $exptime = 0; $lastid = 'NONE';
/** * 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']; } }