Explanation of Laravel Framework Using Redis Method

  • 2021-10-13 06:54:21
  • OfStack

This article illustrates how the Laravel framework uses Redis. Share it for your reference, as follows:

Installation

Using redis in laravel first requires you to install predis/predis packages through Composer:


composer require predis/predis

Configure

The configuration file for redis 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 does not need to be changed when testing and playing, and another place is. env file


REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

These are relevant information, but you don't need to change them. Let's not talk about the redis cluster, but the use of a single redis.

Test

First, you need 1 route:


//redis Test 
Route::get('testRedis','RedisController@testRedis')->name('testRedis');

Create a controller with artisan command


php artisan make:controller RedisController

Then we introduce the corresponding class and create a method in the controller.

After we installed through composer, laravel framework has helped us register redis in app. php configuration file and facade support, so we can use it directly. (Member class is a datasheet model that I tested myself, so don't pay attention to it.)


<?php
namespace 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"
    // Plus 1 A small example, such as a person or a news on the homepage of the website, has a particularly high number of daily visits, which can be stored in redis Relieve memory pressure 
    $userinfo = Member::find(1200);
    Redis::set('user_key',$userinfo);
    if(Redis::exists('user_key')){
      $values = Redis::get('user_key');
    }else{
      $values = Member::find(1200);// Here, for testing, you can put id=1200 Change to another 1 A id
     }
    dump($values);
  }
}

Error problem

You may report this error when you finish 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 many people may step on the pit when they first use it.

This is because the redis service is not installed and started on your server, as is the case with mysql1, only if it is installed and successfully started.

I tested it under windows, so I said it with windows. Follow-up will write related redis articles, Linux installation, startup and use will be introduced.

First download the windows version: https://redis.io/download

Or use my downloaded version: 4.0. 8

4.0. 8-windows-redis on github Download address: https://github.com/antirez/redis/archive/4. 0.8. zip

Or: Click here to download it.

In fact, the following is the tutorial on how to install Redis for windows

Unzip the compressed package just downloaded, change the name to Redis (optional) and put it on C disk

Open the cmd window under this path and enter it directly redis.exe

The following is displayed to indicate that the installation and startup were successful. (Note: If you want to operate on the command line, you should open another cmd window, and this one cannot be closed.)

If you don't want to start in this directory every time, please configure the environment variable.

Now you re-run the request in Laravel just now, and it will work normally.

For more readers interested in Laravel related content, please check the topics on this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to the PHP programming based on Laravel framework.


Related articles: