Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Lumen uses Redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report