求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
 
 
     
   
    全部     工程实例     标准规范     名校讲义     知识库    
 
 

Redis教程
Redis快速入门
Redis环境安装
Redis配置
Redis数据类型
Redis命令
Redis键
Redis字符串
Redis哈希
Redis列表
Redis集合
Redis有序集合
Redis发布订阅
Redis事务
Redis脚本
Redis连接
Redis服务器
Redis备份
Redis安全
Redis基准
Redis客户端连接
Redis管道传输
Redis分区
Redis Java连接操作
Redis PHP连接操作
 
 

Redis PHP连接操作

    您可以捐助,支持我们的公益事业。

金额: 1元 10元 50元

姓名:

邮件:

电话:

公司:

说明:

认证码: 验证码,看不清楚?请点击刷新验证码 必填



 
 捐助

安装

在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:: " + jedis.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


    您可以捐助,支持我们的公益事业。

金额: 1元 10元 50元

姓名:

邮件:

电话:

公司:

说明:

认证码: 验证码,看不清楚?请点击刷新验证码 必填



 
 捐助