Using Redis with PHP
Using Redis for PHP installation
Before we start using Redis in PHP, we need to make sure that we have the Redis service and the PHP Redis driver installed and that PHP is working properly on your machine. Next, let's install the PHP redis driver:https://github.com/phpredis/phpredis/releases.
PHP Installation Redis Extensions
The following operations need to be done in the downloaded Phpredis directory:
$ wget HTTPS:Github.com/phpredis/phpredis/archive/2.2.4.tar.gz$ CD Phpredis-2.2.7 # Enter the Phpredis directory$ /usr/local /php/bin/ phpize # PHP installation path $ ./--with-php- config=/usr/local/< Span class= "PLN" >php/bin/php -config$ make && make install
If you are a PHP7 version, you need to download the specified branch:
-b php7 https://github.com/phpredis/phpredis.git
Modify the php.ini file
/usr/local/PHP/lib/php. INI
Add the following content:
="/usr/local/php/lib/php/extensions/no-debug-zts-20090626"extension=Redis. so
Restart PHP-FPM or Apache after the installation is complete. View the phpinfo information to see the Redis extension.
Connect to the Redis service
<?Php//connect to local Redis service $redis = new redis (); $redis Span class= "pun" >->connect ( ' 127.0.0.1 ' , 6379 echo "Connection to server sucessfully" ; //see if the service is running echo "Server is running:" $redis ->ping (); ?>
Execute the script with the output as:
Connection to server sucessfullyserveris running: PONG
Redis PHP String (string) instance
<?PhpConnect to a local Redis service$redis= New Redis();$redis-Connect(' 127.0.0.1 ', 6379 echo " Connection to Server sucessfully "; //set Redis string data $redis ->set ( "tutorial-name" , " Redis Tutorial "); //get stored data and output echo "Stored string in Redis:: " .->get ( "Tutorial-name" ?>
Execute the script with the output as:
Connection to server sucessfullyStoredstringin redis::redis Tutorial
Redis PHP list (list) instance
<?PhpConnect to a local Redis service$redis= New Redis();$redis-Connect(' 127.0.0.1 ', 6379);Echo"Connection to Server sucessfully"; Storing data in a 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 with the output as:
Connection to server sucessfullyStoredstringin redisredisMongodbMysql
Redis PHP Keys Instance
<?PhpConnect to a local Redis service$redis= New Redis (); $redis ->connect (, 6379); "Connection to server sucessfully" ; //get data and output $arList = $redis ->keys ( "*" echo "Stored keys in Redis::" ; $arList ?>
Execute the script with the output as:
Connection to server sucessfullyStoredstringin redis::Tutorial-nametutorial -list
Go to PHP using Redis