在php程序中使用redis之前,需要确保在机器上安装了redis的php驱动程序和php环境。可以先在将php电脑上并配置好环境。本文就和大家分享php链接redis的方法代码,希望能帮助到大家。
安装
现在,让我们看看如何设置Redis PHP驱动程序。
从github库下载phpredis=> http://github.com/nicolasff/phpredis。 当下载它之后,提取文件到phpredis目录。在Ubuntu上,安装以下扩展。
cd phpredis sudo phpize sudo ./configure sudo make sudo make install
现在,将“modules”文件夹的内容复制并粘贴到PHP扩展目录中,并在php.ini中添加以下行。
extension = redis.so
现在,Redis PHP安装完成!
立即学习“PHP免费学习笔记(深入)”;
使用连接到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(); ?>PHP
当程序执行时,将产生以下结果。
Connection to server sucessfully Server is running: PONG
Redis PHP字符串示例
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"); ?>执行上面代码,将生成以下结果 -
Ke361是一个开源的淘宝客系统,基于最新的ThinkPHP3.2版本开发,提供更方便、更安全的WEB应用开发体验,采用了全新的架构设计和命名空间机制, 融合了模块化、驱动化和插件化的设计理念于一体,以帮助想做淘宝客而技术水平不高的朋友。突破了传统淘宝客程序对自动采集商品收费的模式,该程序的自动 采集模块对于所有人开放,代码不加密,方便大家修改。集成淘点金组件,自动转换淘宝链接为淘宝客推广链接。K
Connection to server sucessfully Stored string in redis:: Redis tutorial
Redis php列表示例
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键示例
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
相关推荐:










