開端在PHP中利用Redis前,要確保曾經安裝了redis效勞及PHPredis驅動,且你的呆板上能正常利用PHP。
PHP安裝redis擴大年夜
/usr/local/php/bin/phpize#php安裝後的道路
./configure--with-php-config=/usr/local/php/bin/php-config
make&&makeinstall
修改php.ini文件
vi/usr/local/php/lib/php.ini
增加如下內容:
extension_dir=\"/usr/local/php/lib/php/extensions/no-debug-zts-20090626\"
extension=redis.so
安裝實現後重啟php-fpm或apache。檢查phpinfo信息,就能看到redis擴大年夜。
連接到redis效勞
<?php
//連接當地的Redis效勞
$redis=newRedis();
$redis->connect(\'127.0.0.1\',6379);
echo\"Connectiontoserversucessfully\";
//檢查效勞能否運轉
echo\"Serverisrunning:\".$redis->ping();
?>
履行劇本,輸出成果為:
Connectiontoserversucessfully
Serverisrunning:PONG
RedisPHPString(字元串)實例
<?php
//連接當地的Redis效勞
$redis=newRedis();
$redis->connect(\'127.0.0.1\',6379);
echo\"Connectiontoserversucessfully\";
//設置redis字元串數據
$redis->set(\"tutorial-name\",\"Redistutorial\");
//獲取存儲的數據並輸出
echo\"Storedstringinredis::\".jedis.get(\"tutorial-name\");
?>
履行劇本,輸出成果為:
Connectiontoserversucessfully
Storedstringinredis::Redistutorial
RedisPHPList(列表)實例
<?php
//連接當地的Redis效勞
$redis=newRedis();
$redis->connect(\'127.0.0.1\',6379);
echo\"Connectiontoserversucessfully\";
//存儲數據到列表中
$redis->lpush(\"tutorial-list\",\"Redis\");
$redis->lpush(\"tutorial-list\",\"Mongodb\");
$redis->lpush(\"tutorial-list\",\"Mysql\");
//獲取存儲的數據並輸出
$arList=$redis->lrange(\"tutorial-list\",0,5);
echo\"Storedstringinredis::\"
print_r($arList);
?>
履行劇本,輸出成果為:
Connectiontoserversucessfully
Storedstringinredis::
Redis
Mongodb
Mysql
RedisPHPKeys實例
<?php
//連接當地的Redis效勞
$redis=newRedis();
$redis->connect(\'127.0.0.1\',6379);
echo\"Connectiontoserversucessfully\";
//獲取數據並輸出
$arList=$redis->keys(\"*\");
echo\"Storedkeysinredis::\"
print_r($arList);
?>
履行劇本,輸出成果為:
Connectiontoserversucessfully
Storedstringinredis::
tutorial-name
tutorial-list