How Drupal Caching Works

Drupal uses cache "bins" for storing temporary data for faster retrieval. Database tables with a "cache" prefix are an example of cache bins, such as cache_block, cache_page, cache_menu, etc. When you clear your Drupal cache, all these tables are truncated and information is readded on-demand.

Authcache deals with the "cache_page" bin, which by default is used for saving cached pages to serve to anonymous users. Drupal uses the page URL as the cache key for looking up cached pages. By modifying this cache key with a special Authcache cookie key, the cache_page bin can be used any combination of anonymous and authenticated user roles.

The Importance of a Cache Handler

Drupal core uses the database for its cache bins, but databases are slow. Installing a cache handler plug-in allows Drupal to use a faster caching technology, such as Memcached or APC, and takes the load off the database. Authcache can then retrieve the cached page without having to establish a database connection. The two popular cache handler projects are Memcache API and CacheRouter (Drupal 6.x only).

In a Drupal install, Authcache is configured as a cache handler in settings.php ("authcache.inc"), however Authcache does not implement a caching system. Instead Authcache detects which cache handler is configured in settings.php, then loads the correct include file. If no cache handler is used, Authcache defaults back to the Drupal core database cache bins (this is useful for testing).

Cache Invalidation

Cache bins are dumb and don't know if their data is stale or up-to-date, so clearing the cache ("cache invalidation") is an important aspect of caching. Drupal automatically clears the cache for common actions using cache_clear_all(), which is called after adding/editing a node, posting a comment, or running the cronjob. Unfortunately, there are currently no hooks to modify cache invalidation.

Caching Pages for Authenticated Users

Authcache acts as a middleman between rendering the page and saving to the cache_page bin. For authenticated users, the final page HTML must be same for every user. This can be challenging, since each page is usually tailored to the user with custom content. Authcache attempts to intercept any customized content and set up a placeholder within the HTML. Then after the page is loaded, an Ajax callback is used to retrieve custom data and fill in the placeholders, dynamically updating the page HTML.

Current Authcache placeholders:

  • Form tokens (logged-in users only; required by Drupal to prevent cross-site request forgery attacks)
  • Tabs (logged-in users)
  • Contact form default values (logged-in user's name)
  • Poll results and vote button
  • "New" tag for comments
  • "Edit" link for comments
  • Number of new threads/replies in forum and the icon to use (hot, new)
  • Username in "navigation" menu block