/** * @param int $database * @throws CM_Exception */ protected function _select($database) { $database = (int) $database; if ('OK' !== $this->_redis->select($database)) { throw new CM_Exception('Cannot select database.', null, ['database' => $database]); } }
/** * Instantiate the Redis class. * * Instantiates the Redis class. * * @param null $persistent_id To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance. */ public function __construct() { global $blog_id, $table_prefix; $redis = array('scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379); if (defined('WP_REDIS_SCHEME')) { $redis['scheme'] = WP_REDIS_SCHEME; } if (defined('WP_REDIS_HOST')) { $redis['host'] = WP_REDIS_HOST; } if (defined('WP_REDIS_PORT')) { $redis['port'] = WP_REDIS_PORT; } if (defined('WP_REDIS_PATH')) { $redis['path'] = WP_REDIS_PATH; } if (defined('WP_REDIS_PASSWORD')) { $redis['password'] = WP_REDIS_PASSWORD; } if (defined('WP_REDIS_DATABASE')) { $redis['database'] = WP_REDIS_DATABASE; } $redis_client = defined('WP_REDIS_CLIENT') ? WP_REDIS_CLIENT : null; if (class_exists('Redis') && strcasecmp('predis', $redis_client) !== 0) { $redis_client = defined('HHVM_VERSION') ? 'hhvm' : 'pecl'; } else { $redis_client = 'predis'; } try { if (strcasecmp('hhvm', $redis_client) === 0) { $this->redis_client = sprintf('HHVM %s Extension', HHVM_VERSION); $this->redis = new Redis(); // adjust host and port, if the scheme is `unix` if (strcasecmp('unix', $redis['scheme']) === 0) { $redis['host'] = 'unix://' . $redis['path']; $redis['port'] = 0; } if (!$this->redis->connect($redis['host'], $redis['port'])) { throw new Exception(); } if (isset($redis['password'])) { $this->redis->auth($redis['password']); } if (isset($redis['database'])) { $this->redis->select($redis['database']); } $this->redis_connected = true; } elseif (strcasecmp('pecl', $redis_client) === 0) { $this->redis_client = 'PCEL Extension'; $this->redis = new Redis(); if (strcasecmp('unix', $redis['scheme']) === 0) { $this->redis->connect($redis['path']); } else { $this->redis->connect($redis['host'], $redis['port']); } if (isset($redis['password'])) { $this->redis->auth($redis['password']); } if (isset($redis['database'])) { $this->redis->select($redis['database']); } $this->redis_connected = true; } else { $this->redis_client = 'Predis'; // require PHP 5.4 or greater if (version_compare(PHP_VERSION, '5.4.0', '<')) { throw new Exception(); } // check if bundled Predis library exists if (!realpath(dirname(__FILE__) . '/plugins/redis-cache/includes/predis.php')) { throw new Exception(); } require_once dirname(__FILE__) . '/plugins/redis-cache/includes/predis.php'; Predis\Autoloader::register(); $this->redis = new Predis\Client($redis); $this->redis->connect(); $this->redis_connected = true; $this->redis_client .= ' v' . Predis\Client::VERSION; } } catch (Exception $exception) { // When Redis is unavailable, fall back to the internal back by forcing all groups to be "no redis" groups $this->no_redis_groups = array_unique(array_merge($this->no_redis_groups, $this->global_groups)); $this->redis_connected = false; } /** * This approach is borrowed from Sivel and Boren. Use the salt for easy cache invalidation and for * multi single WP installs on the same server. */ if (!defined('WP_CACHE_KEY_SALT')) { define('WP_CACHE_KEY_SALT', ''); } // Assign global and blog prefixes for use with keys if (function_exists('is_multisite')) { $this->global_prefix = is_multisite() || defined('CUSTOM_USER_TABLE') && defined('CUSTOM_USER_META_TABLE') ? '' : $table_prefix; $this->blog_prefix = (is_multisite() ? $blog_id : $table_prefix) . ':'; } }
private static function createConnection() { $serverProfile = Predis\RedisServerProfile::get('dev'); $connection = new Predis\Client(RC::getConnectionArguments(), $serverProfile); $connection->connect(); $connection->select(RC::DEFAULT_DATABASE); return $connection; }
public static function createConnection(array $additional = array()) { $serverProfile = Predis\Profiles\ServerProfile::get(self::SERVER_VERSION); $connection = new Predis\Client(RC::getConnectionArguments($additional), $serverProfile); $connection->connect(); $connection->select(RC::DEFAULT_DATABASE); return $connection; }
private function buildRedisObject($client, $host, $port, $database = 0) { if ($client == 'phpredis') { $redis = new Redis(); $redis->connect($host, $port); } elseif ($client == 'predis') { $redis = new Predis\Client(array('scheme' => 'tcp', 'host' => $host, 'port' => $port)); $redis->connect(); } if ($database) { $redis->select($database); } return $redis; }
} $server = $config['servers'][$i]; $server['id'] = $i; if (isset($login, $login['servers'])) { if (array_search($i, $login['servers']) === false) { die('You are not allowed to access this database.'); } foreach ($config['servers'] as $key => $ignore) { if (array_search($key, $login['servers']) === false) { unset($config['servers'][$key]); } } } if (!isset($server['db'])) { $server['db'] = 0; } if (!isset($server['filter'])) { $server['filter'] = '*'; } // Setup a connection to Redis. $redis = new Predis\Client('tcp://' . $server['host'] . ':' . $server['port']); if (isset($server['auth'])) { if (!$redis->auth($server['auth'])) { die('ERROR: Authentication failed (' . $server['host'] . ':' . $server['port'] . ')'); } } if ($server['db'] != 0) { if (!$redis->select($server['db'])) { die('ERROR: Selecting database failed (' . $server['host'] . ':' . $server['port'] . ',' . $server['db'] . ')'); } }
/** * Performs the test. * * @return \Jyxo\Beholder\Result */ public function run() { // The redis extension or Predis library is required if (!extension_loaded('redis') && !class_exists('\\Predis\\Client')) { return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, 'Extension redis or Predis library required'); } $random = md5(uniqid(time(), true)); $key = 'beholder-' . $random; $value = $random; // Status label $description = (false !== filter_var($this->host, FILTER_VALIDATE_IP) ? gethostbyaddr($this->host) : $this->host) . ':' . $this->port . '?database=' . $this->database; // Connection if (extension_loaded('redis')) { $redis = new \Redis(); if (false === $redis->connect($this->host, $this->port, 2)) { return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Connection error %s', $description)); } } else { $redis = new \Predis\Client(array('host' => $this->host, 'port' => $this->port)); if (false === $redis->connect()) { return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Connection error %s', $description)); } } // Select database if (false === $redis->select($this->database)) { return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Database error %s', $description)); } // Saving if (false === $redis->set($key, $value)) { if ($redis instanceof \Redis) { $redis->close(); } else { $redis->quit(); } return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Write error %s', $description)); } // Check $check = $redis->get($key); if (false === $check || $check !== $value) { if ($redis instanceof \Redis) { $redis->close(); } else { $redis->quit(); } return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Read error %s', $description)); } // Deleting if (false === $redis->del($key)) { if ($redis instanceof \Redis) { $redis->close(); } else { $redis->quit(); } return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Delete error %s', $description)); } // Disconnect if ($redis instanceof \Redis) { $redis->close(); } else { $redis->quit(); } // OK return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::SUCCESS, $description); }
/** * Instantiate the Redis class. * * Instantiates the Redis class. * * @param null $persistent_id To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance. */ public function __construct() { global $blog_id, $table_prefix; // General Redis settings $redis = array('host' => '127.0.0.1', 'port' => 6379); if (defined('WP_REDIS_BACKEND_HOST') && WP_REDIS_BACKEND_HOST) { $redis['host'] = WP_REDIS_BACKEND_HOST; } if (defined('WP_REDIS_BACKEND_PORT') && WP_REDIS_BACKEND_PORT) { $redis['port'] = WP_REDIS_BACKEND_PORT; } if (defined('WP_REDIS_BACKEND_AUTH') && WP_REDIS_BACKEND_AUTH) { $redis['auth'] = WP_REDIS_BACKEND_AUTH; } if (defined('WP_REDIS_BACKEND_DB') && WP_REDIS_BACKEND_DB) { $redis['database'] = WP_REDIS_BACKEND_DB; } try { $params = ['tcp://redis-master:6379?alias=master', 'tcp://localhost:6379?alias=slave']; $options = ['replication' => true]; $this->redis = new Predis\Client($params, $options); if (isset($redis['auth'])) { $this->redis->auth($redis['auth']); } if (isset($redis['database'])) { $this->redis->select($redis['database']); } $this->redis_connected = true; } catch (Predis\PredisException $e) { // When Redis is unavailable, fall back to the internal back by forcing all groups to be "no redis" groups $this->no_redis_groups = array_unique(array_merge($this->no_redis_groups, $this->global_groups)); $this->redis_connected = false; } /** * This approach is borrowed from Sivel and Boren. Use the salt for easy cache invalidation and for * multi single WP installs on the same server. */ if (!defined('WP_CACHE_KEY_SALT')) { define('WP_CACHE_KEY_SALT', ''); } // Assign global and blog prefixes for use with keys if (function_exists('is_multisite')) { $this->global_prefix = is_multisite() || defined('CUSTOM_USER_TABLE') && defined('CUSTOM_USER_META_TABLE') ? '' : $table_prefix; $this->blog_prefix = (is_multisite() ? $blog_id : $table_prefix) . ':'; } }
public function getClient($options = array()) { self::setPredisAutoload(); if (!empty($options['socket'])) { $options['scheme'] = 'unix'; $options['path'] = $options['socket']; } foreach ($options as $key => $value) { if (!isset($value)) { unset($options[$key]); } } // I'm not sure why but the error handler is driven crazy if timezone // is not set at this point. // Hopefully Drupal will restore the right one this once the current // account has logged in. date_default_timezone_set(@date_default_timezone_get()); $client = new \Predis\Client($options); if (isset($options['base']) && 0 !== $options['base']) { $client->select((int) $options['base']); } return $client; }
/** * Instantiate the Redis class. * * Instantiates the Redis class. * * @param null $persistent_id To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance. */ public function __construct() { global $blog_id, $table_prefix; // General Redis settings $redis = array('host' => '127.0.0.1', 'port' => 6379); if (defined('WP_REDIS_BACKEND_HOST') && WP_REDIS_BACKEND_HOST) { $redis['host'] = WP_REDIS_BACKEND_HOST; } if (defined('WP_REDIS_BACKEND_PORT') && WP_REDIS_BACKEND_PORT) { $redis['port'] = WP_REDIS_BACKEND_PORT; } if (defined('WP_REDIS_BACKEND_AUTH') && WP_REDIS_BACKEND_AUTH) { $redis['auth'] = WP_REDIS_BACKEND_AUTH; } if (defined('WP_REDIS_BACKEND_DB') && WP_REDIS_BACKEND_DB) { $redis['database'] = WP_REDIS_BACKEND_DB; } if (defined('WP_REDIS_SERIALIZER')) { $redis['serializer'] = WP_REDIS_SERIALIZER; } else { $redis['serializer'] = Redis::SERIALIZER_PHP; } // Use Redis PECL library. try { $this->redis = new Redis(); $this->redis->connect($redis['host'], $redis['port']); $this->redis->setOption(Redis::OPT_SERIALIZER, $redis['serializer']); if (isset($redis['auth'])) { $this->redis->auth($redis['auth']); } if (isset($redis['database'])) { $this->redis->select($redis['database']); } $this->redis_connected = true; } catch (RedisException $e) { // When Redis is unavailable, fall back to the internal back by forcing all groups to be "no redis" groups $this->no_redis_groups = array_unique(array_merge($this->no_redis_groups, $this->global_groups)); $this->redis_connected = false; } /** * This approach is borrowed from Sivel and Boren. Use the salt for easy cache invalidation and for * multi single WP installs on the same server. */ if (!defined('WP_CACHE_KEY_SALT')) { define('WP_CACHE_KEY_SALT', ''); } // Assign global and blog prefixes for use with keys if (function_exists('is_multisite')) { $this->global_prefix = is_multisite() || defined('CUSTOM_USER_TABLE') && defined('CUSTOM_USER_META_TABLE') ? '' : $table_prefix; $this->blog_prefix = (is_multisite() ? $blog_id : $table_prefix) . ':'; } }
* * example : * * $app->container->singleton('authorization.oauth2.github', function ($container) { * return new \PickleWeb\Action\AuthAction('github'); * }); */ // Config $app->container->singleton('app.config', function (Set $container) { return json_decode(file_get_contents(__DIR__ . '/../src/config.json'), true); }); // Redis client $app->container->singleton('redis.client', function (Set $container) { $config = $container->get('app.config'); $client = new Predis\Client(sprintf('tcp://%s:%s', $config['redis']['host'], $config['redis']['port'])); $client->select($config['redis']['db']); return $client; }); // Http client $app->container->singleton('http.client', function (Set $container) { return new Browser(); }); // User repository $app->container->singleton('user.repository', function (Set $container) { return new UserRepository($container->get('redis.client')); }); // User repository $app->container->singleton('extension.repository', function (Set $container) { return new ExtensionRepository($container->get('redis.client')); }); $app->container->singleton('elastica.client', function (Set $container) {