Index: memcache.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/memcache/memcache.inc,v
retrieving revision 1.15.2.8.2.21
diff -u -r1.15.2.8.2.21 memcache.inc
--- memcache.inc	13 Oct 2010 03:11:31 -0000	1.15.2.8.2.21
+++ memcache.inc	15 Oct 2010 19:45:29 -0000
@@ -23,6 +23,9 @@
  *   'cache_menu', 'cache_page', or 'cache' for the default cache.
  */
 function cache_get($cid, $table = 'cache') {
+  // Alter cache key based on cookies.
+  $cid = memcache_cookie_variant_cid($cid, $table)
+  
   // Determine when the current table was last flushed.
   $cache_flush = variable_get("cache_flush_$table", 0);
   // Retrieve the item from the cache.
@@ -85,6 +88,9 @@
  *   A string containing HTTP header information for cached pages.
  */
 function cache_set($cid, $data, $table = 'cache', $expire = CACHE_PERMANENT, $headers = NULL) {
+  // Alter cache key based on cookies.
+  $cid = memcache_cookie_variant_cid($cid, $table)
+  
   $created = $_SERVER['REQUEST_TIME'];
 
   // Create new cache object.
@@ -141,6 +147,9 @@
  *   match. If '*' is given as $cid, the table $table will be emptied.
  */
 function cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) {
+  // Alter cache key based on cookies.
+  $cid = memcache_cookie_variant_cid($cid, $table)
+  
   // Default behavior for when cache_clear_all() is called without parameters
   // is to clear all of the expirable entries in the block and page caches.
   if (!isset($cid) && !isset($table)) {
@@ -278,3 +287,42 @@
 function memcache_clone($object) {
   return version_compare(phpversion(), '5.0') < 0 ? $object : clone($object);
 }
+
+/**
+ * Generate a new CID based on cookies.
+ *
+ * This only impacts the page cache. It allows sites to cache multiple variants
+ * of a page based on cookies. This is useful when you use cookies to switch
+ * themes (and can be far more efficient than relying upon session info).
+ *
+ * If you are using an external proxy such as Varnish, make sure to configure it
+ * to Respect the Cookie (TM). A well-configured proxy cache will be able to
+ * cache multiple copies of pages, as well.
+ *
+ * The map of cookies->prefixes is configurable in settings.php:
+ *
+ * @code
+ * $conf['cookie_page_cache_variants'] = array('cookie_name' => 'cache_prefix');
+ * @endcode
+ *
+ * @param $cid
+ *  The cache ID.
+ * @param $table
+ *  The cache table. This only modifies cids for the cache_page table.
+ *
+ * @return 
+ *  The CID, modified if necessary.
+ */
+function memcache_cookie_variant_cid($cid, $table) {
+  global $conf;
+  
+  if ($table != 'cache_page') return $cid;
+  
+  foreach ($conf['cookie_page_cache_variants'] as $cookie => $prefix) {
+    if (!empty($_COOKIE[$cookie])) {
+      return $prefix . $cid;
+    }
+  }
+  
+  return $cid;
+}
\ No newline at end of file
