Home >Database >Redis >A guide to using Redis with Lumen

A guide to using Redis with Lumen

coldplay.xixi
coldplay.xixiforward
2021-02-03 17:57:232684browse

A guide to using Redis with Lumen

Recommended (free): redis

Since the official document is too simple, I wrote a detailed usage guide

1. Install extensions

To use redis, you must install two extensions

 composer require predis/predis
 composer require illuminate/redis

(PS: There are official requirements to install two installations The versions are predis/predis (~1.0) and illuminate/redis (5.2.*). Because the latest versions currently installed are these two versions, there is no need to use compose when using compose. Add the version number. If you find that it cannot be used after installation, please add the version number when executing composer)

2. Introduce redis support

The redis extension should be introduced in the directory bootstrap/app.php

$app->register(Illuminate\Redis\RedisServiceProvider::class);

3. Enable redis auxiliary functions

Lumen and Laravel have some Different, 'Facades' and 'Eloquent' are not enabled by default. If you want to use redis in laravel, you need to change the ## of 'Facades' and 'Eloquent' in the file bootstrap/app.php #$app->withFacades() and $app->withEloquent() just open the comments

4. Configure redis server parameters

The default system calls the redis configuration file in

.env, but generally there are no these parameters after installation. You can check the file path vendor/laravel/lumen-framework Check what parameters need to be configured in /config/database.php. For example, my .env file needs to be configured

REDIS_HOST=192.168.1.41REDIS_PORT=7000REDIS_PASSWORD=123456

5. Use redis

First introduce the class into the controller using redis.

use Illuminate\Support\Facades\Redis Then you can use the redis function directly

Redis::setex('site_name', 10, 'Lumen的redis');return Redis::get('site_name');

6. The second method of using redis

You can also call redis using the auxiliary function Cache

First, introduce the Cache class into the controller using redis.

Illuminate\Support\Facades\Cache Then you can use the redis function directly

Cache::store('redis')->put('site_name', 'Lumen测试', 10);return Cache::store('redis')->get('site_name');

The above is the detailed content of A guide to using Redis with Lumen. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete