A few days ago, I learned to use it on a project by simply learning the next Redis. We are currently using the YII2 framework, search the website of the next Redis, found the Yii2-redis this extension.
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 had cached it for 30 days, that is 2.592 million seconds, and the Redis cache time precision was in milliseconds by default, so time was 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 the vendor/yiisoft/yii2-redis/cache.php 133 Line 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.
Well, share these today, Ming and tomorrow will say Yii2-redis Connection and ActiveRecord and small pits.
The above describes the use of Yii2-redis-Cache, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.