需要从 github 上资料库: https://github.com/nicolasff/phpredis 下载 phpredis。下载完成以后,将文件解压缩到 phpredis 目录。在 ubuntu 上安装这个扩展,可使用如下图所示的命令来安装。
cd phpredissudo phpizesudo ./configuresudo makesudo make install
现在,复制和粘贴“modules”文件夹的内容复制到php扩展目录中,并在 php.ini 中添加以下几行。
extension = redis.so
现在 redis 和 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();?>
当执行程序时,会产生下面的结果:
connection to server sucessfullyserver 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);?>
当执行程序时,会产生下面的结果:
connection to server sucessfullystored 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 sucessfullystored string in redis::redismongodbmysql
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 sucessfullystored string in redis::tutorial-nametutorial-list