开始在 PHP 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 PHP redis 驱动,且你的机器上能正常使用 PHP。 接下来让我们安装 PHP redis 驱动:下载地址为:https://github.com/phpredis/phpredis/releases 。
1.PHP安装redis扩展
以下操作需要在下载的 phpredis 目录中完成:
$ wget https://github.com/phpredis/phpredis/archive/3.1.4.tar.gz
$ cd phpredis-3.1.4 # 进入 phpredis 目录
$ /usr/local/php/bin/phpize # php安装后的路径
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make && make install
修改php.ini文件
vi /usr/local/php/lib/php.ini
增加如下内容:
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20090626"
extension=redis.so
安装完成后重启php-fpm 或 apache。查看phpinfo信息,就能看到redis扩展。如图:
2.PHP 使用 Redis
连接到 redis 服务
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; //查看服务是否运行 echo "Server is running: " . $redis->ping(); ?>
执行脚本,输出结果为:
Connection to server sucessfully
Server is running: PONG
Redis PHP String(字符串) 实例
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; //设置 redis 字符串数据 $redis->set("tutorial-name", "Redis tutorial"); // 获取存储的数据并输出 echo "Stored string in redis:: " . $redis->get("tutorial-name"); ?>
执行脚本,输出结果为:
Connection to server sucessfully
Stored string in redis:: Redis tutorial
Redis PHP List(列表) 实例
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; //存储数据到列表中 $redis->lpush("tutorial-list", "Redis"); $redis->lpush("tutorial-list", "Mongodb"); $redis->lpush("tutorial-list", "Mysql"); // 获取存储的数据并输出 $arList = $redis->lrange("tutorial-list", 0 ,5); echo "Stored string in redis"; print_r($arList); ?>
执行脚本,输出结果为:
Connection to server sucessfully
Stored string in redis
Mysql
Mongodb
Redis
Redis PHP Keys 实例
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; // 获取数据并输出 $arList = $redis->keys("*"); echo "Stored keys in redis:: "; print_r($arList); ?>
执行脚本,输出结果为:
Connection to server sucessfully
Stored string in redis::
tutorial-name
tutorial-list
3.简单实例
下面实例简单的介绍了向redis的表中添加成员并进行简单的管理操作
添加redis成员
页面展示redisForm.html
<html> <head> <head> <title>redis form 提交</title> </head> </head> <body> <form action="reg.php" method="post"> 用户名: <input type="text" name="username" /><br/> 密码: <input type="password" name="password" /><br/> 年龄: <input type="text" name="age" /><br/> <input type="submit" value="注册" /> <input type="reset" value="重新填写" /> </form> </body> </html>
上面的html来让用户进行添加或者修改redis记录,并将数据提交至reg.php,以下为reg.php的展示
<?php require("redis.php"); $username = $_POST['username']; $password = MD5($_POST['password']); //进行md5加密 $age = $_POST['age']; //自动增加函数incr() $uid = $redis->incr("userid"); //添加用户、hash $res = $redis->hmset("user:".$uid,array("uid"=>$uid,"username"=>$username,"password"=>$password,"age"=>$age)); $redis->rpush("uid",$uid); if($res){ header("location:list.php"); //添加成功则跳转至list.php来显示当前的redis记录列表 } ?>
展示redis记录
以下为list.php,将html与处理逻辑放在一个页面,其中的有分页的简单逻辑,编辑和删除都将记录的id传递到对应的del.php或者edit.php文件进行处理
<html> <head> <title>redis 列表页</title> </head> <?php require("redis.php"); //获取分页--首先知道总数,每页条数,当前页数,页总数 //用户总数 $count = $redis->lsize("uid"); //每页条数 $page_size = 3; //当前页数 $page_num = (!empty($_GET['page']))?$_GET['page']:1; //页总数 $page_count = ceil($count/$page_size); $ids = $redis->lrange("uid",($page_num-1)*$page_size,(($page_num-1)*$page_size+$page_size-1)); $ids = $redis->lrange("uid",($page_num-1)*$page_size,(($page_num-1)*$page_size+$page_size-1)); //取出当前的所有用户 foreach($ids as $val){ $data[] = $redis->hgetall("user:".$val); } $data = array_filter($data);//过滤数组中的空元素 ?> <body> <table border=1> <tr> <th>uid</th> <th>username</th> <th>age</th> <th>操作</th> </tr> <?php foreach($data as $val){ ?> <tr> <td><?php echo $val['uid']; ?></td> <td><?php echo $val['username']; ?></td> <td><?php echo $val['age']; ?></td> <td><a href="del.php?id=<?php echo $val['uid']?>">删除</a> <a href="edit.php?id=<?php echo $val['uid']?>">编辑</a></td> </tr> <?php } ?> <tr> <td colspan="4"> <a href="?page=<?php echo (($page_num-1)<=1)?1:($page_num-1); ?>">上一页</a> <a href="?page=<?php echo (($page_num+1)>=$page_count)?$page_count:($page_num+1); ?>">下一页</a> <a href="?page=1">首页</a> <a href="?page=<?php echo $page_count; ?>">尾页</a> 当前<?php echo $page_num; ?>页 总共<?php echo $page_count; ?>页 总共<?php echo $count; ?>用户 </td> </tr> </table> </body> </html>
修改redis记录
以下为edit.php文件,进行查询对应的数据展示可以进行修改提交到doedit.php
<html> <?php require("redis.php"); $uid = $_GET['id']; $data = $redis->hgetall("user:".$uid); ?> <body> <form action="doedit.php" method="post"> <input type="hidden" value="<?php echo $data['uid']; ?>" name="uid" /> 用户名: <input type="text" name="username" value="<?php echo $data['username']; ?>" /><br/> 年龄: <input type="text" name="age" value="<?php echo $data['age']; ?>" /><br/> <input type="submit" value="修改" /> </form> </body> </html>
doedit.php 进行修改的操作
<?php require("redis.php"); $uid = $_POST['uid']; $username = $_POST['username']; $age = $_POST['age']; $res = $redis->hmset("user:".$uid,array("username"=>$username,"age"=>$age)); if($res){ header("location:list.php"); }else{ header("location:edit.php"); } ?>
删除操作 del.php
<?php require("redis.php"); $uid = $_GET['id']; $res = $redis->del("user:".$uid); $redis->lrem("uid",$uid); if($res){ header("location:list.php"); } ?>
更多PHP使用redis实例:https://ittxx.cn/view/182
参考网址:
http://www.runoob.com/redis/redis-php.html
https://blog.csdn.net/qjwcn/article/details/45293035
https://blog.csdn.net/wang_wxd/article/details/53809959
转载请注明: ITTXX.CN--分享互联网 » PHP使用Redis简单实例
最后更新:2019-04-11 17:49:08