Get the App
SLTechnology News&Howtos  ›  Database  › 

How Lumen uses Redis

Shulou Source: shulou.com Published: 2022-05-31 23:33:11 10月16日 Update

This article mainly introduces Lumen how to use Redis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

1. Install extension

Two extensions must be installed to use redis

Composer require predis/predis composer require illuminate/redis

(PS: officially, it is required to install two installed versions: predis/predis (~ 1.0) and illuminate/redis (5.2.*). Because these two versions are currently installed, you do not add the version number when using compose. If you find that you cannot use it after installation, please add the version number when executing composer)

two。 Introduction of redis support

To introduce the extension of redis into the directory bootstrap/app.php

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

3. Enable redis helper function

Lumen is a little different from Laravel. The default 'Facades'' and 'Eloquent' are not enabled. If you want to use redis in laravel, just open the $app- > withFacades () and $app- > withEloquent () annotations of' Facades' and 'Eloquent' in the file bootstrap/app.php.

4. Configure redis server parameters

The default system is the redis configuration file in the called .env, but these parameters are generally not available after installation. You can check the file path vendor/laravel/lumen-framework/config/database.php to see what parameters need to be configured. For example, my .env file needs to be configured.

REDIS_HOST=192.168.1.41REDIS_PORT=7000REDIS_PASSWORD=123456

5. Use redis

The first step is to introduce classes within the controller that uses redis. Use Illuminate\ Support\ Facades\ Redis

Then you can use the redis function directly.

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

6. The second way to use redis

You can also call redis using the helper function Cache

The first step is to introduce the Cache class into the controller that uses redis. Illuminate\ Support\ Facades\ Cache

Then you can use the redis function directly.

Cache::store ('redis')-> put (' site_name', 'Lumen Test', 10); return Cache::store ('redis')-> get (' site_name'); Thank you for reading this article carefully. I hope the article "how Lumen uses Redis" shared by the editor will be helpful to you. At the same time, I hope you will support us, pay attention to the industry information channel, and more related knowledge is waiting for you to learn!

Tags: Version function file article configuration two parameters controller time control support assistance value interest at the same time official that is method more friends Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker Xiaomi Microsoft Linux