溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

redis連接php

發(fā)布時(shí)間:2020-07-20 11:39:36 來源:網(wǎng)絡(luò) 閱讀:511 作者:liangey 欄目:web開發(fā)
安裝
在PHP程序中使用Redis,需要確保我們有Redis的PHP驅(qū)動(dòng)程序和PHP安裝設(shè)置在機(jī)器上??梢圆榭碢HP教程教你如何在機(jī)器上安裝PHP?,F(xiàn)在,讓我們來看看一下如何設(shè)置Redis的PHP驅(qū)動(dòng)程序。
需要從github上資料庫https://github.com/nicolasff/phpredis下載phpredis。下載了它以后,將文件解壓縮到phpredis目錄。在Ubuntu上安裝這個(gè)擴(kuò)展,如下圖所示。
cd phpredis
sudo phpize
sudo ./configure
sudo make
sudo make install
現(xiàn)在,復(fù)制和粘貼“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í),會(huì)產(chǎn)生下面的結(jié)果:
Connection to server sucessfully
Server is running: PONG
Redis的PHP字符串實(shí)例
<?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:: " + jedis.get("tutorial-name");
?>
當(dāng)執(zhí)行程序時(shí),會(huì)產(chǎn)生下面的結(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);
?>
當(dāng)執(zhí)行程序時(shí),會(huì)產(chǎn)生下面的結(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);
?>
當(dāng)執(zhí)行程序時(shí),會(huì)產(chǎn)生下面的結(jié)果:
Connection to server sucessfully
Stored string in redis::
tutorial-name
tutorial-list


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI