Installing Redis Dependencies in Laravel

Install dependent packages

Terminal window
composer require predis/predis

Configure redis in Laravel

Add connection information to config/database.PHP. For specific parameters, please refer to the official documentation:

'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
],

This means that the redis server is installed locally, the port is 6379, and the database is the 0th database

Used in controller or model

After the above configuration is completed, reference redis in any controller:

use Illuminate\Support\Facades\Redis;

We can use the set command to save the value to redis, and the get command to get the value.

public function testRedis($name = "test"){
Redis::set('name', $name);
$result = Redis::get('name');
var_dump($result);exit;
}

Configure the route to access this method and see if the output is correct.

Summary

Before using it, it is necessary to have a necessary understanding of redis. It is best to install a redis under Linux and then run the basic commands so that some concepts will not be vague.

Redis 基础 7 Application Scenarios for Redis