PHP連結redis的方法代碼

來源:互聯網
上載者:User
在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('127.0.0.1', 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('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"); ?>

執行上面代碼,將產生以下結果 -

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

執行上面代碼,將產生以下結果 -

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

執行上面代碼,將產生以下結果 -

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.