本文整理汇总了PHP中Redis类的典型用法代码示例。如果您正苦于以下问题:PHP Redis类的具体用法?PHP Redis怎么用?PHP Redis使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Redis类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createClients
/**
* 创建 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;
}
示例2: _overview_all
/**
* 查看全部服务器
*/
private function _overview_all()
{
$info = array();
foreach ($this->server_list as $i => $server) {
if (isset($server['cluster_list'])) {
$info[$i]['error'] = FALSE;
$info[$i]['cluster_list'] = $server['cluster_list'];
continue;
}
$redis = new Redis();
$can_connect = FALSE;
try {
$can_connect = $redis->connect($server['host'], $server['port'], 0.5);
} catch (Exception $e) {
$can_connect = TRUE;
}
$info[$i] = array('error' => FALSE);
if (!$can_connect) {
$info[$i]['error'] = TRUE;
} else {
$info[$i] = array_merge($info[$i], $redis->info());
$info[$i]['size'] = $redis->dbSize();
}
}
$page_data['info'] = $info;
$page_data['server_list'] = $this->server_list;
$page_data['title'] = '服务器一览表';
$this->load->view('overview', $page_data);
}
示例3: getAdapter
/**
* {@inheritdoc}
*/
public function getAdapter(array $config)
{
$client = new \Redis();
$dsn = $this->getDsn();
if (empty($dsn)) {
if (false === $client->connect($config['host'], $config['port'])) {
throw new ConnectException(sprintf('Could not connect to Redis database on "%s:%s".', $config['host'], $config['port']));
}
} else {
if (false === $client->connect($dsn->getFirstHost(), $dsn->getFirstPort())) {
throw new ConnectException(sprintf('Could not connect to Redis database on "%s:%s".', $dsn->getFirstHost(), $dsn->getFirstPort()));
}
if (!empty($dsn->getPassword())) {
if (false === $client->auth($dsn->getPassword())) {
throw new ConnectException('Could not connect authenticate connection to Redis database.');
}
}
if ($dsn->getDatabase() !== null) {
if (false === $client->select($dsn->getDatabase())) {
throw new ConnectException(sprintf('Could not select Redis database with index "%s".', $dsn->getDatabase()));
}
}
}
$pool = new RedisCachePool($client);
if (null !== $config['pool_namespace']) {
$pool = new NamespacedCachePool($pool, $config['pool_namespace']);
}
return $pool;
}
示例4: wait
/**
* {@inheritdoc}
*/
public function wait(Closure $callback)
{
while (true) {
if (!$this->shouldProcessNext()) {
break;
}
while (true) {
$messageString = false;
if ($this->redis instanceof \Predis\Client) {
$messageString = $this->redis->spop($this->key);
}
if ($this->redis instanceof \Redis) {
$messageString = $this->redis->sPop($this->key);
}
if (!$messageString) {
break;
}
$callback($this->serializer->unserialize($messageString));
$this->incrementProcessedItems();
}
if ($this->refreshInterval) {
sleep($this->refreshInterval);
}
}
}
示例5: __callStatic
/**
*
* @param string $name
* @param array $arguments
* @return RedisFactory
* @throws Exception
*/
public static function __callStatic($name, $arguments)
{
switch ($name) {
case 'get':
list($redis_name, ) = $arguments ? $arguments : array('normal');
if (!isset(self::$redis_list[$redis_name])) {
$redis_list = Application::$configs['redis'];
if (isset($redis_list[$redis_name])) {
try {
$redis_handle = new Redis();
$connected = $redis_handle->pconnect($redis_list[$redis_name]['host'], $redis_list[$redis_name]['port'], 30, sprintf('%s_%s_%s', $redis_list[$redis_name]['host'], $redis_list[$redis_name]['port'], $redis_list[$redis_name]['db']));
if (false == $connected) {
throw new Exception(sprintf('can\'t connect %s redis %s', $redis_name, json_encode($redis_list[$redis_name])));
}
$selected = $redis_handle->select((int) $redis_list[$redis_name]['db']);
if (false == $selected) {
throw new Exception(sprintf('connect %s redis %s select db failed', $redis_name, json_encode($redis_list[$redis_name])));
}
self::$redis_list[$redis_name] = new self($redis_handle);
} catch (RedisException $e) {
throw new Exception($e->getMessage());
}
} else {
throw new Exception('no config data key `' . $redis_name . '`');
}
}
return self::$redis_list[$redis_name];
break;
//其他case 省略
//其他case 省略
default:
throw new Exception('RedisFactory unknown static method `' . $name . '`');
}
}
示例6: get_redis
/**
* 获取redis对象
*
* @return Redis
*/
public static function get_redis()
{
$redis_conf = $GLOBALS['config']['redis'];
$redis = new Redis();
$res = $redis->connect($redis_conf['host'], $redis_conf['port']);
return $redis;
}
示例7: cache
protected function cache()
{
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth('123456');
return $redis;
}
示例8: testIfCacheInitsWithOpenConnection
public function testIfCacheInitsWithOpenConnection()
{
$redis = new \Redis();
$redis->connect(REDIS_HOST, REDIS_PORT, REDIS_TIMEOUT_SECONDS);
$this->cache = new RedisCache($redis);
$this->assertKeysCanBeWrittenAndRead();
}
示例9: testGetMessageWithNoExistsMessage
/**
* Get message with message not found
*/
public function testGetMessageWithNoExistsMessage()
{
$this->redis->expects($this->once())->method('lPop')->with('foo.bar')->will($this->returnValue(false));
$adapter = new RedisAdapter();
$adapter->setRedis($this->redis)->setListKey('foo.bar');
$this->assertNull($adapter->getMessage());
}
示例10: getCacheAction
public function getCacheAction()
{
$redis = new Redis();
$redis->connect('127.0.0.1');
$page = $redis->get('page');
print_r($page);
}
示例11: class_init
public static function class_init()
{
/** @var Settings $settings */
$settings = resource(Settings::class);
self::$redis = new \Redis();
self::$redis->connect($settings->get("redis", "hostname"), $settings->get("redis", "connect_port"));
}
示例12: o_indirect_tsf
/**
*@author: JJyy
*@todo: get the indirect transform number for the user_id
* less than 30 days
*@param:
*
**/
function o_indirect_tsf($user_id, $channel_name, $timeset, $db)
{
$sql = "select count(order_id) as cn from order_info where order_status=1 and pay_status=2 and confirm_time>" . $timeset . " and confirm_time<" . ($timeset + 3600 * 30);
$row = $db->query($sql);
$cn = $row['cn'];
if ($cn == 0) {
//if within 30 days no order
return 0;
} else {
$rds = new Redis();
//get the keys for the user_id
$keys = $rds->keys("*:" . $user_id);
//string like: li9t209jm7mc6m4vmn88o5a7j0:1454035403.8093:10.10.10.29:baidu:0
$num = 0;
foreach ($keys as $k => $v) {
//the time must after timeset
//same user_id and different channel_name and time after than timeset
if ($v[1] > $timeset && $v[3] != $channel_name && $v[4] == $user_id) {
$sql = "select count(order_id) as cn from order_info where order_status=1 and pay_status=2 and confirm_time>" . $timeset . " and confirm_time<" . ($timeset + 3600 * 2);
$row = $db->query($sql);
$cn = $row['cn'];
$num += $cn;
}
}
return $num;
}
}
示例13: testBasics
public function testBasics()
{
if (extension_loaded('Redis')) {
$redis = new \Redis();
try {
$ok = @$redis->connect('127.0.0.1', 6379);
} catch (\Exception $e) {
$ok = false;
}
if (!$ok) {
$this->markTestSkipped('The ' . __CLASS__ . ' cannot connect to redis');
}
} else {
$this->markTestSkipped('The ' . __CLASS__ . ' requires the use of redis');
}
$cache = new RedisCache();
$cache->setRedis($redis);
// Test save
$cache->save('test_key', 'testing this out');
// Test contains to test that save() worked
$this->assertTrue($cache->contains('test_key'));
$cache->save('test_key1', 'testing this out', 20);
// Test contains to test that save() worked
$this->assertTrue($cache->contains('test_key1'));
// Test fetch
$this->assertEquals('testing this out', $cache->fetch('test_key'));
// Test delete
$cache->save('test_key2', 'test2');
$cache->delete('test_key2');
$this->assertFalse($cache->contains('test_key2'));
$this->assertEquals($redis, $cache->getRedis());
}
示例14: close
/**
* Отключение от Redis.
*
* @return void
* @access public
* @static
*/
public static function close()
{
if (static::$redis) {
static::$redis->close();
static::$redis = null;
}
}
示例15: createRedis
public function createRedis()
{
$redis = new \Redis();
$redis->connect($this->getOption('host'), $this->getOption('port'));
$redis->setOption(\Redis::OPT_PREFIX, $this->getOption('prefix'));
return $redis;
}