Redis PHP连接操作
安裝
要在PHP程序中使用Redis,首先需要確保 Redis 的PHP驅(qū)動(dòng)程序和 PHP 安裝設(shè)置在機(jī)器上。可以查看 PHP教程 教你如何在機(jī)器上安裝PHP?,F(xiàn)在,讓我們來(lái)看看一下如何設(shè)置 Redis 的PHP驅(qū)動(dòng)程序。
需要從 github 上資料庫(kù): https://github.com/nicolasff/phpredis 下載 phpredis。下載完成以后,將文件解壓縮到 phpredis 目錄。在 Ubuntu 上安裝這個(gè)擴(kuò)展,可使用如下圖所示的命令來(lái)安裝。
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 notecho "Server is running: " . $redis->ping(); ?>當(dāng)執(zhí)行程序時(shí),會(huì)產(chǎn)生下面的結(jié)果:
Connection to server sucessfully Server is running: PONGRedis的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 itecho "Stored string in redis:: " . $redis.get("tutorial-name"); ?>當(dāng)執(zhí)行程序時(shí),會(huì)產(chǎn)生下面的結(jié)果:
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); ?>當(dāng)執(zhí)行程序時(shí),會(huì)產(chǎn)生下面的結(jié)果:
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); ?>當(dāng)執(zhí)行程序時(shí),會(huì)產(chǎn)生下面的結(jié)果:
Connection to server sucessfully Stored string in redis:: tutorial-name tutorial-list轉(zhuǎn)載于:https://www.cnblogs.com/favana/p/5584740.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Redis PHP连接操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 6/12 Sprint2 看板和燃尽图
- 下一篇: PHP面向对象(三)