Integrating Redis Cache into Laravel: Installation, Configuration, and Usage
This guide explains how to download and install the Redis extension, add the Predis package to a Laravel project, configure single‑node and clustered Redis connections in config/database.php, and demonstrates basic Redis commands, transactions, and Lua scripting within Laravel controllers.
Redis is an in‑memory key‑value store that supports strings, lists, sets, sorted sets, and hashes. Integrating Redis into a Laravel application can significantly improve performance for caching and other data‑intensive operations.
1. Download Redis extension
For Windows, simply download and extract the appropriate DLL. For Linux, compile the source and add redis.so to the PHP extensions.
2. Install the Predis client
Run the Composer command to add Predis to the project: composer require predis/predis 3. Configure Redis in Laravel
Edit config/database.php and add the following configuration for a single Redis instance:
<?php
return [
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'),
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
];4. Cluster configuration (optional)
For a Redis cluster, extend the configuration with options and clusters sections:
<?php
return [
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
],
'clusters' => [
'default' => [
[
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
// add more nodes as needed
],
],
],
];5. Basic Redis operations in Laravel
Use the Redis facade inside a controller:
<?php
use Illuminate\Support\Facades\Redis;
class IndexController extends Controller {
public function index() {
$key = 'laravel';
// Redis::set($key, 'M0001'); // set a value
$value = Redis::get($key);
print_r($value); // outputs the stored value
}
}6. Transactional operations
Wrap multiple commands in a transaction to ensure atomicity:
<?php
use Illuminate\Support\Facades\Redis;
Redis::transaction(function ($redis) {
$redis->incr('laravel_num', 1); // increment by 1
$redis->incr('laravel_num', 2); // increment by 2
});
$result = Redis::get('laravel_num');
print_r($result); // should output 37. Lua scripting with Redis
Execute a Lua script via the eval method to perform conditional logic:
<?php
use Illuminate\Support\Facades\Redis;
$counter = Redis::eval(
"local counter = redis.call('incr', KEYS[1])
" .
"if counter > 10 then
" .
" redis.call('incr', KEYS[2])
" .
"end
" .
"return counter",
2,
'first',
'second'
);
var_dump($counter);After following these steps, you will have successfully downloaded and installed the Redis extension, added the Predis client, configured both single‑node and clustered connections, and performed basic, transactional, and scripted operations with Redis in a Laravel application.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
