鍍金池/ 教程/ 大數(shù)據(jù)/ PHP連接Redis
C#連接Redis
Redis基準(zhǔn)
Redis數(shù)據(jù)類型
Windows下安裝Redis
Redis事務(wù)
Redis腳本
Redis配置
Redis客戶端連接
Redis字符串
Redis管道
PHP連接Redis
Redis快速入門(mén)
Redis命令
Redis教程
Redis哈希
Redis環(huán)境安裝配置
Redis分區(qū)
Redis備份
Redis發(fā)送訂閱
Redis連接
Redis服務(wù)器
Redis集合
Redis鍵命令
Redis HyperLogLog
Java連接Redis
Redis列表

PHP連接Redis

在php程序中使用Redis之前,需要確保在機(jī)器上安裝了Redis的PHP驅(qū)動(dòng)程序和PHP環(huán)境。可以先在將PHP電腦上并配置好環(huán)境。

安裝

現(xiàn)在,讓我們看看如何設(shè)置Redis PHP驅(qū)動(dòng)程序。
從github庫(kù)下載phpredis=> http://github.com/nicolasff/phpredis。 當(dāng)下載它之后,提取文件到phpredis目錄。在Ubuntu上,安裝以下擴(kuò)展。

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

現(xiàn)在,將“modules”文件夾的內(nèi)容復(fù)制并粘貼到PHP擴(kuò)展目錄中,并在php.ini中添加以下行。

extension = redis.so

現(xiàn)在,Redis PHP安裝完成!

使用連接到Redis服務(wù)器

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

當(dāng)程序執(zhí)行時(shí),將產(chǎn)生以下結(jié)果。

Connection to server sucessfully 
Server is running: PONG

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

執(zhí)行上面代碼,將生成以下結(jié)果 -

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

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

執(zhí)行上面代碼,將生成以下結(jié)果 -

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('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); 
?>

執(zhí)行上面代碼,將生成以下結(jié)果 -

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