DEV Community

Dendi Handian
Dendi Handian

Posted on • Edited on

Redis in Laradock

Cache is the first thing you have in mind and would do to speed-up your application performance. Any kind of cache storages are worthful, but memory is more preferable. When it come to memory, Memcached and Redis are top choiches. Memcached arguably has the higher performance, but some people prefer Redis because of its data-persistent ability. Regardless of your choice, I still go with Redis because it's what I want to tell you here and I think it's the popular one 😋

Laradock has Redis and PhpRedisAdmin services pre-configured and let's try these out!

Run Redis and PhpRedisAdmin

I assume you have tried and know how to work with Laradock, if not then you can start to setup a laravel app with Laradock here.

To run the Redis and PhpRedisAdmin, simply run this docker-compose command:



docker-compose up -d redis redis-webui


Enter fullscreen mode Exit fullscreen mode

after executing above command, check the containers status by executing docker-compose ps . Everything are OK if these services runs like this:



           Name                          Command               State                    Ports
---------------------------------------------------------------------------------------------------------------
...
laradock_redis-webui_1        tini -- php -S 0.0.0.0:80        Up      0.0.0.0:9987->80/tcp
laradock_redis_1              docker-entrypoint.sh redis ...   Up      0.0.0.0:6379->6379/tcp
...


Enter fullscreen mode Exit fullscreen mode

We could also check the amount of resources used by these container by executing docker stats and here are what I got:



CONTAINER ID        NAME                          CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
b03c9e10d43c        laradock_redis-webui_1        0.03%               4.391MiB / 3.846GiB   0.11%               726B / 0B           14.5MB / 0B         2   
244ddeb2ceb7        laradock_redis_1              0.70%               2.016MiB / 3.846GiB   0.05%               726B / 0B           7.44MB / 0B         4 


Enter fullscreen mode Exit fullscreen mode

We can see that the containers uses small amount of resources.

Enter Redis Console for using Redis CLI

Even if I'm about to tell you about PhpRedisAdmin later, you might want to check out how to use redis' cli commands in Laradock. To enter the console, simply run this command:



docker-compose exec redis bash


Enter fullscreen mode Exit fullscreen mode

Then you will be inside the console and you can try a redis-cli command like this:



C:\Users\drizzer\laradock>docker-compose exec redis bash
root@82596b34b720:/data# redis-cli PING
PONG
root@82596b34b720:/data#


Enter fullscreen mode Exit fullscreen mode

to exit the console, type exit and ENTER.

Setup Redis as Cache Engine for Laravel App

To continue, make sure you have redis client installed first. It's either Predis or PHPRedis, but I will use Predis here. So, if you not installed predis yet, then add it to your laravel app by executing this composer command:



composer require predis/predis:^1.0


Enter fullscreen mode Exit fullscreen mode

When you done installing predis, then make sure these parameters and values are set in your laravel app .env file:



...

CACHE_DRIVER=redis

REDIS_URL=redis
REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379

...


Enter fullscreen mode Exit fullscreen mode

Then, you can start a test to set a value to redis through laravel app. The simple one is by modifying the default laravel welcome route in routes\web.php like this:



...

Route::get('/', function () {
    \Cache::store('redis')->put('Laradock', 'Awesome', 100);
    return view('welcome');
});

...


Enter fullscreen mode Exit fullscreen mode

Then access your laravel app homepage once, and let's see the result in PhpRedisAdmin.

PS: the third parameter with 100 value above is the lifetime of the created cache until it destroyed automatically.

Accessing PhpRedisAdmin

When you accessing http://localhost:9987 for the first time or out of the session, you will be alert-prompted to enter username and password. enter laradock for both and you will see the interface like this:

phpredisadminui

By default, our created cache is created in database 0 and you will definitely see it already.

test-redis


That's all basic usage of Redis in Laradock from me, have fun experimenting Redis in Laradock environment!



laravel version used: 6.0 LTS


Enter fullscreen mode Exit fullscreen mode

Top comments (0)