Hướng Dẫn Cài Đặt và Cấu Hình Redis Cache Cho Drupal và DDEV

Hướng Dẫn Cài Đặt và Cấu Hình Redis Cache Cho Drupal và DDEV

Redis cache là công cụ lưu trữ dữ liệu trong bộ nhớ mạnh mẽ, giúp tăng tốc website đáng kể. Hướng dẫn này sẽ chỉ bạn cách cài đặt Redis trên Linux, cấu hình cho Drupal với mã code đầy đủ của settings.redis.php, và tích hợp với DDEV.

Bước 1: Cài đặt Redis Server trên Linux

Cài đặt Redis và tiện ích mở rộng PhpRedis:

  • Cài đặt gói phụ thuộc:

    bash
    sudo apt install php-dev php-pear
     
  • Cài đặt PhpRedis:

    bash
    sudo pecl install redis
     
Bước 2: Cấu hình Redis

Chỉnh sửa tệp /etc/redis/redis.conf (ví dụ: Ubuntu 18) để tối ưu bộ nhớ đệm:

  • Tắt lưu xuống đĩa: save ""
  • Giới hạn bộ nhớ: maxmemory 4gb (30% dung lượng bộ đệm)
  • Sử dụng chính sách LRU: maxmemory-policy allkeys-lru
  • Tắt appendfsync: appendfsync no
  • Kích hoạt luồng I/O: io-threads 2io-threads-do-reads yes
Bước 3: Cấu hình Redis cho Drupal

Tạo tệp settings.redis.php với mã code sau để kích hoạt bộ nhớ đệm Redis:

php
<?php // #Use for redis cache use Drupal\Core\Installer\InstallerKernel; if (!InstallerKernel::installationAttempted() && extension_loaded('redis') && class_exists('Drupal\redis\ClientFactory')) { // Customize host and port. $settings['redis.connection']['host'] = '127.0.0.1'; $settings['redis.connection']['port'] = 6379; // Customize used interface. $settings['redis.connection']['interface'] = 'PhpRedis'; $settings['cache']['default'] = 'cache.backend.redis'; $settings['cache']['bins']['config'] = 'cache.backend.redis'; $settings['cache']['bins']['discovery'] = 'cache.backend.redis'; $settings['cache']['bins']['bootstrap'] = 'cache.backend.redis'; $settings['cache']['bins']['render'] = 'cache.backend.redis'; $settings['cache']['bins']['page'] = 'cache.backend.redis'; $settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.redis'; // Use compression for cache entries longer than the specified limit. $settings['redis_compress_length'] = 100; $settings['container_yamls'][] = 'modules/contrib/redis/example.services.yml'; $settings['container_yamls'][] = 'modules/contrib/redis/redis.services.yml'; $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', ], ], ]; }
 

Thêm vào tệp settings.php:

php
if (file_exists(__DIR__ . '/settings.redis.php')) { include __DIR__ . '/settings.redis.php'; }
 
Bước 4: Redis với DDEV

Với DDEV, sử dụng tiện ích bổ sung Redis:

  1. Cài đặt:

    bash
    ddev add-on get ddev/ddev-redis ddev restart
     
  2. Cập nhật settings.redis.php cho DDEV:

    php
    $settings['redis.connection']['host'] = 'redis';
     

Tìm hiểu thêm tại DDEV Redis GitHub.

Tại sao chọn Redis Cache?

Redis giảm tải cơ sở dữ liệu và tăng tốc hiển thị trang, lý tưởng cho website Drupal, thương mại điện tử và hơn thế nữa.

Kêu gọi hành động: Thực hiện hướng dẫn này để tối ưu website với Redis cache! Có thắc mắc? Hãy để lại câu hỏi bên dưới.

Working Contact Form with Ajax & PHP

Get a functional and working contact form with Ajax & PHP in a few minutes. Just copy and paste the files, add a little code and you’re done.

Download Now