首頁  >  文章  >  後端開發  >  PHP連結redis的方法程式碼

PHP連結redis的方法程式碼

小云云
小云云原創
2018-03-07 09:08:093533瀏覽

在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安裝完成!

使用連接到Redis伺服器

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect(&#39;127.0.0.1&#39;, 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字串範例

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect(&#39;127.0.0.1&#39;, 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 sucessfully 
Stored string in redis:: Redis tutorial

Redis php清單範例

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect(&#39;127.0.0.1&#39;, 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鍵範例

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect(&#39;127.0.0.1&#39;, 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

相關推薦:

#PHP運算Redis實例講解

一個php+redis的簡單實例分享

Redis在PHP的一些使用方法

以上是PHP連結redis的方法程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn