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

A guide to using Redis with Lumen

coldplay.xixi
coldplay.xixiOriginal
2021-01-22 10:17:121925browse

A guide to using Redis with Lumen

Recommended (free): redis tutorial

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: Officially requires the installation of two extensions The installed versions are predis/predis (~1.0) and illuminate/redis (5.2.*). Because the latest versions currently installed are these two versions, when using compose No version number is added. If you find that it cannot be used after installation, please add the version number when executing composer)

2. Introduce redis support

Introduce the redis extension in the directory bootstrap/app.php

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

3. Enable redis auxiliary functions

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

4. Configure the 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- Check what parameters need to be configured in framework/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');

Original link: Dennis`s blog

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:
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