Methods of using Redis in the Laravel framework

Source: Internet
Author: User
Tags redis cluster redis tutorial
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.