Using Redis with PHP

Source: Internet
Author: User

From:http://www.runoob.com/redis/redis-php.html

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/nicolasff/phpredis.

PHP Installation Redis Extensions
/usr/local/PHP/bin/#php安装后的路径./--with-php-Config =/usr/local/PHP/bin/php-&& make Install  
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:: " ..?>                

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 (, " Mysql "); //get stored data and output  $arList = $redis ->lrange ( "tutorial-list" , 0 , 5 echo  "Stored string in Redis::"  Print_r ( Span class= "PLN" > $arList ); ?>             

Execute the script with the output as:

Connection to server sucessfullyStoredstringin redis::redisMongodbMysql 
Redis PHP Keys Instance
<?PhpConnect to a local Redis service$redis= New redis ();  $redis -> ( ' 127.0.0.1 ' ,  6379 echo  "Connection to server sucessfully" ; Span class= "com" >//get data and output  $arList = $redis ->keys ( "*"  echo  "Stored keys in Redis::"  Print_r ( $arList ?>             

Execute the script with the output as:

Connection to server sucessfullyStoredstringin redis::Tutorial-nametutorial -list          

Using Redis with PHP

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.