Home >PHP Framework >YII >How to use redis in yii2 project
If you want to use redis key-value storage well in the PHP framework of Yii2, then you must first recommend the official Github library yii2-redis. This library can help us use redis in the Yii2 framework. It provides support for caching, Session and ActiveRecord mode.
Install the yii2-redis library
It is recommended to use composer to install the yii2-redis library and execute it in the root directory of your project
php composer.phar require --prefer-dist yiisoft/yii2-redis:"~2.0.0"
Or add
"yiisoft/yii2-redis": "~2.0.0"
to your composer.json file, and then run composer update. It is really slow inside the wall, just wait patiently.
Configuring redis
To use this extension correctly, you must configure the Connection class in the configuration file of your application, generally For example, the configuration file is config\web.php.
Add the redis project to your component, as follows:
return [ //.... 'components' => [ 'redis' => [ 'class' => 'yii\redis\Connection', 'hostname' => 'localhost', 'port' => 6379, 'database' => 0, ], ] ];
After this, you can use redis normally in the yii2 framework.
Example
Simple use
Let’s first look at the simplest way to use redis Code:
$redis = Yii::$app->redis; $key = 'username'; if ($val = $redis->get($key)) { return ['redis' => $val]; } else { $redis->set($key, 'Leon'); $redis->expire($key, 5); } return ['redis' => 'no data'];
There is not a single line of comments, but it is clear at a glance.
Look for the username key. If it cannot be found, set the key-value store and the expiration time is 5 seconds.
This is a complete example of using redis.
Cache
So next, let’s take a look at how to use redis for caching.
Similarly, for use as a cache, we need to modify the cache items in the configuration file:
'components' => [ 'cache' => [ // 'class' => 'yii\caching\FileCache', 'class' => 'yii\redis\Cache', ], ],
If you have not configured the redis component, you also need to configure redis under the cache:
'components' => [ 'cache' => [ // 'class' => 'yii\caching\FileCache', 'class' => 'yii\redis\Cache', 'redis' => [ 'hostname' => 'localhost', 'port' => 6379, 'database' => 0, ], ], ],
The sample code is as follows, it is easy to understand and there is no need to explain too much:
$cache = Yii::$app->cache; $key = 'username'; if ($cache->exists($key)) { return ['cache' => $cache->get($key)]; } else { $cache->set($key, 'Leon', 5); } return ['cache' => 'no cache'];
Session
Finally, redis is used as session. It also needs to be configured in the component:
'components' => [ 'session' => [ 'name' => 'advanced-frontend', 'class' => 'yii\redis\Session' ], ],
If redis has not been configured, it also needs to be configured:
'components' => [ 'session' => [ 'name' => 'advanced-frontend', 'class' => 'yii\redis\Session', 'redis' => [ 'hostname' => 'localhost', 'port' => 6379, 'database' => 0, ], ], ],
The sample code is as follows:
$session = Yii::$app->session; $key = 'username'; if ($session->has($key)) { return ['session' => $session->get($key)]; } else { $session->set($key, 'Leon'); } return ['session' => 'no session'];
In a simple demonstration, how to Proper use of redis, an efficient tool, will test everyone's ability. Come on!
PHP Chinese website has a large number of free Yii introductory tutorials, everyone is welcome to learn!
The above is the detailed content of How to use redis in yii2 project. For more information, please follow other related articles on the PHP Chinese website!