Using Redis with PHP

Source: Internet
Author: User
Tags php redis redis tutorial

PHP Use Redis

Installation

started inPHPused inRedisago, we need to make sure we've installed theRedisServices andPHP RedisDrive, and you can use it properly on your machinePHP. next let's installPHP RedisDrive: For:Https://github.com/nicolasff/phpredis.

PHP installation Redis Extended

/usr/local/php/bin/phpize #php the path after installation

./configure--with-php-config=/usr/local/php/bin/php-config

Make && make install

Modify php.ini file

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

Restart after installation is complete PHP-FPM or Apache . View The phpinfo information to see the redis extension.

connected to Redis Service

<?php //Connect to localRedisService $redis = new Redis (); $redis->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 sucessfully
Server is Running:pong

Redis PHP String ( string ) Example

<?php //Connect to localRedisService $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379);
echo "Connection to server sucessfully";
   //SetRedisString Data
$redis->set ("Tutorial-name", "Redis Tutorial");
   // get the stored data and output
echo "Stored string in Redis::" + jedis.get ("Tutorial-name");
?>

Execute the script with the output as:

Connection to Server sucessfully
Stored string in Redis:: Redis Tutorial

Redis PHP list ( list ) Example

<?php //Connect to localRedisService$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 the 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 sucessfully
Stored string in Redis::
Redis
Mongodb
Mysql

Redis PHP Keys Example

<?php //Connect to localRedisService$redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379);
echo "Connection to server sucessfully";
   // get the data and output
$arList = $redis->keys ("*");
echo "Stored keys in Redis::"
Print_r ($arList);
?>

Execute the script with the output as:

Connection to Server sucessfully
Stored string in Redis::
Tutorial-name
Tutorial-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.