In this tutorial I am will explain to you how we can configure the Redis server and use Redis as a cache in our web application.

Redis.

Redis is an in-memory data structure store, used as a database, cache and message broker. It stores data in key-value which gives Redis more flexibility and performance.

Prerequisites

Before we start this tutorial you need to have complete access to your server we need to install Redis server and also add some required PHP extension.

Installing and configure Redis

Step 1. Login to your server via terminal or putty if you are using the window.

Step 2. Once login successfully runs bellow commands.

sudo apt update
sudo apt install redis-server

Step 3. Check if Redis is installed successfully using below command

sudo systemctl status redis


Step 4. Now we can check Redis server is functioning correctly, we can use redis-cli, Redis CLI is a command-line tool for Redis.

redis-cli

Step 5. Now if you want to restart Redis server run this command

sudo systemctl restart redis

Or, if you want to clear Redis cache you can run this command

redis-cli flushall

Configure Redis for PHP extension

To use Redis with your PHP application we need an extension that connects PHP with Redis to do that we need to run below command.

sudo pecl install redis
sudo apt-get install php-redis

After this, we need to restart our PHP using below command

sudo service php7.0-fpm restart

Now once your PHP restarts you are ready to use the Redis server.

Add Redis as Cache in CakePHP 3 Application

If you want to use Redis as a cache for your CakePHP 3 Application you update your cache setting in config/app.php and update your cache setting with this

'Cache' => [
        'default' => [
            'className' => 'Redis',
            'path' => CACHE,
            'password' => false,
            'server' => '127.0.0.1',
            'port' => 6379,
        ],
        '_cake_core_' => [
            'className' => 'Redis',
            'prefix' => 'cake_redis_core_',
            'path' => CACHE . 'persistent/',
            'serialize' => true,
            'duration' => '+1 years',
            'server' => '127.0.0.1',
            'port' => 6379,
            'password' => false,
        ],
        '_cake_model_' => [
            'className' => 'Redis',
            'prefix' => 'cake_redis_model_',
            'path' => CACHE . 'models/',
            'serialize' => true,
            'duration' => '+1 years',
            'server' => '127.0.0.1',
            'port' => 6379,
            'password' => false,
        ],
    ],

That’s all I hope this post helps you to configure Redis on your server. Please like this post and also subscribe to our youtube channel. 🙂