Using a Redis Server

This is a community-contributed tutorial.
Jun 16, 2023

By default, Concrete stores non-volatile cache items in the filesystem (in the /application/files/cache directory).

If you want to use a redis server instead, you have to manually edit the /application/config/concrete.php file, where you can define how the different persistent caches have to be stored.

Concrete caches

You can use Redis instead of the filesystem cache with these configuration keys:

  • concrete.cache.levels.overrides a cache used to store the resolved overrides
  • concrete.cache.levels.expensive a cache used to store data that should be kept across different web requests (it's the cache/expensive described here)
  • concrete.cache.levels.object this is usually a cache that's ephemeral, but you may want to use a persistent driver for it (it's the cache described here)

Concrete supports 2 different type of connection to a redis server:

  1. To define connection via TCP/IP, you can use an array like this:

    [
       'host' => '127.0.0.1', // The IP or the host name of the Redis server
       'port' => 6379, // The TCP port to be used, you can omit it if you use the default port (6379)
       'ttl' => 5, // The connection timeout (in seconds)
       'password' => null, // The password to be used to connect to the Redis server, omit or set to null if none
    ]
    
  2. To define connection via Unix socket, you can use an array like this:

    [
       'socket' => '/run/redis/redis.sock',
       'password' => null, // The password to be used to connect to the Redis server, omit or set to null if none
    ]
    

Concrete supports connecting to multiple Redis servers, and the definition of all the server connections must be stored in a servers configuration key. In addition, if your Redis server serves multiple applications or websites, you may also want to specify which database to be used (via a database configuration key), as well as a prefix for the keys of the cached items (via a prefix configuration key). You finally have to set the preferred_driver configuration key to redis in order to instruct Concrete to use the Redis servers instead of the filesystem.

To summarize, here's an extract from a sample /application/config/concrete.php file that configure Redis for the Concrete caches:

<?php

return [
    'cache' => [
        'levels' => [
            'overrides' => [
                'preferred_driver' => 'redis',
                'drivers' => [
                    'redis' => [
                        'options' => [
                            'database' => 0,
                            'prefix' => 'myapp:overrides',
                            'servers' => [
                                [
                                    'host' => '127.0.0.1',
                                    'port' => 6379,
                                    'ttl' => 5,
                                    'password' => null,
                                ],
                            ],
                        ],
                    ],
                ],
            ],
            'expensive' => [
                'preferred_driver' => 'redis',
                'drivers' => [
                    'redis' => [
                        'options' => [
                            'database' => 0,
                            'prefix' => 'myapp:expensive',
                            'servers' => [
                                [
                                    'host' => '127.0.0.1',
                                    'port' => 6379,
                                    'ttl' => 5,
                                    'password' => null,
                                ],
                            ],
                        ],
                    ],
                ],
            ],
            'object' => [
                'preferred_driver' => 'redis',
                'drivers' => [
                    'redis' => [
                        'options' => [
                            'database' => 0,
                            'prefix' => 'myapp:object',
                            'servers' => [
                                [
                                    'host' => '127.0.0.1',
                                    'port' => 6379,
                                    'ttl' => 5,
                                    'password' => null,
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
];

Session storage

You can use Redis instead of the filesystem to store the data of your visitor sessions. The definition of the Redis server(s) to be used must be done using the concrete.session.redis configuration key (its definition is the same as above), and you have to set the concrete.session.handler to redis.

Here's an extract from a sample /application/config/concrete.php file that configure Redis for the session data:

<?php

return [
    'session' => [
        'handler' => 'redis',
        'redis' => [
            'database' => 0,
            'prefix' => 'myapp:session',
            'servers' => [
                [
                    'host' => '127.0.0.1',
                    'port' => 6379,
                    'ttl' => 5,
                    'password' => null,
                ],
            ],
        ],
    ],
];

Page cache

In order to instruct Concrete to use Redis to store the cached pages, you can configure the concrete.cache.page.redis configuration key as described above, and set the concrete.cache.page.adapter configuration key to redis. You can configure the prefix with the prefix key,

Here's an extract from a sample /application/config/concrete.php file that configure Redis for the page cache:

<?php

return [
    'cache' => [
        'page' => [
            'adapter' => 'redis',
            'redis' => [
                'prefix' => 'myapp:page-cache',
                'database' => 0,
                'servers' => [
                    [
                        'host' => '127.0.0.1',
                        'port' => 6379,
                        'ttl' => 5,
                        'password' => null,
                    ],
                ],
            ],
        ],
    ],
];
Recent Tutorials
Create custom Site Health tasks
Apr 19, 2024
By myq.

This tutorial will guide you through the creation of a new Site Health task

Reusing the same Express entity in multiple associations
Apr 11, 2024
By myq.

How to create and manage multiple associations in Express

Express Form Styling
Apr 11, 2024
By myq.

Different ways to style Express forms

Setting addon/theme version compatibility in the marketplace
Jan 9, 2024

For developers worn out with setting the latest addon or theme version manually across too many core versions, here is a JavaScript bookmarklet to do it for you.

How to get the locale of a page
Jan 8, 2024
By wtfdesign.

Now, why don't we just have a getLocale() method on Page objects beats me, but here's how you work around it

Using a Redis Server
Jun 16, 2023
By mlocati.

How to configure Concrete to use one or more Redis servers to persist the cache.

Improvements?

Let us know by posting here.