Redis 简明教程

Redis - PHP

在 PHP 程序中使用 Redis 之前,需要确保计算机上已设置好 Redis PHP 驱动和 PHP。可以查看 PHP 教程,以在计算机上安装 PHP。

Before you start using Redis in your PHP programs, you need to make sure that you have Redis PHP driver and PHP set up on the machine. You can check PHP tutorial for PHP installation on your machine.

Installation

现在,让我们检查如何设置 Redis PHP 驱动。

Now, let us check how to set up Redis PHP driver.

需要从 github 存储库 https://github.com/nicolasff/phpredis 下载 phpredis。下载后,将文件解压到 phpredis 目录。在 Ubuntu 上,安装以下扩展程序。

You need to download the phpredis from github repository https://github.com/nicolasff/phpredis. Once you’ve downloaded it, extract the files to phpredis directory. On Ubuntu, install the following extension.

cd phpredis
sudo phpize
sudo ./configure
sudo make
sudo make install

现在,将“modules”文件夹的内容复制并粘贴到 PHP 扩展目录,并在 php.ini 中添加以下行。

Now, copy and paste the content of “modules” folder to the PHP extension directory and add the following lines in php.ini.

extension = redis.so

现在,Redis PHP 安装已完成

Now, your Redis PHP installation is complete

Connect to Redis Server

<?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();
?>

此程序执行时将会显示以下结果。

When the program is executed, it will produce the following result.

Connection to server sucessfully
Server is running: PONG

Redis PHP String Example

<?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");
?>

执行上述程序后,它将产生以下结果。

When the above program is executed, it will produce the following result.

Connection to server sucessfully
Stored string in redis:: Redis tutorial

Redis php List Example

<?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);
?>

执行上述程序后,它将产生以下结果。

When the above program is executed, it will produce the following result.

Connection to server sucessfully
Stored string in redis::
Redis
Mongodb
Mysql

Redis PHP Keys Example

<?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);
?>

此程序执行时将会显示以下结果。

When the program is executed, it will produce the following result.

Connection to server sucessfully
Stored string in redis::
tutorial-name
tutorial-list