安裝
現在,讓我們看看如何設置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: PONGRedis 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 tutorialRedis 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 MysqlRedis 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
相關推薦:
PHP操作Redis實例講解
一個php+redis的簡單實例分享
Redis在PHP的一些使用方法
以上就是PHP鏈接redis的方法代碼的詳細內容,更多請關注 其它相關文章!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答