- π¨π¦Canada tondeuse
The following solution works for me on Drupal 10.4. Thanks for all for the ideas and tips, this is team works, as usual.
In this format, I may use redis only for local usage, and the performance benefits it provides. The files, variables and module may be absent on hosted site instance without failure.
My dependency to drupal/redis is declared as a DEV dependency in my composer file, in this case.
In my Dockerfile, for all cases
FROM php:8.3-apache ARG REDIS_PORT ARG REDIS_HOST ARG REDIS_PASSWORD ARG ENABLE_REDIS_CACHE ARG LOCAL_DEV # Install Redis PHP extension RUN pecl install redis \ && docker-php-ext-enable redis
In my Dockerfile, when running in local dev mode :
An enterprise service inject these files into our hosted containers, this is a solution for local dev that also works for hosted site instances.
# Config for local dev RUN \ if [ "$LOCAL_DEV" = "TRUE" ]; \ [...] if [ "$ENABLE_REDIS_CACHE" = "TRUE" ]; \ then mkdir -p /var/run/secrets/vdmtl/redis; \ echo "Redis configuration"; \ echo "$REDIS_PASSWORD" > /var/run/secrets/vdmtl/redis/password; \ echo "$REDIS_HOST" > /var/run/secrets/vdmtl/redis/host; \ echo "$REDIS_PORT" > /var/run/secrets/vdmtl/redis/port; \ fi; \ [...] fi
In my docker-compose file :
web: container_name: web-${APP_NAME} links: - database:localhost build: context: . dockerfile: Dockerfile args: - LOCAL_DEV=${LOCAL_DEV} - REDIS_PORT=${REDIS_PORT} - REDIS_HOST=${REDIS_HOST} - REDIS_PASSWORD=${REDIS_PASSWORD} - ENABLE_REDIS_CACHE=${ENABLE_REDIS_CACHE} environment: ENV: ${ENV} LOCAL_DEV: ${LOCAL_DEV} ENABLE_REDIS_CACHE: ${ENABLE_REDIS_CACHE} redis: container_name: redis-${APP_NAME} image: redis:${REDIS_VERSION} restart: always ports: - ${REDIS_PORT}:6379 command: --port ${REDIS_PORT} expose: - ${REDIS_PORT} volumes: - redis-cache:/data:delegated
In my local .env file :
LOCAL_DEV=TRUE # Redis REDIS_PORT=6382 REDIS_VERSION=6.2.7 REDIS_HOST='redis' REDIS_PASSWORD= ENABLE_REDIS_CACHE=TRUE
In web/sites/default/settings.php
/** * Redis config -- import as needed, check if extension is installed first */ $redis = getenv('ENABLE_REDIS_CACHE') === 'TRUE' ? true : false; $redis_extension_installed = file_exists($app_root . '/modules/contrib/redis/redis.info.yml'); if (file_exists($app_root . '/' . $site_path . '/settings.redis.php') && $redis && $redis_extension_installed) { include($app_root . '/' . $site_path . '/settings.redis.php'); }
In web/sites/default/settings.redis.php
<?php /** * Redis settings file * @see https://git.drupalcode.org/project/redis/-/blob/8.x-1.x/settings.redis.example.php?ref_type=heads * * Leverages the php redis ressources installed from dockerfile * Will set the redis config before drupal bootstrap, even if the Redis module * is not yet installed */ use Drupal\Core\Installer\InstallerKernel; if (!InstallerKernel::installationAttempted() && extension_loaded('redis')) { // Set Redis as the default backend for any cache bin not otherwise specified. $settings['state_cache'] = TRUE; $redis_host = trim(file_get_contents('/var/run/secrets/vdmtl/redis/host'), "\r\n"); $redis_password = trim(file_get_contents('/var/run/secrets/vdmtl/redis/password'), "\r\n"); $redis_port = trim(file_get_contents('/var/run/secrets/vdmtl/redis/port'), "\r\n"); $redis_password = $redis_password === '' ? NULL : $redis_password; // Apply changes to the container configuration to better leverage Redis. // This includes using Redis for the lock and flood control systems, as well // as the cache tag checksum. Alternatively, copy the contents of that file // to your project-specific services.yml file, modify as appropriate, and // remove this line. $settings['cache']['default'] = 'cache.backend.redis'; $settings['redis_compress_length'] = 100; $settings['container_yamls'][] = 'modules/contrib/redis/redis.services.yml'; $class_loader->addPsr4('Drupal\\redis\\', 'modules/contrib/redis/src'); $settings['bootstrap_container_definition'] = [ 'parameters' => [], 'services' => [ 'redis.factory' => [ 'class' => 'Drupal\redis\ClientFactory', ], 'cache.backend.redis' => [ 'class' => 'Drupal\redis\Cache\CacheBackendFactory', 'arguments' => ['@redis.factory', '@cache_tags_provider.container', '@serialization.phpserialize'], ], 'cache.container' => [ 'class' => '\Drupal\redis\Cache\PhpRedis', 'factory' => ['@cache.backend.redis', 'get'], 'arguments' => ['container'], ], 'cache_tags_provider.container' => [ 'class' => 'Drupal\redis\Cache\RedisCacheTagsChecksum', 'arguments' => ['@redis.factory'], ], 'serialization.phpserialize' => [ 'class' => 'Drupal\Component\Serialization\PhpSerialize', ], ], ]; try { $redis = new Redis(); $redis->connect($redis_host, $redis_port); if ($redis->IsConnected()) { $redis->auth($redis_password); $response = $redis->ping(); if ($response) { # Redis cache configuration $settings['cache_prefix'] = 'dm_'; $settings['redis.connection']['host'] = $redis_host; $settings['redis.connection']['password'] = $redis_password; $settings['redis.connection']['port'] = $redis_port; $settings['redis.connection']['instance'] = 'cache'; $settings['redis.connection']['interface'] = 'PhpRedis'; $settings['cache']['default'] = 'cache.backend.redis'; $settings['container_yamls'][] = $app_root . '/modules/contrib/redis/example.services.yml'; $settings['redis_compress_length'] = 100; $settings['redis_compress_level'] = 3; $conf['redis_perm_ttl'] = 2592000; $conf['redis_flush_mode'] = 1; } } } catch (Exception $e) { } }