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

A guide to using Redis with Lumen

coldplay.xixi
Release: 2021-02-03 17:58:06
forward
2705 people have browsed it

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

(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);
Copy after login

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

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

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

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!

Related labels:
source:csdn.net
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