Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Status
colourYellow
titleDraft

Warning

DEPRECATED

The information below is either outdated, or no longer considered best practice at Kalamuna.

What is Conditional Caching?

Sometimes you only want to refer anonymous users to cached content if certain conditions apply. This is conditional caching.

Use Case: Google's "First Click Free

...

" Policy

Google demotes/bans results from publishers who show different content to web crawlers and people. They also demote/ban publishers from having content in Google News if it does not show ALL the article content.

For publishers who want to require registration for full-content access, Google has instituted a "First Click Free" policy. Simply put, if you let people coming from Google domains see all content, then Google's ok with you. Note this is only for their first page view that was referred from a Google domain; subsequent pages they view can show truncated content.

A First Click Free Implementation Using Dynamic Cache on Pantheon

On Pantheon, Varnish makes it very difficult to do conditional caching for anonymous users, since you won't be able to run back-end code to serve different responses after Varnish caches the first response and uses it for subsequent requests.

To get around this, you'll need to disable Drupal's page cache (since this enables Varnish for all anonymous requests). But never fear, we'll get your cached responses back.

The Dynamic Cache module allows you to set a global variable to turn on and off cached page responses if you use it within a hook_boot() implementation. The following hook_boot() sees if anonymous traffic is coming from Google; if it is, it keeps cache disabled. Otherwise, it'll enable caching and use cached responses (or generate one):


FCF w/Dynamic Cache
Code Block
languagephp
function fcf_boot() {
  if (!$GLOBALS['user']->uid) {
    global $user;
    // Determine if Google is the referrer.
    $host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); // subdomain.google.com
    $parts = array_reverse(explode('.', $host)); // (com, google, subdomain)
    // Make sure we have at least 3 parts to avoid undefined array key notices.
    while (count($parts) < 3) {
      $parts[] = '';
    }
    list($tld, $domain, $subdomain) = $parts;

    if ($domain == "google" ) {
      // Set a variable (only good for current request) that other
      // applications can use to determine if this is a FCF instance.
      $_POST['fcf'] = TRUE;
    } else {
      // Enable cache for non-google requests.
      $GLOBALS['conf']['cache'] = TRUE;
    }
  }
}

First Click Free Not on Pantheon

If you're doing this outside of Pantheon and either don't have Varnish or can do some Varnish configuration wizardry to not worry about it, you could probably do the inverse of this approach: leave page cache on, and conditionally disable it for Google referred requests. Probably doesn't matter, but seems aesthetically more pleasing.

Potential Downsides

I haven't done any rigorous performance testing, but I have to assume that doing the Drupal bootstrap up to the point of our hook_boot() implementation must have some performance implications. However, I think these are probably minor, and if you're looking

Resources

Dynamic Cache: Allows you to skip Drupal's page cache for a given page (as you probably surmised, will not skip Varnish).

...

https://www.drupal.org/node/875152: D6-relevant thread, which outlines the approach that was used to create Dynamic Cache.

...

Review History

Who

When

Status

Bob

20230509

Deprecated