English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Before starting to use Redis in PHP, We need to make sure that the redis service and PHP redis driver are installed, and that PHP can be used normally on your machine. Next, let's install the PHP redis driver: download address is:https://github.com/phpredis/phpredis/releases.
The following operations need to be completed in the downloaded phpredis directory:
$ wget https://github.com/phpredis/phpredis/archive/3.1.4.tar.gz $ cd phpredis-3.1.4 # Enter the phpredis directory $ /usr/local/php/bin/phpize # php installation path $ ./configure --with-php-config=/usr/local/php/bin/php-config $ make && make install
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
Restart php after installation-fpm or apache. Check phpinfo information to see the redis extension.
<?php //Connect to the local Redis server $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server successfully"; //Check if the service is running echo "Server is running: " . $redis->ping(); ?>
Execute the script, the output is:
Connection to server successfully Server is running: PONG
<?php //Connect to the local Redis server $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server successfully"; //Set redis string data $redis->set("tutorial-name, "Redis tutorial"); // Get stored data and output echo "Stored string in redis:: " . $redis->get("tutorial-name}); ?>
Execute the script, the output is:
Connection to server successfully Stored string in redis:: Redis tutorial
<?php //Connect to the local Redis server $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server successfully"; //Store data in the list $redis->lpush("tutorial-list", "Redis"); $redis->lpush("tutorial-list", "Mongodb"); $redis->lpush("tutorial-list", "Mysql"); // Get stored data and output $arList = $redis->lrange("tutorial-list", 0 ,5); echo "Stored string in redis"; print_r($arList); ?>
Execute the script, the output is:
Connection to server successfully Stored string in redis Mysql Mongodb Redis
<?php //Connect to the local Redis server $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server successfully"; // Get data and output $arList = $redis->keys("*"); echo "Stored keys in redis:: "; print_r($arList); ?>
Execute the script, the output is:
Connection to server successfully Stored string in redis:: tutorial-name tutorial-list