Exemple #1
1
 function getUser($uid)
 {
     $key = self::$prefix . 'user_' . $uid;
     $ret = $this->redis->get($key);
     $info = unserialize($ret);
     return $info;
 }
Exemple #2
1
 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;
 }
Exemple #3
0
 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;
 }
Exemple #4
0
function connredis()
{
    static $r = null;
    if ($r !== null) {
        return $r;
    }
    $r = new redis();
    $r->connect('localhost');
    return $r;
}
Exemple #5
0
 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);
 }
Exemple #6
0
 public function testRedis()
 {
     $this->assertTrue(in_array('redis', get_loaded_extensions()), '缺少redis extension');
     $cfg = (include ROOT_PATH . '/../config/sysconfig.php');
     $params = array('server', 'port', 'timeout');
     foreach ($params as $param) {
         $this->assertArrayHasKey($param, $cfg['redisConf'], 'redis缺少' . $param . '配置');
     }
     $r = new redis();
     $this->assertTrue($r->connect($cfg['redisConf']['server'], $cfg['redisConf']['port'], $cfg['redisConf']['timeout']), 'redis连接失败');
 }
Exemple #7
0
 public function __construct($config)
 {
     $redis = new \redis();
     if (isset($config['PCONNECT'])) {
         $redis->pconnect($config['HOST'], $config['PORT'], $config['TIMEOUT']);
     } else {
         $redis->connect($config['HOST'], $config['PORT'], $config['TIMEOUT']);
     }
     $redis->setOption(\redis::OPT_SERIALIZER, \redis::SERIALIZER_NONE);
     $redis->select($config['DB']);
     $this->redis = $redis;
     self::$config = $config;
 }
Exemple #8
0
 public function test_redis($loop_times)
 {
     $tbname = $this->tbname;
     $dbname = $this->dbname;
     $start_time = microtime(true);
     for ($i = 0; $i < $loop_times; $i++) {
         ##   for ($j = 0; $j < 5; $j++){
         $redis = new redis();
         $redis->connect('127.0.0.1', 6379);
         $redis->hGetAll("{$tbname}");
         ##    }
     }
     $end_time = microtime(true);
     $rstime = $end_time - $start_time;
     return $rstime;
 }
Exemple #9
0
 public function __construct($name, $age, $sex = 'male', $class = 'PHP Developer')
 {
     $this->sid = redis::getInstance()->incr('Student_SID_INCR_KEY');
     $this->name = $name;
     $this->age = $age;
     $this->sex = $sex;
     $this->class = $class;
 }
 /**
  * Flush all existing items at the server
  *
  * CacheRedis::truncate() immediately invalidates all existing items.
  * If using multiple databases, items in other databases are not affected.
  *
  * @return bool Returns true on success or false on failure.
  */
 function truncate()
 {
     try {
         return $this->redis->flushDB();
     } catch (RedisException $e) {
         return $this->status = false;
     }
 }
Exemple #11
0
 protected function _cmd_getNodeList($fd, $req)
 {
     $nodeList = $this->redis->sMembers(self::KEY_NODE_LIST);
     array_walk($nodeList, function (&$a) {
         $a = self::KEY_NODE_INFO . ':' . $a;
     });
     $nodeInfo = $this->redis->mget($nodeList);
     $this->sendResult($fd, 0, '', $nodeInfo);
 }
Exemple #12
0
 /**
  * 单例模型,获取实例的方法
  * @return [Object] [redis操作对象的一个实例,该实例已经完成了连接操作]
  */
 public static function getInstance()
 {
     if (!self::$_instance instanceof self) {
         $conf = Registry::get('global_conf');
         self::$_instance = new redis();
         self::$_r = new Credis_Client($conf['redis_server_url'], $conf['redis_server_port']);
     }
     return self::$_instance;
 }
