Redis(二十九)PHP 使用 Redis

Stella981
• 阅读 588

安装

开始在 PHP 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 PHP redis 驱动,且你的机器上能正常使用 PHP。 接下来让我们安装 PHP redis 驱动:下载地址为:https://github.com/phpredis/phpredis/releases

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扩展。

Redis(二十九)PHP 使用 Redis

连接到 redis 服务

<?php
    //连接本地的 Redis 服务
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   echo "Connection to server successfully";
         //查看服务是否运行
   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 successfully";
   //设置 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 successfully";
   //存储数据到列表中
   $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 successfully";
   // 获取数据并输出
   $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
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
java中使用redis缓存数据库操作
开始在Java中使用Redis前,首先需要确保已经安装了redis服务及Javaredis驱动,且你的机器上能正常使用Java。安装配置Redis:maven配置如下<dependency<groupIdredis.clients</groupId<artifactIdjedi
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年前
Java 使用 Redis
安装开始在Java中使用Redis前,我们需要确保已经安装了redis服务及Javaredis驱动,且你的机器上能正常使用Java。Java的安装配置可以参考我们的 Java开发环境配置(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fw
Stella981 Stella981
2年前
Redis学习(二十八)Java 使用 Redis
Java使用Redis安装开始在Java中使用Redis前,我们需要确保已经安装了redis服务及Javaredis驱动,且你的机器上能正常使用Java。Java的安装配置可以参考我们的 Java开发环境配置(https://www.oschina.net/action/GoToLin
Stella981 Stella981
2年前
PHP Redis扩展无法加载的问题解决方法
最近在工作中需要使用PHP访问Redis,从https://github.com/phpredis/phpredis(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fphpredis%2Fphpredis)下载了phpredis,并且按照官方的说明进行了安装,安
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年前
Redis PHP连接操作
安装要在PHP程序中使用Redis,首先需要确保Redis的PHP驱动程序和PHP安装设置在机器上。可以查看PHP教程教你如何在机器上安装PHP。现在,让我们来看看一下如何设置Redis的PHP驱动程序。需要从github上资料库:https://github.com/nicolasff/phpredis(https: