Go to PHP using Redis

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.