PHP uses redis____php

Source: Internet
Author: User
Tags mongodb php redis redis redis tutorial

installation

Before starting to use Redis in PHP, we need to make sure that the Redis service and the PHP Redis drive are installed and that PHP is available on your machine. Next let's install the PHP redis driver: Download the address: https://github.com/phpredis/phpredis/releases.

PHP installation Redis extension

The following operations need to be done in the downloaded Phpredis directory:

$ wget https://github.com/phpredis/phpredis/archive/3.1.4.tar.gz
$ cd phpredis-3.1.4                      # Enter Phpredis directory
$/ Usr/local/php/bin/phpize              # PHP installation after the path
$/configure--with-php-config=/usr/local/php/bin/php-config
$ Make && make install
Modifying php.ini files

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
Reboot php-fpm or Apache after Setup completes. Viewing phpinfo information, you can see the redis extension.


connecting to the Redis service

<?php
    //Connect the local Redis service
   $redis = new Redis ();
   $redis->connect (' 127.0.0.1 ', 6379);
   echo "Connection to Server sucessfully";
         To see if the service is running
   echo "Server is running:". $redis->ping ();
? >
Execute the script, and the output is:

Connection to server sucessfully
server is Running:pong
Redis PHP String (string) instance

<?php
   //Connect the local Redis service
   $redis = new Redis ();
   $redis->connect (' 127.0.0.1 ', 6379);
   echo "Connection to Server sucessfully";
   Sets the Redis string data
   $redis->set ("Tutorial-name", "Redis Tutorial");
   Gets the stored data and outputs the
   echo "Stored string in Redis::". $redis->get ("Tutorial-name");
? >
Execute the script, and the output is:

Connection to server sucessfully
Stored string in Redis:: Redis Tutorial
redis PHP List (list) instance

<?php
   //Connect the local Redis service
   $redis = new Redis ();
   $redis->connect (' 127.0.0.1 ', 6379);
   echo "Connection to Server sucessfully";
   Store the data into the list
   $redis->lpush ("Tutorial-list", "Redis");
   $redis->lpush ("Tutorial-list", "Mongodb");
   $redis->lpush ("Tutorial-list", "Mysql");
   Gets the stored data and outputs the
   $arList = $redis->lrange ("Tutorial-list", 0, 5);
   echo "Stored string in Redis";
   Print_r ($arList);
? >
Execute the script, and the output is:

Connection to server sucessfully
Stored string in Redis
Mysql
Mongodb
Redis

Redis PHP Keys instance

<?php
   //Connect the local Redis service
   $redis = new Redis ();
   $redis->connect (' 127.0.0.1 ', 6379);
   echo "Connection to Server sucessfully";
   Get data and output
   $arList = $redis->keys ("*");
   echo "Stored keys in Redis::";
   Print_r ($arList);
? >
Execute the script, and the output is:

Connection to server sucessfully
Stored string in Redis::
tutorial-name
tutorial-list

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.