Home > PHP Framework > YII > How to use redis in yii2 project

How to use redis in yii2 project

爱喝马黛茶的安东尼
Release: 2019-12-09 10:28:22
Original
3303 people have browsed it

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"
Copy after login

Or add

"yiisoft/yii2-redis": "~2.0.0"
Copy after login

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,
        ],
    ]
];
Copy after login

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'];
Copy after login

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',
    ],
],
Copy after login

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,
        ],
    ],
],
Copy after login

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'];
Copy after login

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'
    ],
],
Copy after login

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,
        ],
    ],
],
Copy after login

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'];
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template