Redis Php
Here's the translated English HTML content (with code blocks and HTML tags preserved):
## Installation
Before starting to use Redis in PHP, we need to ensure that the Redis service and PHP Redis driver are installed, and that PHP is functioning properly on your machine. Next, let's install the PHP Redis driver: Download it from [**https://github.com/phpredis/phpredis/releases**](https://github.com/phpredis/phpredis/releases).
### Install Redis extension for PHP
The following operations need to be performed in the downloaded phpredis directory:
$ wget https://github.com/phpredis/phpredis/archive/3.1.4.tar.gz $ tar zxvf 3.1.4.tar.gz # Extract $ cd phpredis-3.1.4 # Enter phpredis directory $ /usr/local/php/bin/phpize # Path after PHP installation $ ./configure --with-php-config=/usr/local/php/bin/php-config $ make && make install
### Modify php.ini file
vi /usr/local/php/lib/php.ini
Add the following content:
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20090626" extension=redis.so
After installation, restart php-fpm or apache. Check the phpinfo information to see the Redis extension.
!(#)
* * *
## Connect to Redis service
## Example
connect('127.0.0.1', 6379); echo"Connection to server successfully"; echo"Server is running: " . $redis->ping(); ?>
Execution output:
Connection to server successfully Server is running: PONG
* * *
## Redis PHP String Example
## Example
connect('127.0.0.1', 6379); echo"Connection to server successfully"; $redis->set("tutorial-name", "Redis tutorial"); echo"Stored string in redis:: " . $redis->get("tutorial-name"); ?>
Execution output:
Connection to server successfully Stored string in redis:: Redis tutorial
* * *
## Redis PHP List Example
## Example
connect('127.0.0.1', 6379); echo"Connection to server successfully"; $redis->lpush("tutorial-list", "Redis"); $redis->lpush("tutorial-list", "Mongodb"); $redis->lpush("tutorial-list", "Mysql"); $arList = $redis->lrange("tutorial-list", 0 ,5); echo"Stored string in redis"; print_r($arList); ?>
Execution output:
Connection to server successfully Stored string in redis MysqlMongodbRedis
* * *
## Redis PHP Keys Example
## Example
connect('127.0.0.1', 6379); echo"Connection to server successfully"; $arList = $redis->keys("*"); echo"Stored keys in redis:: "; print_r($arList); ?>
Execution output:
Connection to server successfully Stored string in redis:: tutorial-name tutorial-list
YouTip