public function clear($prefix = '') { $prefix = $this->getNamespace() . $prefix . '*'; $it = null; self::$cache->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY); while ($keys = self::$cache->scan($it, $prefix)) { self::$cache->delete($keys); } return true; }
private function initializeCAS() { $casClient = new \CAS_Client(CAS_VERSION_2_0, true, Config::get('cas.hostname'), Config::get('cas.port'), Config::get('cas.context')); $casClient->setNoCasServerValidation(); if (true === Config::get('pgtservice.enabled', false)) { $casClient->setCallbackURL(Config::get('pgtservice.callback')); $casClient->setPGTStorage(new ProxyTicketServiceStorage($casClient)); } else { if (false !== Config::get('redis.hostname', false)) { $casClient->setCallbackURL($this->url->getURL() . '/callback.php'); $redis = new \Redis(); $redis->connect(Config::get('redis.hostname'), Config::get('redis.port', 6379), 2, null, 100); $redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP); $redis->setOption(\Redis::OPT_PREFIX, Config::get('application.project_name') . ':PHPCAS_TICKET_STORAGE:'); $redis->select((int) Config::get('redis.hostname', 2)); $casClient->setPGTStorage(new RedisTicketStorage($casClient, $redis)); } else { $casClient->setCallbackURL($this->url->getURL() . '/callback.php'); $casClient->setPGTStorageFile(session_save_path()); // Handle logout requests but do not validate the server $casClient->handleLogoutRequests(false); } } // Accept all proxy chains $casClient->getAllowedProxyChains()->allowProxyChain(new \CAS_ProxyChain_Any()); return $casClient; }
/** * Constructor. * * @param string $uniqId to be used to separate objects in addition to their key identifiers, for instance in a * multi-user scenario. * * @return \iveeCrest\RedisWrapper */ public function __construct($uniqId) { $this->redis = new \Redis(); $this->redis->connect(Config::getCacheHost(), Config::getCachePort()); $this->redis->setOption(\Redis::OPT_PREFIX, Config::getCachePrefix()); $this->uniqId = $uniqId; }
/** * @param string $host redis server host * @param int $port redis server port * @param int $database redis server database num * @param string $channel redis queue key * @param string $prefix prefix of redis queue key */ public function __construct($host = '127.0.0.1', $port = 6379, $database = 0, $channel = 'cache', $prefix = 'simple-fork-') { $this->redis = new \Redis(); $connection_result = $this->redis->connect($host, $port); if (!$connection_result) { throw new \RuntimeException('can not connect to the redis server'); } if ($database != 0) { $select_result = $this->redis->select($database); if (!$select_result) { throw new \RuntimeException('can not select the database'); } } if (empty($channel)) { throw new \InvalidArgumentException('channel can not be empty'); } $this->channel = $channel; if (empty($prefix)) { return; } $set_option_result = $this->redis->setOption(\Redis::OPT_PREFIX, $prefix); if (!$set_option_result) { throw new \RuntimeException('can not set the \\Redis::OPT_PREFIX Option'); } }
function init() { list($ip, $port) = explode(':', $this->_defaults['server']); $this->_handler = new Redis(); $this->_handler->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); $this->_handler->connect($ip, $port); }
/** * @return void */ protected function init() { $this->prefix = sprintf("%s#", base64_encode($this->identifier)); $this->client = new \Redis(); call_user_func_array([$this->client, 'connect'], func_get_args()); $this->client->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP); $this->client->setOption(\Redis::OPT_PREFIX, $this->prefix); }
/** * If you want to use your own instance of Redis then pass it to UaiCachePhpredis::$redis before calling UserAgentInfoPeer for the first time. */ public static function autoInit() { if (self::$redis instanceof Redis) { return; } self::$redis = new Redis(); self::$redis->pconnect('127.0.0.1'); self::$redis->setOption(Redis::OPT_SERIALIZER, UserAgentInfoConfig::CACHE_USE_IGBINARY ? Redis::SERIALIZER_IGBINARY : Redis::SERIALIZER_PHP); }
public function __construct($connectionOrHost, $port = 6379) { if ($connectionOrHost instanceof \Redis) { $this->connection = $connectionOrHost; } else { $this->connection = new \Redis(); $this->connection->connect($connectionOrHost, $port); $this->connection->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP); } }
public function register(Container $pimple) { $pimple['redis'] = function () use($pimple) { $redis = new \Redis(); $options = $pimple['redis.options']; $redis->connect($options['host'], $options['port'], $options['timeout']); $redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_NONE); $redis->setOption(\Redis::OPT_PREFIX, $options['dbname'] . ':'); $redis->auth($options['password']); return $redis; }; }
protected function _connect() { $this->_redis = new \Redis(); if ($this->_config['persistent']) { $this->_redis->pconnect($this->_config['host'], isset($this->_config['port']) ? $this->_config['port'] : null, isset($this->_config['timeout']) ? $this->_config['timeout'] : null); } else { $this->_redis->connect($this->_config['host'], isset($this->_config['port']) ? $this->_config['port'] : null, isset($this->_config['timeout']) ? $this->_config['timeout'] : null); } $this->_redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_NONE); if ($this->_config['database'] > 0) { $this->_redis->select($this->_config['database']); } }
/** * @return \Redis */ public function getInstance() { if (is_null($this->instance)) { $this->instance = new \Redis(); $this->instance->connect('localhost'); /** * Without it doesn't work with array type */ $serializer = defined('Redis::SERIALIZER_IGBINARY') ? \Redis::SERIALIZER_IGBINARY : \Redis::SERIALIZER_PHP; $this->instance->setOption(\Redis::OPT_SERIALIZER, $serializer); return $this->instance; } return $this->instance; }
/** * @return \Redis * @throws InvalidConfigException */ public function getRedis() { if (null === $this->instance) { if (!extension_loaded('redis')) { throw new InvalidConfigException(__CLASS__ . ' requires PHP `redis` extension to be loaded.'); } $this->instance = new \Redis(); if (!empty($this->options)) { foreach ($this->options as $key => $value) { $this->instance->setOption($key, $value); } } } return $this->instance; }
/** * Gets the redis client * @return Redis the redis client */ public function getClient() { if ($this->_client === null) { $this->_client = new Redis(); $this->_client->connect($this->hostname, $this->port); if (isset($this->password)) { if ($this->_client->auth($this->password) === false) { throw new CException('Redis authentication failed!'); } } $this->_client->setOption(Redis::OPT_PREFIX, $this->prefix); $this->_client->select($this->database); } return $this->_client; }
/** * @param string|string[] $channels * @param Closure $callback */ public function subscribe($channels, Closure $callback) { $channels = (array) $channels; $this->_subscribeCallback = $callback; $this->_redis->setOption(Redis::OPT_READ_TIMEOUT, 86400 * 100); $this->_redis->subscribe($channels, array($this, '_subscribeCallback')); }
/** * Constructs the redis Kohana_Cache object * * @param array configuration * @throws Kohana_Cache_Exception */ protected function __construct(array $config) { if (!extension_loaded('redis')) { throw new Cache_Exception('PHP redis extension is not available.'); } parent::__construct($config); $host = isset($config['host']) ? $config['host'] : $this->_default_config['host']; $port = isset($config['port']) ? $config['port'] : $this->_default_config['port']; $this->_redis = new Redis(); $this->_redis->connect($host, $port, 1); if (@$config['igbinary_serialize'] === true) { $this->_redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); } $db_num = isset($config['db_num']) ? $config['db_num'] : $this->_default_config['db_num']; $this->_redis->select($db_num); }
/** * Must be defined before including bootstrap.php * as this is the only custom part in the example. */ function get_driver() { $redis = new Redis(); $redis->connect('localhost'); $redis->setOption(Redis::OPT_PREFIX, 'bernard:'); return new PhpRedisDriver($redis); }
/** * @param $redisId * * @return Redis|bool */ function _storage_nosql_connect($redisId) { static $allConnectionsConfig; static $connections; lets_use('core_config'); if (isset($connections[$redisId])) { return $connections[$redisId]; } if (!isset($allConnectionsConfig)) { $allConnectionsConfig = core_config_get('redis', []); } if (!isset($allConnectionsConfig[$redisId])) { core_error('redis config not found for id:' . serialize($redisId)); return false; } $connectionConfig = $allConnectionsConfig[$redisId]; $connection = new Redis(); $connected = $connection->connect($connectionConfig['host'], $connectionConfig['port'], $connectionConfig['connect_timeout']); if (!$connected) { core_error('Cannot connect redis driver'); return false; } $connection->setOption(Redis::OPT_READ_TIMEOUT, $connectionConfig['read_timeout']); return $connections[$redisId] = $connection; }
/** * Retrieve Redis Cache Instance * * @return RedisCache|boolean */ public function init() { $redis_cache = false; $config = $this->getServiceLocator()->get('Config'); $config = $config['caches']['redis']; $namespace = $config['adapter']['options']['namespace']; $host = $config['adapter']['options']['server']['host']; $port = $config['adapter']['options']['server']['port']; $ttl = $config['adapter']['options']['ttl']; $redis = new \Redis(); /** * This is not required, although it will allow to store anything that * can be serialized by PHP in Redis */ try { $conn = $redis->pconnect($host, $port, $ttl); $redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP); } catch (\Exception $e) { $conn = false; } if ($conn) { $redis_cache = new RedisCache(); $redis_cache->setNamespace($namespace); $redis_cache->setRedis($redis); } return $redis_cache; }
/** * 获取实例 * * @return void * @author seatle <*****@*****.**> * @created time :2016-04-10 22:55 */ public static function init() { if (extension_loaded('Redis')) { $_instance = new Redis(); } else { $errmsg = "extension redis is not installed"; log::add($errmsg, "Error"); return null; } // 这里不能用pconnect,会报错:Uncaught exception 'RedisException' with message 'read error on connection' $_instance->connect($GLOBALS['config']['redis']['host'], $GLOBALS['config']['redis']['port'], $GLOBALS['config']['redis']['timeout']); // 验证 if ($GLOBALS['config']['redis']['pass']) { if (!$_instance->auth($GLOBALS['config']['redis']['pass'])) { $errmsg = "Redis Server authentication failed!!"; log::add($errmsg, "Error"); return null; } } // 不序列化的话不能存数组,用php的序列化方式其他语言又不能读取,所以这里自己用json序列化了,性能还比php的序列化好1.4倍 //$_instance->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // don't serialize data //$_instance->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // use built-in serialize/unserialize //$_instance->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // use igBinary serialize/unserialize $_instance->setOption(Redis::OPT_PREFIX, $GLOBALS['config']['redis']['prefix'] . ":"); return $_instance; }
public function __construct($settings) { parent::bootstrap(); $t = microtime(true); $redis = new \Redis(); try { $redis->connect($settings['host'], $settings['port']); if (isset($settings['password']) && !empty($settings['password'])) { if ($redis->auth($settings['password']) === false) { throw new \Exception('Unable to authenticate with redis!'); } } if (isset($settings['database']) && !empty($settings['database'])) { if ($redis->select($settings['database']) === false) { throw new \Exception('Unable to Redis database select'); } } } catch (\Exception $e) { throw new \Exception('Unable to connect to Redis server'); } if (isset($settings['prefix'])) { $redis->setOption(\Redis::OPT_PREFIX, $settings['prefix'] . ':'); } parent::$serviceInstance[get_class()] = $redis; $queryTime = round((microtime(true) - $t) * 1000, 2); self::logQuery(array('command' => 'CONNECTION to ' . $settings['host'] . ':' . $settings['port'], 'time' => $queryTime)); self::$_totalTime[get_class()] += $queryTime; self::$_totalQueries[get_class()]++; }
public static final function openConnection($target) { if (empty(self::$redisInfo)) { self::parseConnectionInfo(); } $connection = new Redis(); if (isset(self::$redisInfo[$target])) { $info = self::$redisInfo[$target]; } else { $info = array('host' => '127.0.0.1', 'port' => 6379, 'timeout' => 0, 'database' => 0, 'password' => '', 'options' => array()); } try { $connection->connect($info['host'], $info['port'], $info['timeout']); if ($info['password']) { $connection->auth($info['password']); } $connection->select($info['database']); foreach ($info['options'] as $k => $v) { $connection->setOption($k, $v); } } catch (Exception $e) { $connection = null; } return $connection; }
function conn_redis() { $redis = new Redis(); $redis->connect('localhost', '6379'); $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); return $redis; }
public function createRedis() { $redis = new \Redis(); $redis->connect($this->getOption('host'), $this->getOption('port')); $redis->setOption(\Redis::OPT_PREFIX, $this->getOption('prefix')); return $redis; }
/** * Sets a prefix for all keys (if needed) using options in the given configuration array. * * @return bool True if setting the prefix succeeded or no prefix was specified, else false */ protected function _setPrefix() { if ($this->config['prefix']) { return $this->_connection->setOption(Redis::OPT_PREFIX, $this->config['prefix']); } return true; }
public function connect() { if ($this->handler) { return $this->handler; } $config = $this->config; $handler = new \Redis(); // 优先使用unix socket $conn_args = $config['unix_socket'] ? array($config['unix_socket']) : array($config['host'], $config['port'], $config['timeout']); if ($this->isPersistent()) { $conn_args[] = $config['persistent_id']; $conn = call_user_func_array(array($handler, 'pconnect'), $conn_args); } else { $conn = call_user_func_array(array($handler, 'connect'), $conn_args); } if (!$conn) { throw new \Owl\Service\Exception('Cannot connect redis'); } if ($config['password'] && !$handler->auth($config['password'])) { throw new \Owl\Service\Exception('Invalid redis password'); } if ($config['database'] && !$handler->select($config['database'])) { throw new \Owl\Service\Exception('Select redis database[' . $config['database'] . '] failed'); } if (isset($config['prefix'])) { $handler->setOption(\Redis::OPT_PREFIX, $config['prefix']); } return $this->handler = $handler; }
/** * 创建 Redis.io client * @param array $servers * @param array $options * @return array * @throw exception */ protected function createClients(array $servers, array $options = []) { $clients = []; try { foreach ($servers as $key_s => $server) { $redis = new \Redis(); //长连接为pconnect,长连接要注意执行close关闭 $func = Arr::get($server, 'persistent', false) ? 'pconnect' : 'connect'; $redis->connect(Arr::get($server, 'host', ''), Arr::get($server, 'port'), $this->timeOut); //有配置密码的,进行auth操作 if ($pwd = Arr::get($server, 'password', '')) { $redis->auth($pwd); } $redis->select(Arr::get($server, 'database')); //设置redis的option,如Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE foreach ($options as $key => $val) { $redis->setOption($key, $val); } $clients[$key_s] = $redis; } } catch (\Exception $e) { throw new \Exception("connect redis error:" . var_export($e->getMessage(), 1)); } return $clients; }
/** * Returns a Redis connection. * * @param array $options Available options are 'host' and 'port' * * @return \Redis * * @throws RuntimeException */ public function getRedisConnection(array $options = []) { $options = array_replace(['host' => 'localhost', 'port' => 6379], $options); if (null !== ($cache = $this->getConnection('redis', $options))) { return $cache; } if (!extension_loaded('redis')) { throw new RuntimeException('The Redis cache requires the Redis extension.'); } $redis = new \Redis(); if (!@$redis->connect($options['host'], $options['port'])) { throw new RuntimeException(sprintf("Redis instance with host '%s' and port '%s' is not reachable.", $options['host'], $options['port'])); } if (!defined('Redis::SERIALIZER_IGBINARY') || !$redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_IGBINARY)) { $redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP); } return $this->setConnection('redis', $options, $redis); }
public static function getRedisClient() { $redis = new \Redis(); $redis->connect(self::HOST, self::PORT); $redis->select(self::DB_INDEX); if ($redis->getOption(\Redis::OPT_SERIALIZER) === \Redis::SERIALIZER_NONE) { $redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP); } return $redis; }
private static function redis_init() { static $redis = null; if (empty($redis) || !$redis->ping()) { $redis = new Redis(); $redis->connect(SPWX_REDIS_HOST, SPWX_REDIS_PORT); $redis->setOption(Redis::OPT_PREFIX, SPWX_REDIS_PREFIX); } return $redis; }
public static function createConnection() { $host = isset($GLOBALS['redis_host']) ? $GLOBALS['redis_host'] : '127.0.0.1'; $port = isset($GLOBALS['redis_port']) ? $GLOBALS['redis_port'] : 6379; $prefix = isset($GLOBALS['redis_prefix']) ? $GLOBALS['redis_prefix'] : 'task_queue_tests:'; $redis = new \Redis(); $redis->connect($host, $port); $redis->setOption(\Redis::OPT_PREFIX, $prefix); return $redis; }