📅  最后修改于: 2020-11-26 08:22:05             🧑  作者: Mango
在PHP程序中开始使用Redis之前,需要确保已在计算机上设置了Redis PHP驱动程序和PHP。您可以检查PHP教程以在您的计算机上安装PHP。
现在,让我们检查如何设置Redis PHP驱动程序。
您需要从github仓库https://github.com/nicolasff/phpredis下载phpredis。下载后,将文件解压缩到phpredis目录。在Ubuntu上,安装以下扩展名。
cd phpredis
sudo phpize
sudo ./configure
sudo make
sudo make install
现在,将“ modules”文件夹的内容复制并粘贴到PHP扩展目录,并在php.ini中添加以下行。
extension = redis.so
现在,您的Redis PHP安装已完成
connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
//check whether server is running or not
echo "Server is running: ".$redis->ping();
?>
执行该程序时,将产生以下结果。
Connection to server sucessfully
Server is running: PONG
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
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
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