Using Redis in PHP

Source: Internet
Author: User

First, make sure you have a Redis service

# redis-cli -vredis-cli 3.2.8
# redis-server -vRedis server v=3.2.8 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=e034df79550b120

Then make sure that PHP has added the Redis extension

# php -m |grep redisredis

Then you create the Redis class

<?phpclass Red {    static private $redis = NULL;    private $_red = NULL;    private $_return_data = NULL;    static public function create() {        if(self::$redis) {            return Red::$redis;           }                self::$redis = new self;        return self::$redis;    }       public function __call($func, $params) {        if ($func == 'multi') {            $this->_return_data = $this->_red->multi($params[0]);        } else {            $this->_return_data = call_user_func_array(array(&$this->_red, $func), $params);        }           return $this->_return_data;    }       private function __construct() {        $this->_red = new Redis();         $this->_red->connect(C("REDIS_HOST"),C("REDIS_PORT"));        $this->_red->select(C('REDIS_DB') ?: 0);        return Red::$redis;    }   }

Configuring Redis in Config.php

'REDIS_HOST'                =>  '127.0.0.1','REDIS_PROT'                =>  6379,

With Redis, you can improve query speed, and some unchanging queries can be saved to Redis for quick querying. For example, query city information, save to Redis, do not have to view the database every time.

Get Zone Data Public Function Get_map () {Vendor (' redis.red ');        $redis = Red::create ();        $data = $redis->get (' China:area ');            if (! $data) {$areaModel = M (' China_area ');            $p = $areaModel->field (' Id,name,level ')->where ([' Level ' = + 1])->select ();  foreach ($p as $k + $v) {$c = $areaModel->field (' Id,name,level ')->where ([' Level ' = 2, ' Upid '                = = $v [' id ']])->select ();                    foreach ($c as $ck = + $CV) {$p [$k] [' Child '] [$ck] = $CV;                    $d = $areaModel->field (' Id,name,level ')->where ([' Level ' = + 3, ' upid ' = ' + $CV [' id ']])->select ();                    foreach ($d as $DK = $DV) {$p [$k] [' Child '] [$ck] [' child '] [] = $DV;        }}} $redis->set (' China:area ', Json_encode ($p, Json_unescaped_unicode));        } else {$p = Json_decode ($data, true); }       $this->json->setattr ("Data", $p); $this->json->send ();}

First query, elapsed time

Second query, has been deposited into Redis

The gap is obvious!!!

Redis a key or value can store 512M of data, very powerful.

You can go to Redis for a look,

127.0.0.1:6379> keys china:area1) "china:area"

Redis can store any type of data, preferably in JSON encryption. When it is acquired, it is decrypted.

To view the PHP extension documentation,https://github.com/phpredis/phpredis/

Using Redis in 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.