<p>在php程序中使用Redis之前,需要确保在机器上安装了Redis的PHP驱动程序和PHP环境。可以先在将PHP电脑上并配置好环境。</p><p>安装</p><p>现在,让我们看看如何设置Redis PHP驱动程序。</p><p>从github库下载phpredis=> http://github.com/nicolasff/phpredis。 当下载它之后,提取文件到phpredis目录。在Ubuntu上,安装以下扩展。</p><p>cd phpredis</p><p>sudo phpize</p><p>sudo ./configure</p><p>sudo make</p><p>sudo make install</p><p>Shell</p><p>现在,将"modules"文件夹的内容复制并粘贴到PHP扩展目录中,并在php.ini中添加以下行。</p><p>extension = redis.so</p><p>Shell</p><p>现在,Redis PHP安装完成!</p><p>使用连接到Redis服务器</p><pre class="brush:php;toolbar:false"><?php
//ConnectingtoRedisserveronlocalhost
$redis=newRedis();
$redis->connect('127.0.0.1',6379);
echo"Connectiontoserversucessfully";
//checkwhetherserverisrunningornot
echo"Serverisrunning:".$redis->ping();
?></pre><p>当程序执行时,将产生以下结果。</p><p>Connection to server sucessfully</p><p>Server is running: PONG</p><p>Shell</p><p>Redis PHP字符串示例</p><pre class="brush:php;toolbar:false"><?php
//ConnectingtoRedisserveronlocalhost
$redis=newRedis();
$redis->connect('127.0.0.1',6379);
echo"Connectiontoserversucessfully";
//setthedatainredisstring
$redis->set("tutorial-name","Redistutorial");
//Getthestoreddataandprintit
echo"Storedstringinredis::".$redis→get("tutorial-name");
?></pre><p>执行上面代码,将生成以下结果 -</p><p>Connection to server sucessfully</p><p>Stored string in redis:: Redis tutorial</p><p>Java</p><p>Redis php列表示例</p><pre class="brush:php;toolbar:false"><?php
//ConnectingtoRedisserveronlocalhost
$redis=newRedis();
$redis->connect('127.0.0.1',6379);
echo"Connectiontoserversucessfully";
//storedatainredislist
$redis->lpush("tutorial-list","Redis");
$redis->lpush("tutorial-list","Mongodb");
$redis->lpush("tutorial-list","Mysql");
//Getthestoreddataandprintit
$arList=$redis->lrange("tutorial-list",0,5);
echo"Storedstringinredis::";
print_r($arList);
?></pre><p>执行上面代码,将生成以下结果 -</p><p>Connection to server sucessfully</p><p>Stored string in redis::</p><p>Redis</p><p>Mongodb</p><p>Mysql</p><p>PHP</p><p>Redis php键示例</p><pre class="brush:php;toolbar:false"><?php
//ConnectingtoRedisserveronlocalhost
$redis=newRedis();
$redis->connect('127.0.0.1',6379);
echo"Connectiontoserversucessfully";
//Getthestoredkeysandprintit
$arList=$redis->keys("*");
echo"Storedkeysinredis::"
print_r($arList);
?></pre><p>PHP</p><p>执行上面代码,将生成以下结果 -</p><pre class="brush:bash;toolbar:false">Connectiontoserversucessfully
Storedstringinredis::
tutorial-name
tutorial-list</pre>