PHP连接Redis

<p>在php程序中使用Redis之前,需要确保在机器上安装了Redis的PHP驱动程序和PHP环境。可以先在将PHP电脑上并配置好环境。</p><p>安装</p><p>现在,让我们看看如何设置Redis PHP驱动程序。</p><p>从github库下载phpredis=&gt; 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>现在,将&quot;modules&quot;文件夹的内容复制并粘贴到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">&lt;?php //ConnectingtoRedisserveronlocalhost $redis=newRedis(); $redis-&gt;connect(&#39;127.0.0.1&#39;,6379); echo&quot;Connectiontoserversucessfully&quot;; //checkwhetherserverisrunningornot echo&quot;Serverisrunning:&quot;.$redis-&gt;ping(); ?&gt;</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">&lt;?php //ConnectingtoRedisserveronlocalhost $redis=newRedis(); $redis-&gt;connect(&#39;127.0.0.1&#39;,6379); echo&quot;Connectiontoserversucessfully&quot;; //setthedatainredisstring $redis-&gt;set(&quot;tutorial-name&quot;,&quot;Redistutorial&quot;); //Getthestoreddataandprintit echo&quot;Storedstringinredis::&quot;.$redis→get(&quot;tutorial-name&quot;); ?&gt;</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">&lt;?php //ConnectingtoRedisserveronlocalhost $redis=newRedis(); $redis-&gt;connect(&#39;127.0.0.1&#39;,6379); echo&quot;Connectiontoserversucessfully&quot;; //storedatainredislist $redis-&gt;lpush(&quot;tutorial-list&quot;,&quot;Redis&quot;); $redis-&gt;lpush(&quot;tutorial-list&quot;,&quot;Mongodb&quot;); $redis-&gt;lpush(&quot;tutorial-list&quot;,&quot;Mysql&quot;); //Getthestoreddataandprintit $arList=$redis-&gt;lrange(&quot;tutorial-list&quot;,0,5); echo&quot;Storedstringinredis::&quot;; print_r($arList); ?&gt;</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">&lt;?php //ConnectingtoRedisserveronlocalhost $redis=newRedis(); $redis-&gt;connect(&#39;127.0.0.1&#39;,6379); echo&quot;Connectiontoserversucessfully&quot;; //Getthestoredkeysandprintit $arList=$redis-&gt;keys(&quot;*&quot;); echo&quot;Storedkeysinredis::&quot; print_r($arList); ?&gt;</pre><p>PHP</p><p>执行上面代码,将生成以下结果 -</p><pre class="brush:bash;toolbar:false">Connectiontoserversucessfully Storedstringinredis:: tutorial-name tutorial-list</pre>
RangeTime:0.009080s
RangeMem:207.54 KB
返回顶部 留言