Using Redis with PHP

Source: Internet
Author: User
Tags php redis redis tutorial

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
  1. /usr/local/PHP/bin/phpize #php安装后的路径
  2. ./Configure --with-php-config=/usr/local/PHP/Bin /php-config
  3. Make && make install
Modify the php.ini file
    1. VI /usr/local/PHP/lib/php. INI

Add the following content:

    1. Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20090626"
    2. 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
  1. <?php
  2. //连接本地的 Redis 服务
  3. $redis = new Redis();
  4. $redis->Connect(' 127.0.0.1 ', 6379);
  5. echo "Connection to Server sucessfully";
  6. //See if the service is running
  7. Echo "Server is running:"+ $redisping();
  8. ?>

Execute the script with the output as:

    1. Connection to server sucessfully
    2. Server is running: PONG
Redis PHP String (string) instance
  1. <?php
  2. //连接本地的 Redis 服务
  3. $redis = new Redis();
  4. $redis->Connect(' 127.0.0.1 ', 6379);
  5. echo "Connection to Server sucessfully";
  6. //Set Redis string data
  7. $redis, set("Tutorial-name", "Redis Tutorial");
  8. //Get stored data and output
  9. echo "Stored string in Redis::" + Jedis. Get("Tutorial-name");
  10. ?>

Execute the script with the output as:

    1. Connection to server sucessfully
    2. Stored string in redis:: redis tutorial
Redis PHP list (list) instance
  1. <?php
  2. //连接本地的 Redis 服务
  3. $redis = new Redis();
  4. $redis->Connect(' 127.0.0.1 ', 6379);
  5. echo "Connection to Server sucessfully";
  6. //Store data in the list
  7. $redis-Lpush("Tutorial-list", "Redis");
  8. $redis-Lpush("Tutorial-list", "Mongodb");
  9. $redis-Lpush("Tutorial-list", "Mysql");
  10. //Get stored data and output
  11. $arList = $redislrange("Tutorial-list", 0 ,5);
  12. echo "Stored string in Redis::"
  13. Print_r($arList);
  14. ?>

Execute the script with the output as:

    1. Connection to server sucessfully
    2. Stored string in redis::
    3. Redis
    4. Mongodb
    5. Mysql
Redis PHP Keys Instance
  1. <?php
  2. //连接本地的 Redis 服务
  3. $redis = new Redis();
  4. $redis->Connect(' 127.0.0.1 ', 6379);
  5. echo "Connection to Server sucessfully";
  6. //Get data and output
  7. $arList = keys("*") $redis;
  8. echo "Stored keys in Redis::"
  9. Print_r($arList);
  10. ?>

Execute the script with the output as:

    1. Connection to server sucessfully
    2. Stored string in redis::
    3. Tutorial-name
    4. 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.