Exemple #13
0
 /**
  * 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 __construct()
 {
     $redis = new \redis();
     $redis->pconnect('127.0.0.1', '6379', '0.0');
     $this->redis = $redis;
     $this->access_token = $redis->get('access_token');
     if (!$this->access_token) {
         $appId = Config::get('wechat.app_id');
         $appSecret = Config::get('wechat.secret');
         $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appId . '&secret=' . $appSecret . '';
         $res = Curl::get($url);
         $arr = json_decode($res);
         $access_token = $arr->access_token;
         $time_out = $arr->expires_in;
         $this->redis->set('access_token', $access_token);
         $this->redis->expire('access_token', $time_out);
         $this->access_token = $access_token;
     }
 }
Exemple #15
0
 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} ");
 }
Exemple #16
0
<?php

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->hset("id:1", "time", "2");
$redis->hset("id:1", "time", "2");
$redis->hset("id:1", "time", "2");
$redis->hset("id:1", "time", "2");
$redis->hset("id:1", "time", "2");
$redis->hset("id:1", "time", "2");
$redis->hset("id:1", "time", "2");
$redis->hset("id:1", "time", "2");
$redis->hset("id:1", "time", "2");
Exemple #17
0
 public function rem($queue, $from_name)
 {
     $this->redis->lRem($queue, $from_name, 1);
 }
Exemple #18
0
<?php

/* 这里替换为连接的实例host和port */
$host = "127.0.0.1";
$port = 6379;
/* 这里替换为实例id和实例password,没有则注释掉 */
/*$user = "******";
$pwd = "xxxxxxx";*/
$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_put_contents('./redis.json', '');
$it = NULL;
/* Initialize our iterator to NULL */
$Redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
/* retry when we get no keys back */
while ($keys = $Redis->scan($it)) {
    $out = '';
    foreach ($keys as $key) {
        //echo $Redis->type($key),"\n";
        $arr = array();
        $type = $Redis->type($key);
        $expire = $Redis->ttl($key);
        switch ($type) {
            case $Redis::REDIS_STRING:
Exemple #19
0
 function getUser($userid)
 {
     $ret = $this->redis->get($userid);
     $info = unserialize($ret);
     return $info;
 }
Exemple #20
0
 public function redis()
 {
     var_dump(redis::set('name', 'enychen'));
     var_dump(redis::get('name'));
 }
<?php

//链接redis数据库
$redis = new redis();
$redis->connect("localhost", 6379);
header("content-type:text/html;charset=utf-8");
//读取字典文件
$lines = file("/tmp/words.txt");
/*echo "<pre>";

	var_dump($lines);	

	echo "</pre>";*/
//获取到一个数组,数组的每一个元素就是每一行话
//批量处理这个数组的每个元素就是处理每一行话
foreach ($lines as $line) {
    $str = "";
    $line = rtrim($line, "\n");
    for ($i = 0; $i < strlen($line); $i++) {
        $str .= mb_substr($line, $i, 1, 'utf-8');
        //将分好的词插入到有序集合当中
        $redis->zadd('words', 0, $str);
    }
}
$words = $redis->zrange("words", 0, -1);
echo "<pre>";
var_dump($words);
echo "</pre>";
 public function __construct($host = '127.0.0.1', $port = 6379, $timeout = 0.0)
 {
     $redis = new \redis();
     $redis->connect($host, $port, $timeout);
     $this->redis = $redis;
 }
Exemple #23
0
#!/usr/bin/php

<?php 
$redis = new redis();
$result = $redis->connect('127.0.0.1', 6379);
if (!$result) {
    die("Redis连接失败;\n");
}
$set_result = $redis->set('test1', 'test1-info-value');
var_dump($set_result);
if (!$set_result) {
    die("Redis set 操作失败;\n");
} else {
    die("Redis set 操作成功;\n");
}
<?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);
Exemple #25
0
 *  Mac 安 装 量
 */
for ($i = 0; $i < count($install); $i++) {
    $mac_arr[] = explode(",", $install[$i]);
}
foreach ($mac_arr as $a => $b) {
    if (in_array("安装", $b)) {
        $mac[] = $b;
    }
}
foreach ($mac as $k => $v) {
    $name[] = substr($v[3], strrpos($v[3], '.'));
    $uname = array_unique($name);
    if (in_array(".exe", $uname)) {
        $re[$v[3]][] = $v;
    }
}
foreach ($re as $k => $v) {
    for ($i = 0; $i < count($v); $i++) {
        $mbr[$k] = count($v);
        $_mbr[] = count($v) . ',' . $k . ',' . $v[$i][1] . ',' . $data;
    }
}
for ($i = 0; $i < count($_mbr); $i++) {
    $_mbr_arr[] = explode(",", $_mbr[$i]);
}
$redis = new redis();
$redis->connect("127.0.0.1", 6379);
foreach ($_mbr_arr as $key => $val) {
    $redis->hset("install", $val[2], json_encode($val));
}
function connectRedis($host, $port, $pwd = None)
{
    $r = new redis();
    $r->connect($host, $port);
    if ($pwd) {
        $r->auth($pwd);
    }
    return $r;
}
Exemple #27
0
<?php

header("content-type:text/html;charset=utf-8");
$redis = new redis();
$result = $redis->connect('127.0.0.1', 6379);
$mywatchkey = $redis->get("mywatchkey");
$rob_total = 10;
//抢购数量
if ($mywatchkey < $rob_total) {
    $redis->watch("mywatchkey");
    $redis->multi();
    //设置延迟,方便测试效果。
    sleep(5);
    //插入抢购数据
    $redis->hSet("mywatchlist", "user_id_" . mt_rand(1, 9999), time());
    $redis->set("mywatchkey", $mywatchkey + 1);
    $rob_result = $redis->exec();
    if ($rob_result) {
        $mywatchlist = $redis->hGetAll("mywatchlist");
        echo "抢购成功!<br/>";
        echo "剩余数量:" . ($rob_total - $mywatchkey - 1) . "<br/>";
        echo "用户列表:<pre>";
    } else {
        echo "手气不好,再抢购!";
        exit;
    }
}
?>
  
Exemple #28
0
    {
    }
    function get($key)
    {
    }
}
class myredis
{
    function __construct($redis)
    {
        $this->_redis = $redis;
    }
    public function __call($name, $args)
    {
        return call_user_func_array(array($this->_redis, $name), $args);
    }
}
$redis = new redis();
$redis->connect('127.0.0.1');
$redis->set('key1', rand());
$redis->get('key1');
$myredis = new myredis($redis);
$myredis->set('key2', rand());
$myredis->get('key2');
$redis2 = new redis2();
$redis2->connect('127.0.0.1');
$redis2->set('key3', rand());
$redis2->get('key3');
$myredis2 = new myredis($redis2);
$myredis2->set('key4', rand());
$myredis2->get('key4');
Exemple #29
0
 public function update($key, $data, $ttl = 0)
 {
     return $this->_redis->set($key, $data, $ttl);
 }
<?php

header("content-type:text/html;charset=utf-8");
//链接redis数据库
$redis = new redis();
$redis->connect("localhost", 6379);
$key = $_GET['key'];
$rank = $redis->zrank("words", $key);
$search = $redis->zrange("words", $rank, -1);
foreach ($search as $word) {
    if (strstr($word, $key)) {
        $data[] = $word;
    }
}
//echo "<pre>";
//var_dump($data);
//echo "</pre>";
echo json_encode($data);