installation
Before starting to use Redis in PHP, we need to make sure that the Redis service and the PHP Redis drive are installed and that PHP is available on your machine. Next let's install the PHP redis driver: Download the address: https://github.com/phpredis/phpredis/releases.
PHP installation Redis extension
The following operations need to be done in the downloaded Phpredis directory:
$ wget https://github.com/phpredis/phpredis/archive/3.1.4.tar.gz
$ cd phpredis-3.1.4 # Enter Phpredis directory
$/ Usr/local/php/bin/phpize # PHP installation after the path
$/configure--with-php-config=/usr/local/php/bin/php-config
$ Make && make install
Modifying php.ini files
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
Reboot php-fpm or Apache after Setup completes. Viewing phpinfo information, you can see the redis extension.
connecting to the Redis service
<?php
//Connect the local Redis service
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
echo "Connection to Server sucessfully";
To see if the service is running
echo "Server is running:". $redis->ping ();
? >
Execute the script, and the output is:
Connection to server sucessfully
server is Running:pong
Redis PHP String (string) instance
<?php
//Connect the local Redis service
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
echo "Connection to Server sucessfully";
Sets the Redis string data
$redis->set ("Tutorial-name", "Redis Tutorial");
Gets the stored data and outputs the
echo "Stored string in Redis::". $redis->get ("Tutorial-name");
? >
Execute the script, and the output is:
Connection to server sucessfully
Stored string in Redis:: Redis Tutorial
redis PHP List (list) instance
<?php
//Connect the local Redis service
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
echo "Connection to Server sucessfully";
Store the data into the list
$redis->lpush ("Tutorial-list", "Redis");
$redis->lpush ("Tutorial-list", "Mongodb");
$redis->lpush ("Tutorial-list", "Mysql");
Gets the stored data and outputs the
$arList = $redis->lrange ("Tutorial-list", 0, 5);
echo "Stored string in Redis";
Print_r ($arList);
? >
Execute the script, and the output is:
Connection to server sucessfully
Stored string in Redis
Mysql
Mongodb
Redis
Redis PHP Keys instance
<?php
//Connect the local Redis service
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
echo "Connection to Server sucessfully";
Get data and output
$arList = $redis->keys ("*");
echo "Stored keys in Redis::";
Print_r ($arList);
? >
Execute the script, and the output is:
Connection to server sucessfully
Stored string in Redis::
tutorial-name
tutorial-list