User Agents

Web browsers and everyday web tech, explained

How to Enable Cache Blocks in Drupal

· 6 min read · Cookies and Cache

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Enabling cache blocks in Drupal is a powerful way to boost site performance by storing the rendered output of blocks and serving it to subsequent visitors without re-executing the block's code. This reduces server load, speeds up page loads, and improves scalability. Before you start, you need administrative access to your Drupal site and a basic understanding of where blocks are managed.

What You'll Need

  • Administrator or permission to manage blocks and performance settings.
  • Access to the Drupal admin interface (e.g., /admin/structure/block).
  • Knowledge of which blocks should be cached (e.g., static content vs. user-specific content).

Step-by-Step

  1. Log in to your Drupal admin panel. Use an account with permissions to administer blocks and performance settings.
  2. Navigate to the block layout page. Go to Structure > Block layout (or Blocks depending on your Drupal version).
  3. Choose the region and block you want to configure. Click the Configure link next to the block you wish to cache. For example, if you have a custom block, find it in the list under the appropriate region (e.g., Sidebar first).
  4. Open the caching settings. In the block configuration form, scroll down to the Caching section. This section is usually located near the bottom, below the visibility settings.
  5. Select a cache mode. You will see a dropdown or radio buttons with options like:
    • Do not cache – disables caching for this block.
    • Cache per role – caches a separate version for each user role.
    • Cache per user – caches a version for each logged-in user (use sparingly).
    • Cache per page – caches a version for each URL.
    • Cache per role and page – combines role and page caching.
    • Custom cache – allows you to specify a cache context and max age.
    For most static blocks (e.g., a welcome message), choose Cache per page or Cache per role and page.
  6. Set the cache max age (if using custom cache). If you selected Custom cache, you can set a maximum age in seconds (e.g., 3600 for one hour) and define cache contexts (like 'url' or 'user.roles'). This gives fine-grained control.
  7. Save the block configuration. Click the Save block button at the bottom of the form.
  8. Clear the cache to apply changes. After saving, go to Configuration > Development > Performance and click Clear all caches. This ensures the new settings take effect immediately.
  9. Verify caching is working. Load the page where the block appears, then view the page source or use developer tools to check if the block's HTML is served from cache. You can also enable the Drupal cache debug mode (if available) to see cache hits/misses.

Common Mistakes

  • Caching user-specific content incorrectly. If a block shows personalized data (e.g., a shopping cart), caching it per page will show the same content to all users. Always choose a cache mode that respects user context.
  • Forgetting to clear caches after changes. If you modify a block's content or settings and don't clear the cache, you might see stale content.
  • Setting too long a max age for dynamic content. For blocks that update frequently (e.g., latest news), a long max age can make the site look outdated.
  • Not testing after enabling caching. Caching can break functionality if the block relies on session data or cookies. Always test thoroughly after enabling.

Tips

  • Use cache contexts wisely. Drupal's caching system is context-based. The more contexts you add (e.g., 'user', 'url', 'route'), the more cache entries are created, which can increase database size. Only add contexts that are necessary.
  • Leverage the 'Custom cache' option for advanced control. You can specify cache tags and contexts to invalidate the cache when related content changes. For example, if your block shows nodes of a certain type, add the 'node' cache tag.
  • Consider using the 'Cache per role' option for blocks that differ by role but not per user. This reduces the number of cache entries compared to per-user caching.
  • Monitor performance with tools like the Drupal database logging module or the 'Cache' status page. This helps you see how effective your caching is.
  • Combine block caching with page caching. For anonymous users, enabling page caching (in Performance settings) can give even bigger speed gains, but block caching still helps for authenticated users.

Troubleshooting

If you enable block caching and notice issues, here’s what to check:

  • Stale content appears. Clear the cache manually via Configuration > Development > Performance > Clear all caches. Also check if the block's max age is too high.
  • Block doesn't update for logged-in users. Ensure you selected a cache mode that includes user context (e.g., 'Cache per user' or 'Cache per role'). If you used 'Cache per page', logged-in users might see the same content as anonymous users.
  • Block shows wrong content for certain visitors. This usually means the cache context is too broad. For example, if you have a block that displays a personalized greeting, you need 'user' context. Go back to the block settings and adjust the cache mode.
  • Performance doesn't improve. Check if the block is actually being cached by enabling Drupal's cache debug mode (in settings.php or via a module). Look for 'HIT' in the page source. If you see 'MISS', the block might be excluded from caching due to its code (some modules disable caching for their blocks).
  • Errors after enabling caching. If you see PHP errors or blank blocks, try setting the cache mode back to 'Do not cache' and clear caches. Then re-enable with a more conservative mode (e.g., 'Cache per page') and test again.

Frequently Asked Questions

Can I cache blocks for anonymous users only?

Yes. Drupal automatically caches blocks for anonymous users if you enable page caching. For block-level caching, you can set the block to cache per page, which will affect all users, but you can also use the 'Cache per role' option and exclude the authenticated role if needed.

What is the difference between 'Cache per role' and 'Cache per user'?

'Cache per role' creates a single cache entry for each role (e.g., one for admins, one for editors). 'Cache per user' creates a separate cache entry for every logged-in user, which is much more resource-intensive. Use per-user only when the block content is unique to each user.

How do I know if a block is already cached?

You can enable the Drupal cache debug feature by adding $settings['cache']['debug'] = TRUE; to your settings.php file. Then, in the page source, you'll see comments indicating cache hits or misses for each block.

Does block caching work with views blocks?

Yes, views blocks can be cached. In the views settings, you can set caching options (e.g., time-based or tag-based). However, the block-level cache settings in the block configuration will still apply on top of that.

Will caching blocks affect user-specific forms inside the block?

Yes. If a block contains a form (like a login form or a search form), caching it can cause issues because forms rely on session tokens. In such cases, set the block to 'Do not cache' or use a cache mode that includes 'session' context (if available).

Can I set a different max age for each block?

Yes, via the 'Custom cache' option. You can set a max age in seconds and define cache contexts. This allows you to have a short max age for dynamic blocks and a long one for static blocks.