As you see — in this short article I’m gonna show you how you can use Redis and Laravel. So, let’s begin 💥
And if you don’t know Redis — store, you can use it to save some hashes, sets, or even cache if you want. The logic of the Redis store is simple key-value. That’s all information that we need to know, for now, let’s move on to the next topic.
Before you gonna start creating a new laravel app — we need to install Redis by using brew (if you’re using a macOS). Open your terminal and type the next two commands:
$ brew tap redis-stack/redis-stack
$ brew install --cask redis-stack
And after that you can start the Redis server:
$ redis-stack-server
If the server is started — you will see the next message in the terminal:
First of all, let’s create a new laravel project:
$ composer create-project laravel/laravel redis-tutorial
Okay, and now let’s install a Redis package via composer as I said:
$ composer require predis/predis
Now open your config/database.php and change the Redis client to the predis (cause we want to use Redis via the composer package that we installed):
<?php
// ...
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
// ...
];
// ...
So, now we can start using Redis in this app.
Good, now let’s try to make the first example. Let’s create a new controller:
$ php artisan make:controller PostsController
Open this controller and let’s try to create a few keys with values:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
class PostsController extends Controller {
public function index()
{
Redis::set('name:1:first_name', 'Mike');
Redis::set('name:2:first_name', 'John');
Redis::set('name:3:first_name', 'Kate');
echo "Added few values";
}
}
Now open routes/web.php and add the next route:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostsController;
// ...
Route::get('/redis', [PostsController::class, 'index']);
The next step is to start the local server of the app and visit this page in your browser:
$ php artisan serve
If we open now RedisInsight tool — you will see that we have these three keys with their values on the Redis server:
You can also get the values of these keys by using the “get” function:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
class PostsController extends Controller {
public function index()
{
Redis::set('name:1:first_name', 'Mike');
Redis::set('name:2:first_name', 'John');
Redis::set('name:3:first_name', 'Kate');
$name_1 = Redis::get('name:1:first_name');
$name_2 = Redis::get('name:2:first_name');
$name_3 = Redis::get('name:3:first_name');
echo "Names: " . $name_1 . " " . $name_2 . " " . $name_3;
}
}
And if you see the success in your browser — that means that all works:
That’s all for this article. Now you know how to set up Redis for your project quickly and use it for saving or caching data inside the Redis database. Soon, I’m gonna publish an article where we gonna talk about Redis deployment