Redis PHP连接操作

Stella981
• 阅读 611

安装

要在PHP程序中使用Redis,首先需要确保 Redis 的PHP驱动程序和 PHP 安装设置在机器上。可以查看 PHP教程 教你如何在机器上安装PHP。现在,让我们来看看一下如何设置 Redis 的PHP驱动程序。

需要从 github 上资料库: https://github.com/nicolasff/phpredis 下载 phpredis。下载完成以后,将文件解压缩到 phpredis 目录。在 Ubuntu 上安装这个扩展,可使用如下图所示的命令来安装。

cd phpredis
sudo phpize
sudo ./configure
sudo make
sudo make install

现在,复制和粘贴“modules”文件夹的内容复制到PHP扩展目录中,并在 php.ini 中添加以下几行。

extension = redis.so

现在 Redis 和 PHP 安装完成

连接到Redis服务器

<?php
   //Connecting to Redis server on localhost
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   echo "Connection to server sucessfully";
   //check whether server is running or not
   echo "Server is running: " . $redis->ping();
?>

当执行程序时,会产生下面的结果:


Connection to server sucessfully
Server is running: PONG

Redis的PHP字符串实例

<?php
   //Connecting to Redis server on localhost
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   echo "Connection to server sucessfully";
   //set the data in redis string
   $redis->set("tutorial-name", "Redis tutorial");
   // Get the stored data and print it
   echo "Stored string in redis:: " . $redis.get("tutorial-name");
?>

当执行程序时,会产生下面的结果:


Connection to server sucessfully
Stored string in redis:: Redis tutorial

Redis的PHP列表示例

<?php
   //Connecting to Redis server on localhost
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   echo "Connection to server sucessfully";
   //store data in redis list
   $redis->lpush("tutorial-list", "Redis");
   $redis->lpush("tutorial-list", "Mongodb");
   $redis->lpush("tutorial-list", "Mysql");
   // Get the stored data and print it
   $arList = $redis->lrange("tutorial-list", 0 ,5);
   echo "Stored string in redis:: "
   print_r($arList);
?>

当执行程序时,会产生下面的结果:


Connection to server sucessfully
Stored string in redis::
Redis
Mongodb
Mysql

Redis的PHP键例

<?php
   //Connecting to Redis server on localhost
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   echo "Connection to server sucessfully";
   // Get the stored keys and print it
   $arList = $redis->keys("*");
   echo "Stored keys in redis:: "
   print_r($arList);
?>

当执行程序时,会产生下面的结果:


Connection to server sucessfully
Stored string in redis::
tutorial-name
tutorial-list
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
Redis(二十九)PHP 使用 Redis
安装开始在PHP中使用Redis前,我们需要确保已经安装了redis服务及PHPredis驱动,且你的机器上能正常使用PHP。接下来让我们安装PHPredis驱动:下载地址为:https://github.com/phpredis/phpredis/releases(https://www.oschina.
Wesley13 Wesley13
2年前
thinkphp+redis实现秒杀,缓存等功能
秒杀是商城常见功能phpredis是最常见的秒杀功能1,安装redis,根据自己的php版本安装对应的redis扩展首先查看phpinfo();php环境信息2,下载redishttps://windows.php.net/downloads/pecl/snaps/redis/https://windows.php.ne
Wesley13 Wesley13
2年前
Linux下PHP安装Redis扩展
pecl.php.net,PECL库是一个PHP扩展,提供一个目录的所有已知的扩展和托管设备下载PHP扩展,PHP很多扩展都可以在这里面找到。今天主要讲解下Linux下php安装redis扩展,附有windows下redis扩展安装。1、PHP redis(https://www.oschina.net/action/GoToLink?url
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
2年前
Laravel使用redis保存SESSION
Laravel使用redis保存SESSION首先确认服务器已经安装redis服务,php安装了redis扩展.1.打开config/database.php。在redis配置项中增加session的连接'session''host'
Wesley13 Wesley13
2年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
PHP+jQuery寥寥几行代码轻松实现百度搜索那样的无刷新PJAX的分页列表和导航链接
!(https://static.oschina.net/uploads/space/2016/1208/171419_U00R_561214.png)PHP寥寥几行代码轻松实现百度搜索那样的分页列表和导航链接,某些语言的拥趸哭晕在厕所.<?php$apparray('db_prefix''