Skip to main content

Implementation of a Redis cache for a Drupal cache

Drupal offers several caching systems, including APCu and backend.
For applications developed under Drupal, particular emphasis must be placed on caching, and Redis is a high-performance system that already has a module to facilitate its integration and configuration.

Server-level installation

sudo apt install redis-server php8.3-redis

Installation on Drupal:

composer require 'drupal/redis:^1.10'
Vendor/bin/drush en redis

Server-level configuration: /etc/redis/redis.conf

bind 127.0.0.1
port 6379
maxmemory 4GB                # 10-15% de la RAM totale
maxmemory-policy allkeys-lru # Éviction LRU si mémoire pleine
save ""                      # Désactive la persistance (optionnel pour cache)
requirepass "votre_mot_de_passe_secure" # Sécurité !

Start redis:

sudo systemctl restart redis-server
sudo systemctl enable redis-server



Configuration in Drupal's settings.php file:

/**
 * Configuring PhpRedis
 */
// ////////////////////////
// Enable Redis for cache/sessions/locks
$settings['redis.connection']['interface'] = 'PhpRedis';
$settings['redis.connection']['host'] = '127.0.0.1';
$settings['redis.connection']['password'] = 'your_password';
$settings['redis.connection']['port'] = 6379;
$settings['redis.connection']['persistent'] = TRUE;

// Default cache (main bins)
$settings['cache']['default'] = 'cache.backend.redis';

/**
 * Getting started with Drupal.
  */
// Booting the Drupal kernel (using ACPu if available).
$settings['cache']['bins']['bootstrap'] = 'cache.backend.chainedfast';
// Active configuration (YAML imported into DB)
$settings['cache']['bins']['config'] = 'cache.backend.chainedfast';
// Discovering plugins
$settings['cache']['bins']['discovery'] = 'cache.backend.chainedfast';
/**
 * Heavy bin
 */
$settings['cache']['bins']['render'] = 'cache.backend.redis';
$settings['cache']['bins']['page'] = 'cache.backend.redis';

// Redis sessions (optional but recommended for consistency)
$settings['session_storage']['container'] = 'session.storage.redis';
$settings['redis_serialization_php_igbinary'] = TRUE; # If igbinary is installed
                                                      
// Redis locks (avoids cluster conflicts)
$settings['container_yamls'][] = 'modules/contrib/redis/example.services.yml';
$settings['container_yamls'][] = 'modules/contrib/redis/redis.services.yml';

Verify that the Redis cache is being used correctly:

dd(\Drupal\Core\Cache\Cache::getBins());

Some useful commands for redis


To connect:

redis-cli

Log in (replace [password] with your password):

127.0.0.1:6379> AUTH [password]

Exit:

127.0.0.1:6379> QUIT
#Displays the number of stored keys:
127.0.0.1:6379> DBSIZE
#information about memory:
127.0.0.1:6379> INFO memory
#Scan the data:
127.0.0.1:6379> SCAN 0 MATCH drupal* COUNT 50

 

 

 

 

 

 

 

Profile picture for user admin Stephane K

Écrit le

Il y'a 4 days
Modifié
Il y'a 4 days
Loading ...
WhatsApp
Habeuk Support: +49 152 108 01753
Send