Redis (五 php与redis的结合使用)
2017-02-04 15:57
387 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。
1 安装phpredis扩展
(1)下载phpredis源码,hhtps://github.com/owlient/phpredis/downloads
(2)解压 tar -xzvf phpredis.tar.gz
(3)编译安装
cd phpredis
/usr/local/PHP/bin/phpize
./configure --with-php-config=config path(你的配置文件的路径)
make
make install
(4)修改php.ini
添加 extension=Redis.so
用phpinfo查看或者php -m | grep redis 查看redis扩展是否可用
(5)testCon.php
测试是否可以连接成功
1 安装phpredis扩展
(1)下载phpredis源码,hhtps://github.com/owlient/phpredis/downloads
(2)解压 tar -xzvf phpredis.tar.gz
(3)编译安装
cd phpredis
/usr/local/PHP/bin/phpize
./configure --with-php-config=config path(你的配置文件的路径)
make
make install
(4)修改php.ini
添加 extension=Redis.so
用phpinfo查看或者php -m | grep redis 查看redis扩展是否可用
(5)testCon.php
测试是否可以连接成功
$redis = new Redis(); $con = $redis->connect('127.0.0.1', 6379); $redis->set('key', 'val'); $val = $redis->get('key'); var_dump($val); $redis->close(); (6)实例分析
发布微博是可能很多人同时操作,并发很多,单纯mysql可能会导致连接数过多,而使服务宕机,可以使用redis的list把消息放入队列,然后用cron定时刷新到mysql数据库,降低mysql的并发 用到的文件: redis.php 实现消息放入redis weibo.php 实现消息获取,放入数据库 function.php 实现消息获取的模拟 内容如下: 1 redis.php include_once 'function.php'; $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $webInfo = array( 'uid' => get_uid(), 'content' => get_content(), 'timstamp' => time() ); $redis->lPush('weiboList', json_encode($webInfo)); $redis->close(); 2 function.php function get_uid() { return rand(1, 1000); } //echo get_uid(); function get_content() { return 'test - ' . getmygid() . rand(1, 10) . 'nj'; } 3 weibo.php include_once "function.php"; class weibo { private $host = '127.0.0.1'; 4000 private $user = '...'; private $pass = '...'; private $db = 'test'; private $table = 'weibo'; // 实现发布微博的功能 public function post() { echo $this->connect(); } public function connect() { $con = mysqli_connect($this->host, $this->user, $this->pass, $this->db); if (!$con) { echo "mysql connect failed"; echo "<br/>"; echo "error no is :" . mysqli_errno(); echo "<br/>"; echo "error is :" . mysqli_error(); echo "<br/>"; } $uid = get_uid(); $content = get_content(); $sql = "insert into " . $this->table . "(uid,content) " . " values(" . $uid . ",'" . $content . "')"; $res = mysqli_query($con, $sql); if (!$res) { return 'pub failed'; } return 'pub success'; } }
相关文章推荐
- PHP的Laravel框架结合MySQL与Redis数据库的使用部署
- Redis (五 php与redis的结合使用)
- php 与redis 结合 使用predis
- PHP的Laravel框架结合MySQL与Redis数据库的使用部署
- php与redis使用经验和redis可视化
- 使用jQuery结合PHP和MySQL读取和发表评论
- 结合 PHP 使用 HTML 表单
- php单件模式结合命令链模式使用说明
- 结合使用PHP和RSS
- heredoc 不能结合 php中的循环使用?
- while循环+list+each结合使用 php
- 个人技术作品 - PHP 水印类及上传图片加水印结合使用的API及使用实例
- 结合 PHP 使用 HTML 表单
- 使用Highcharts结合PHP与Mysql生成饼状图
- 初探PureMVC:使用PHP+MySQL+Flex结合PureMVC框架做了个Flex留言本
- php-redis客户端使用方法
- 结合PHP使用HTML表单(3)
- 结合使用 PHP 和 RSS
- 一个比较经典的php批量删除程序,与smarty相互结合使用
- phpredis使用方法