My site is very much faster with page caching on. However, I have a custom module that serves 5 different slightly different versions of my content. In other words, people in New York see East Coast stories, but not California stories, but everyone sees national stories. This is mostly done through a custom Views filter that checks for a cookie.

Each node is tagged with an internal taxonomy that determines which areas to show that content in lists generated by views. The actual content isn't hidden or different based on the cookie, just the lists of events, local stories, etc.

Is there a way of serving cache A to people with cookie A, cache B to people with Cookie B, etc?

Code from the custom Views filter-

function query(){

  $invert = $this->options['invert'];
  //Get the taxonomy id corresponding to the user's location from a cookie.
  $office_tid = MCC_location_get_cookie("tid");

  //Get the taxonomy id of the country- code used on 2 sites
  $country_name = variable_get('MCC_location_site')?"U.S.":"Canada"; 
  $country_tid = taxonomy_get_term_by_name($country_name,'regions');
  $country_tid = current($country_tid)->tid;

  if($office_tid == $country_tid){
    // An area without an office was selected, such as Yukon, filter should act as if no province has been selected
    $case = $this->options['office_not_set'];

  $join = new views_join();

  $join->definition = array(
    'table' => 'taxonomy_index',
    'field' => 'nid',
    'left_table' => 'node',
    'left_field' => 'nid',
  $this->query->add_table("taxonomy_index", NULL, $join);
  $this->query->add_table("taxonomy_index", NULL, $join, "taxonomy_index_for_office");

  $this->query->add_where_expression($this->options['group'], "taxonomy_index_for_office.tid = ".$office_tid." AND taxonomy_index_for_office.nid NOT IN (SELECT taxonomy_index_for_office.nid FROM taxonomy_index AS taxonomy_index_for_office WHERE taxonomy_index_for_office.tid = ".$country_tid.")");
  • 1
    Yes, there is. And that's pretty much all we can really tell without seeing your custom code and knowing your cache strategy and settings.
    – Mołot
    Oct 13, 2014 at 14:17
  • Currently, caching is off completely. I'm trying to figure out how to turn it on while maintaining functionality. I am hoping to be able to accomplish it with the caching available in core. What code would be helpful here? The query() from the custom filter? Something else from that section? I'm not sure what would be helpful.
    – James
    Oct 13, 2014 at 14:29
  • "I am hoping to be able to accomplish it with the caching available in core" - probably not going to happen. But without seeing how do you detect what set of pages you want to show, we can't tell. You wrote custom code to determine this, right? So that's the code you should add to the question.
    – Mołot
    Oct 13, 2014 at 14:32
  • Thanks for the help, Molot. I've added what I think is the crucial code here.
    – James
    Oct 13, 2014 at 17:37

1 Answer 1


If you're using Varnish, you can simply add the cookie as a hash key - there's lots of information about how to do this.

One example and one based on mobile devices.

If you mean Drupals internal page cache then maybe look to this module.

  • Thanks. I'm testing Cookie-Aware Page Cache right now, and it's looking promising.
    – James
    Oct 13, 2014 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.