Redis PHP

Redis PHP

在开始使用Redis在你的PHP程序之前,你需要确保你的机器上已经安装了Redis PHP驱动和PHP。你可以查看PHP教程来安装PHP

安装

现在,让我们看一下如何设置Redis PHP驱动。

你需要从github仓库下载phpredis。https://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(); 
?>

当程序被执行时,它将产生以下结果。

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 itarList = 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程