Yii2-Redis with note, yii2-redis NOTE _ PHP Tutorial

Source: Internet
Author: User
Yii2-Redis with note, yii2-redis note. Yii2-Redis uses note, yii2-redis note a few days ago simple learning Redis, now ready to use it on the project. We are currently using the Yii2 framework, in the official website to search for Redis, Yii2-Redis use note, yii2-redis note

I learned about Redis a few days ago, and now I am going to use it on the project. We are currently using the Yii2 framework, in the official website to search for Redis, we found the yii2-redis this extension.

After installation, it is very easy to use. open the common/config/main. php file and modify it as follows.

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

Okay, now we have used redis to take over the yii cache. the cache usage is the same as before. how can we use it now? but there is a bug that does not count as a bug, so it's a pitfall, will be said later.

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";

Let's take a look at the test results.

It is the same as the original usage ..

But as I said, there is a bug that does not count as a bug. So what is it?
If you directly use redis to take over the cache, if it is normal to use, but when the expiration time value exceeds the int range, redis will report an error.
I used the yii2-admin, coincidentally let me step on the pit, because he cached 30 days, that is, 2592000 seconds, and redis Cache time precision default uses milliseconds, so the time is 2592000000 milliseconds.
The expiration time of redis can only be int type. php in Cache. php is forcibly converted to int type without any other processing, so it will become-1702967296 and an error will be reported.

But directly under the redis command line will not be negative ,.

But it doesn't matter. it's easy to fix. we can change it to seconds.
Open row 133rd of vendor/yiisoft/yii2-redis/Cache. php 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); // Default unit: millisecond // 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]); // cache by second }}


In this case, OK.

Now, let's share this today. tomorrow we will talk about the yii2-redis's Connection, ActiveRecord, and trap.

I learned about Redis a few days ago, and now I am going to use it on the project. We are currently using the Yii2 framework. I searched for Redis on the official website ,...

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.