Yii2-Redis use note

Source: Internet
Author: User
Yii2-Redis used note a few days ago a 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, 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.

Text

'cache' => [    // 'class' => 'yii\caching\FileCache',    'class' => 'yii\redis\Cache',],'redis' => [    'class' => 'yii\redis\Connection',    'hostname' => 'localhost',    'port' => 6379,    'database' => 0,],
'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,

Text

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";
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 vendor/yiisoft/yii2-redis/Cache. php

The

133

Line, modify to the following code.

Text

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 }}
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 these ideas today.

Problem http://www.linuxidc.com/Linux/2016-03/128949.htm that using Pjax in Yii2 causes Yii2 inline script loading failure

Yii2 implement password change function http://www.linuxidc.com/Linux/2015-07/120137.htm

Yii user login mechanism http://www.linuxidc.com/Linux/2015-01/111602.htm

Introduce js and css file http://www.linuxidc.com/Linux/2015-01/111603.htm in Yii

Yii incomplete solution http://www.linuxidc.com/Linux/2015-01/111606.htm

Yii CGridView basic use http://www.linuxidc.com/Linux/2015-01/111607.htm

Implementation Scheme of Yii Framework distributed cache http://www.linuxidc.com/Linux/2015-02/113828.htm

Yii details: Click here

Yii: Click here

This article permanently updates the link address: Http://www.linuxidc.com/Linux/2016-06/132347.htm

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.