How To Set and Get Redis Data in Laravel Using Commands

Last updated 5th September, 2023

Redis' most common application in the average Laravel app is as a cache driver. Its high throughput, lightning fast speed and easy configuration make for an obvious choice.

Cache operations are usually performed via the Cache facade in Laravel. Occasionally however, I've needed to interact with Redis in a more bespoke context.

Setting Data In Redis

Using Laravel's Redis facade [DOCS] gives you access to the command method, which will proxy all of your calls onto an underlying Redis instance.

It accepts two arguments:

  • The command you want to run, in this case set. All commands
  • An array of args that you want to forward to Redis.

All of the members of the second argument you pass to the function will be spread directly into the command executed by Redis.

In this case, we're using set, and the first argument we forward to Redis represents the string key, the second is the value we want to set, and the third is the ttl or the amount of time the key should persist in seconds before being dropped.

use Illuminate\Support\Facades\Redis;

Redis::command('set', [
    'Hello', 'World!', 300
]);

When Laravel passes this command on to redis, it'll look like this - if you executed it on the Redis CLI:

SET Hello "World!" EX 300

Getting Data In Redis

If we then want to get that value back out of Redis we can run the following similar get command:

use Illuminate\Support\Facades\Redis;


Redis::command('get', ['Hello']);

Which is equivalent to running:

GET Hello

# World!

Couldn't be simpler right?

Customising The Redis Connection

You can also choose which connection you'd like to access Redis on with the facade.

I've worked on a couple of projects that has multiple instances of Redis running. One where we had a cache instance running on Redis, and a second which acted as a high throughput storage of analytics data.

Using the connection method, you can change which connection Laravel will connect to:

Redis::connection('analytics')->command('set', [
    'key', 'value', 300
]);

This connection method corresponds to a database you have defined in the config/database.php file in your Laravel project.

Here's an example of what that configuration might look like:

// config/database.php

'redis' => [
    'analytics' => [
        'host' => env('REDIS_ANALYTICS_HOST', '127.0.0.1'),
        'password' => env('REDIS_ANALYTICS_PASSWORD', null),
        'port' => (int) env('REDIS_ANALYTICS_PORT', 6379),
        'database' => env('REDIS_ANALYTICS_DB', 0),
        'persistent' => env('REDIS_ANALYTICS_PERSISTENT', 1),
        'timeout' => 2,
    ],

    'cache' => [
        'host' => env('REDIS_CACHE_HOST', '127.0.0.1'),
        'password' => env('REDIS_CACHE_PASSWORD', null),
        'port' => (int) env('REDIS_CACHE_PORT', 6380),
        'database' => env('REDIS_CACHE_DB', 0),
        'persistent' => env('REDIS_CACHE_PERSISTENT', 1),
        'timeout' => 2,
    ],
],

The key of those Redis configurations are the argument given to Redis in the command method.

Final Thoughts

The Redis facade gives you access to any of the supported Redis commands that area available. See the Redis Documentation for a full list of commands.

Hope this helps 🤙

No comments yet…

DevInTheWild.

Login To Add Comments.

Want More Like This?

Subscribe to occasional updates.