PHP连接Redis大屏查看

发布于:2017年03月17日 已被阅读

在php程序中使用Redis之前,需要确保在机器上安装了Redis的PHP驱动程序和PHP环境。可以先在将PHP电脑上并配置好环境。

安装

现在,让我们看看如何设置Redis PHP驱动程序。
从github库下载phpredis=> http://github.com/nicolasff/phpredis。 当下载它之后,提取文件到phpredis目录。在Ubuntu上,安装以下扩展。

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

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

extension = redis.soShell

现在,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(); ?>PHP

当程序执行时,将产生以下结果。

Connection to server sucessfully 
Server is running: PONGShell

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"); ?>PHP

执行上面代码,将生成以下结果 -


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

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); ?>PHP

执行上面代码,将生成以下结果 -


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

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); ?>PHP

执行上面代码,将生成以下结果 -

Connection to server sucessfully 
Stored string in redis:: tutorial-name 
tutorial-listPHP


本文属作者原创,转载请注明出处:易百教程 » PHP连接Redis


最新发布
linux下svn提交忽略某些文件... (173)
使用批处理来批量更新、提交SVN... (135)
linux查看目录文件大小命令 (145)
linux tar打包压缩排除某个... (133)
Linux tar压缩和解压 (192)
SVN子命令add用法浅析 (129)
热门博文
网友FBI探案:马蓉iPad惊人发现... (43343)
霍金携手俄罗斯富豪耗资1亿美元寻找外... (4746)
如何才能查看PHP内置函数源代码... (1209)
微信支付开发当前URL未注册的解决方... (573)
《谁为爱情买单》中的经典面试 ... (440)
让虚拟主机也用上SVN:适用于个人的... (394)
精华博文
[推荐]Centos7 安装配置 SVN (156)
easyswoole框架安装 (173)
php开启pecl的支持(推荐) (157)
1-10个恋爱表现:男朋友爱你程度到... (163)
女生喜欢你的10个程度,到第六个就可... (140)
Eclipse 没有Server选项... (211)
友情链接
我来忙 (110)