How to connect redis in laravel project ?

In this tutorial we’re going to learn how to connect redis database in laravel project. I have mentioned in step by step ini very easy way.

What is Redis ?

Redis is an open-source, in-memory data store that functions as a versatile tool for developers. Here’s a breakdown of its key characteristics:pen_spark

In-Memory Storage:

  • Data resides in RAM, enabling extremely fast access times (sub-milliseconds) compared to traditional disk-based databases. This makes it ideal for real-time applications and caching frequently accessed data.

Key-Value Store:

  • Data is organized as key-value pairs, similar to a dictionary. Keys are unique identifiers used to retrieve their corresponding values. This simple structure allows for quick data manipulation.

How to connect in redis in laravel Project ?

1st step open your project and run below command

composer require predis/predis

2nd step go to below path

config/app.php

Paste below code in app.php code

'redis' => [
 
    'client' => env('REDIS_CLIENT', 'phpredis'),
 
    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD'),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_DB', 0),
    ],
 
    'cache' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD'),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_CACHE_DB', 1),
    ],
 
],

Next to create controller

php artisan make:controller RedisController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
class RedisController extends Controller
{
    public function index(){
        Redis::set('users:1:first_name','amit');
        return 'redis testing amit';
    }
}

Next go to web.php and put below code

Route::get('/redis', [App\Http\Controllers\RedisController::class, 'index']);

Next go to .env and put below code

REDIS_CLIENT=predis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

Next to open your server and check

Next go to redis stack and check data is coming or not

Now data inserted successfully.

Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.

Related Posts

Linux Commands to Troubleshoot Performance Issues

In this tutorial we’re going to learn how to Troubleshoot Performance Issues with the CPU, RAM, disc I/O, network usage, and general responsiveness of the system. You…

Here’s a list of 50 Linux commands commonly used for troubleshooting a Linux server:

1. ping Command Tests connectivity to a remote host. 2. netstat Command Displays network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. Output: – 3….

Top 50 Linux Commands for Administrator

In this tutiorial i’m going to share Top 50 Linux command for administrator use. ls is probably the first command every Linux user types in their terminal….

Problem 1 – laravel/passport[v11.5.0, …, v11.8.4] require league/oauth2-server ^8.2

In this tutorial we’re going to solve then error Problem 1 – laravel/passport[v11.5.0, …, v11.8.4] require league/oauth2-server ^8.2 Error :- Solutions : – Now issue has been…

Traceback (most recent call last):File “/usr/bin/pipenv”, line 33, insys.exit(load_entry_point(‘pipenv==11.9.0’, ‘console_scripts’, ‘pipenv’)())

In this tutorial i’m going to solve the issue “Traceback (most recent call last):File “/usr/bin/pipenv”, line 33, insys.exit(load_entry_point(‘pipenv==11.9.0’, ‘console_scripts’, ‘pipenv’)())” Error :- Solutions :-

Top 25 Linux Commands for Users ?

In this tutorial i’m going to share Top 50 Important Linux Command for users with example. 1. Use of ls -la command. The ls -la command is…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x