Caching

Caching

Moodle uses a sophisticated caching framework called MUC (Moodle Universal Cache) to improve performance by reducing database queries and expensive computations. Understanding and properly configuring the caching system is one of the most impactful optimizations you can make for a Moodle site.

Understanding MUC

MUC provides a standardized API for caching data throughout Moodle. It defines three types of cache:

  • Application cache: Shared across all users and requests. Contains data like language strings, plugin configurations, and theme CSS. Persists until explicitly invalidated.
  • Session cache: Per-user data that persists for the duration of a user's session. Contains data like navigation structure and user preferences.
  • Request cache: Data that only lasts for a single page request. Used for frequently-accessed data within a single request cycle.

Cache Stores

Cache stores are the backends that physically store cached data. Moodle supports several store types out of the box:

File Cache (Default)

The default cache store uses the file system (within moodledata). It requires no additional software but is slower than in-memory alternatives. Suitable for small to medium sites.

Redis

Redis is a high-performance in-memory data store ideal for Moodle caching. It provides excellent performance and supports all cache types. To use Redis:

  1. Install Redis server: sudo apt install redis-server
  2. Install the PHP Redis extension: sudo apt install php-redis
  3. Navigate to Site administration > Plugins > Caching > Configuration
  4. Under Installed cache stores, click Add instance next to Redis
  5. Configure the Redis connection:
    • Store name: A descriptive name (e.g., "Primary Redis")
    • Server: Redis hostname (e.g., 127.0.0.1)
    • Port: Default is 6379
    • Password: Redis password if configured
    • Key prefix: Prefix to avoid collisions if sharing Redis with other applications

Memcached

Memcached is another popular in-memory caching system. To use Memcached:

  1. Install Memcached: sudo apt install memcached
  2. Install the PHP Memcached extension: sudo apt install php-memcached
  3. Add a Memcached store instance in the caching configuration page
  4. Configure the server addresses (multiple servers for distributed caching)

APCu

APCu (APC User Cache) is a local in-memory cache that is very fast but only available within a single web server process. It is suitable for single-server deployments. Install with: sudo apt install php-apcu. APCu works best for application and request caches but is not suitable for session caches in load-balanced environments.

Configuring Cache Mappings

Navigate to Site administration > Plugins > Caching > Configuration. This page shows:

  • Installed cache stores: Available backends and their instances
  • Known cache definitions: All cache definitions registered by Moodle core and plugins
  • Cache store mappings: Which store instance handles each cache type

The recommended configuration for a high-performance site:

  • Application cache: Redis or APCu (with Redis as backup)
  • Session cache: Redis or Database
  • Request cache: Static (in-memory PHP) — this is the default and optimal choice

Configuring Caching via config.php

For automated deployments, you can pre-configure cache stores in config.php:

$CFG->alternative_cache_factory_class = 'tool_forcedcache\cache_factory';

Or define cache stores directly:

define('CACHE_DISABLE_ALL', false);
define('CACHE_DISABLE_STORES', false);

Purging Caches

You can purge all caches from Site administration > Development > Purge all caches or via the CLI:

php admin/cli/purge_caches.php

Purging caches temporarily increases server load as everything is regenerated. Avoid purging caches during peak usage.

Cache Performance Testing

Moodle includes a cache performance test accessible from the caching configuration page. This test measures read and write speeds for each configured cache store, helping you compare performance between different backends and make informed decisions about cache mappings.

Troubleshooting

  • Stale data after updates: Purge all caches after upgrading Moodle or plugins
  • Redis connection errors: Verify Redis is running (redis-cli ping) and the PHP extension is loaded
  • High memory usage: Monitor Redis/Memcached memory consumption and set appropriate limits
  • Cache is disabled warning: Check that CACHE_DISABLE_ALL is not set to true in config.php

¿Le ha resultado útil este artículo?

  • Site Administration Overview

    Site Administration Overview The Site administration panel is the central hub for managing all aspec...
  • Server Settings

    Server Settings Moodle provides extensive server configuration options that control how the applicat...
  • Security Settings

    Security Settings Moodle provides a comprehensive set of security settings to protect your site from...
  • Backup Settings

    Backup Settings Moodle provides a built-in course backup system that creates compressed archives of ...
  • Performance Tuning

    Performance Tuning Optimizing Moodle performance involves a combination of server-level configuratio...