This article mainly introduces the method of using Redis in Laravel framework, and analyzes the configuration, usage and operation of Redis database in Laravel framework in detail, and the need of friends can refer to
The examples in this article describe how Redis is used by the Laravel framework. Share to everyone for your reference, as follows:
Installation
Using Redis in Laravel first requires you to install the Predis/predis package via Composer:
Composer require Predis/predis
Configuration
The Redis configuration file is: config/database.php
' Redis ' = [ ' client ' = ' Predis ', ' default ' = ' = ' host ' + env (' redis_host ', ' 127.0.0.1 '), ' Password ' = env (' Redis_password ', null), ' Port ' = env (' Redis_port ', 6379), ' database ' = 0, ], ],
This self-test play without the need to change, another place is. env file
redis_host=127.0.0.1redis_password=nullredis_port=6379
These are the relevant information, but you don't need to change them. Let's not talk about the Redis cluster, first of all, the use of a single redis.
Test
First you need a route:
Redis test Route::get (' Testredis ', ' Rediscontroller@testredis ')->name (' Testredis ');
Create a controller with the artisan command
PHP Artisan Make:controller Rediscontroller
Then we introduce the corresponding class in the controller and create a method.
Since we installed through composer, the Laravel framework has helped us to register and façade support for Redis in the app.php configuration file, so we can use it directly. (Member class is my own test data table model, regardless of)
<?phpnamespace app\http\controllers;use illuminate\http\request;use app\models\member;use Illuminate\Support\ Facades\redis;class Rediscontroller extends controller{public function Testredis () { redis::set (' name ' , ' Guwenjie '); $values = redis::get (' name '); DD ($values); Output: "Guwenjie" //Add a small example such as the site home page A person or a news day visit is particularly high, can be stored in Redis, reduce memory pressure $userinfo = Member::find (); Redis::set (' User_key ', $userinfo); if (redis::exists (' User_key ')) { $values = redis::get (' User_key '); } else{ $values = member::find (1200);//here to test you can change id=1200 to another ID } dump ($values);} }
Error issues
This error may be reported when you are done with the above operation:
(1/1) Connectionexceptionŀܾӡ[tcp://127.0.0.1:6379]in abstractconnection.php (line 155) at Abstractconnection->onconnectionerror (' Ŀܾӡ ', 10061) in streamconnection.php (line 128) ....
In fact, this problem is not a problem, but it is possible that many people will step on the pit when they are just using it.
This is because the Redis service is not installed and started on your server, just like MySQL, and is used as a prerequisite for installation and successful startup.
I tested it under windows and said things in Windows. Follow-up will write the relevant Redis articles, Linux installation, startup use, etc. will be introduced.
First download windows version: Https://redis.io/download
or use me to download the good version: 4.0.8
In fact, the next step is how Windows installs the Redis tutorial
Unzip the package you just downloaded and change the name to Redis (not modifiable) to C drive
Open the cmd window under this path and enter directlyredis.exe
The following display indicates the installation and success of the boot. (Note: If you want to operate at the command line, you should open a CMD window again, this cannot be turned off)
If you do not want to start the directory every time, configure the environment variable.
Now you rerun the request in the laravel just as it will work.