public function __construct($host = '127.0.0.1', $port = 6379, $timeout = 0.0, $pass = null) { $redis = new \redis(); $redis->pconnect($host, $port, $timeout); if ($pass) { $redis->auth($pass); } $this->redis = $redis; }
public function __construct() { if (self::$reObj === null) { if (!extension_loaded('redis')) { die('服务器不支持redis扩展!'); } $reObj = new \redis(); $config = \System\Entrance::config('REDIS_CONFIG'); $host = $config['host']; $port = $config['port']; $auth = $config['auth']; if ($config['connectType'] == 'connect') { $connect = $reObj->connect($host, $port); } else { $connect = $reObj->pconnect($host, $port); } if (!empty($auth)) { if (!$reObj->auth($auth)) { die('redis密码错误!'); } } if (!$connect) { die('redis服务器连接失败!'); } self::$reObj = $reObj; } return self::$reObj; }
public function info() { $server = $this->model->one(intval($this->input->get('id'))); $redis = new \redis(); if ($redis->connect($server->host, $server->port, 30) == false) { echo "Redis '{$server->host}' Connected Failed. \n"; exit($redis->getLastError()); } if ($server->password) { if ($redis->auth($server->password) == false) { echo "Redis '{$server->host}' Password Is Incorrect. \n"; exit($redis->getLastError()); } } $data['keyspace'] = $redis->info('keyspace'); $data['memory'] = $redis->info('memory'); $data['clients'] = $redis->info('clients'); //$data['slow'] = $redis->slowLog('get',10); $redis->close(); $result = $this->db('bs')->query('select * from en_server_redis_data where server_id=' . $server->id . ' order by created asc ')->result(); $data['chart_memory_data'] = []; $data['chart_memory_time'] = []; $data['chart_connection_data'] = []; $data['chart_connection_time'] = []; foreach ($result as $rs) { $data['chart_memory_data'][] = round($rs->memory / 1024 / 1024, 2); $data['chart_memory_time'][] = date('H:i', $rs->created); $data['chart_connection_data'][] = intval($rs->connection); $data['chart_connection_time'][] = date('H:i', $rs->created); } $data['server'] = $server; $this->output->view('monitor/redis/server_info', $data); }
/** * Construct * * Do not use this directly. You can use getInstance() instead. * @param string $url url of Redis * @return void */ function __construct($url) { //$config['url'] = 'redis://localhost:6379/1'; $config['url'] = is_array($url) ? reset($url) : $url; if (!class_exists('Redis')) { return $this->status = false; } try { $this->redis = new Redis(); $info = parse_url($url); $this->redis->connect($info['host'], $info['port'], 0.15); if (isset($info['user']) || isset($info['pass'])) { $this->redis->auth(isset($info['user']) ? $info['user'] : $info['pass']); } if (isset($info['path']) && ($dbnum = intval(substr($info['path'], 1)))) { $this->redis->select($dbnum); } return $this->status = true; } catch (RedisException $e) { return $this->status = false; } }
/** * redis 链接 */ private function redis() { $cfg = $this->getRedisHost(); try { $redis = new redis(); $redis->pconnect($cfg['host'], $cfg['port'], 0); if ($cfg['auth']) { $redis->auth($cfg['auth']); } $this->redis = $redis; $this->redis->ping(); } catch (Exception $e) { $this->log($e->getMessage() . ' now retrying'); sleep(1); //如果报错等一秒再重连 $this->redis(); } }
public function redisData() { $result = $this->db('bs')->query("select * from en_server_redis")->result(); foreach ($result as $server) { $redis = new \redis(); if ($redis->connect($server->host, $server->port, 30) == false) { echo "Redis '{$server->host}' Connected Failed. \n"; continue; } if ($server->password) { if ($redis->auth($server->password) == false) { echo "Redis '{$server->host}' Password Is Incorrect. \n"; $redis->close(); continue; } } $memory = $redis->info('memory'); $clients = $redis->info('clients'); $redis->close(); $this->db('bs')->insert('en_server_redis_data', ['server_id' => $server->id, 'memory' => $memory['used_memory'], 'connection' => $clients['connected_clients'], 'created' => time()]); } $del = time() - 3600 * 24; $this->db('bs')->delete('en_server_redis_data', " created<{$del} "); }
<?php header('Access-Control-Allow-Origin:*'); $IM = 'Im.malu.me'; $IM_ROOM = array('C1' => 0, 'C2' => 0, 'M1' => 0, 'M2' => 0, 'AI' => 0); $redis = new redis(); $redis->connect('localhost', 6379); $result = $redis->auth('REDIS_PASS'); if ($result) { $pipe = $redis->multi(Redis::PIPELINE); foreach ($IM_ROOM as $key => $value) { $pipe->lrange($IM . '_' . $key, 0, 0); } $result = $pipe->exec(); //var_dump($result); } if ($result) { $i = 0; foreach ($IM_ROOM as $key => $value) { if ($result[$i]) { $r = explode('-', $result[$i][0]); $IM_ROOM[$key] = $r[0]; } $i++; } //var_dump($IM_ROOM); } $json_result = json_encode($IM_ROOM); echo $json_result; //$rlocal->setex($IM,2,$json_result);
function connectRedis($host, $port, $pwd = None) { $r = new redis(); $r->connect($host, $port); if ($pwd) { $r->auth($pwd); } return $r; }
} /* 这里替换为连接的实例host和port */ $host = "xxxxxxxxxxx.m.cnhza.kvstore.aliyuncs.com"; //改成你自己的 $port = 6379; /* 这里替换为实例id和实例password */ $user = "******"; //改成你自己的 $pwd = "xxxxx"; //改成你自己的 $Redis = new redis(); //$Redis->pconnect('127.0.0.1', 6379); if ($Redis->pconnect($host, $port) == false) { die($Redis->getLastError()); } if ($Redis->auth($user . ":" . $pwd) == false) { die($Redis->getLastError()); } $file = fopen("redis.json", "r"); $arr = array(); while (!feof($file)) { //echo fgets($file). "<br />"; $arr = json_decode(fgets($file), true); $Redis->delete($arr['key']); switch ($arr['type']) { case $Redis::REDIS_STRING: echo "string\n"; $Redis->set($arr['key'], $arr['val']); setTimeout($Redis, $arr['key'], $arr['expire']); break; case $Redis::REDIS_HASH: