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