Skip to main content

In Drupal, caching is a critical aspect of optimizing site performance, and two key concepts in caching are Cache Keys and Cache Tags. While both play a role in improving performance, they serve distinct purposes. Understanding the difference between them is essential for developers to effectively manage caching and ensure optimal site performance.

Cache Keys

Cache Keys are used to uniquely identify a cache entry. A cache key typically represents a specific combination of data and request parameters that determines how a piece of content is cached. When a cache entry is created, the cache key helps Drupal store and retrieve the correct data.

For example, if you are caching content based on a user's session or specific conditions, the cache key will vary to reflect those changes. If the cache key is the same, Drupal will serve the same cached data.

Code Example:

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheBackendInterface;

/**
 * Cache data with a custom cache key.
 */
function mymodule_cache_data() {
  $cache_key = 'custom_cache_key:' . \Drupal::currentUser()->id();
  $cache = \Drupal::cache()->get($cache_key);

  if ($cache) {
    // Return cached data.
    return $cache->data;
  }
  else {
    // Generate data and store in cache.
    $data = 'Some dynamic data';
    \Drupal::cache()->set($cache_key, $data);
    return $data;
  }
}

In this example, the cache key is based on the current user's ID, so the data will be cached separately for each user.

Cache Tags

Cache Tags allow Drupal to invalidate caches that depend on specific entities or configurations when those entities or configurations change. Cache tags are associated with cached content and help clear or update the cache when the content changes. Cache tags are typically tied to content entities like nodes, taxonomy terms, or user data.

Cache tags are useful for managing cache invalidation, ensuring that changes to content trigger the necessary cache invalidation across the system.

Code Example:

use Drupal\Core\Cache\CacheableMetadata;

/**
 * Cache data with associated cache tags.
 */
function mymodule_cache_data_with_tags() {
  $cache_key = 'custom_cache_key:' . \Drupal::currentUser()->id();
  $cache = \Drupal::cache()->get($cache_key);

  if ($cache) {
    // Return cached data.
    return $cache->data;
  }
  else {
    // Generate data and store in cache.
    $data = 'Some dynamic data';
    
    // Cache with tags related to current user.
    \Drupal::cache()->set($cache_key, $data, CacheBackendInterface::CACHE_PERMANENT, ['user:' . \Drupal::currentUser()->id()]);
    return $data;
  }
}

In this example, the cache entry is tagged with the user's ID. When any user-related data changes, the cache tagged with user:<user_id> will be invalidated.

Key Differences

  • Cache Keys uniquely identify a cached item, ensuring Drupal retrieves the correct data.
  • Cache Tags are used to invalidate caches when related content (like nodes, users, or other entities) changes.

When to Use Cache Keys vs. Cache Tags

  • Cache Keys: Use them when you need to differentiate cached data based on specific conditions (e.g., user ID, request parameters).
  • Cache Tags: Use them when you need to associate cached data with content or entities so that the cache can be invalidated automatically when those entities change.

Conclusion

Both Cache Keys and Cache Tags are integral to managing Drupal's caching system efficiently. Cache keys ensure the correct data is retrieved from cache, while cache tags help manage cache invalidation when related content changes. By understanding how and when to use each, developers can build more efficient, faster, and scalable Drupal sites.