Yii2 Redis and Cache

Source: Internet
Author: User
Tags yii

Original address: http://www.myexception.cn/php/1974979.html

Composer require Yiisoft/yii2-redis

After installation, use ultra-simple, open the common/config/main.php file, modify the following.

' Cache ' = [    //' class ' = ' Yii\caching\filecache ',    ' class ' = ' Yii\redis\cache ',], ' redis ' = [    ' class ' = ' yii\redis\connection ',    ' hostname ' = ' localhost ',    ' port ' = 6379,    ' database ' = > 0,],

OK, now has used Redis to take over the cache of Yii, the use of the cache as before, how to use the present or how to use, but there is not a bug, so calculate the small pits, and so will say.

To test the cache first,

Yii:: $app->cache->set (' Test ', ' hehe. '); echo Yii:: $app->cache->get (' Test '), ' \ n '; Yii:: $app->cache->set (' test1 ', ' haha. ', 5); Echo ' 1 ', Yii:: $app->cache->get (' test1 '), "\ n"; sleep (6); Echo ' 2 ', Yii:: $app->cache->get (' test1 '), "\ n";

Look at the test results.

With the same usage, no problem.

But just now I said there is a bug that does not count the bug, so the small pits, what is it?
If you take over the cache directly with Redis, if it's OK to use it properly, then Redis will get an error when the value of the expiration time exceeds the int range.
I used yii2-admin, and it happened that I stepped on the pit because he cached it for 30 days, which is 2.592 million seconds, and the Redis cache time precision is in milliseconds by default, so the time is 2592000000 milliseconds.
and the Redis expiration time can only be int type, cache.php php forced to int, and no other processing, so it will become 1702967296 and then the error.

However, there are no negative numbers directly under the Redis command line.

But it doesn't matter, it's easy to fix, we can change it to seconds.
Open vendor/yiisoft/yii2-redis/cache.php Line 133th and modify it to the following code.

protected function SetValue ($key, $value, $expire) {    if ($expire = = 0) {        return (bool) $this->redis-> ExecuteCommand (' SET ', [$key, $value]);    } else {        //$expire = (int) ($expire * 1000);//unit default is milliseconds        //return (BOOL) $this->redis->executecommand (' SET ', [$key, $value, ' PX ', $expire]);        $expire = + $expire > 0? $expire: 0; Prevent negative        return (BOOL) $this->redis->executecommand (' SET ', [$key, $value, ' EX ', $expire]);//per second cache    }}

This will be OK.

Yii2 Redis and Cache

Related Article

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.