Index: memcache.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/memcache/memcache.inc,v
retrieving revision 1.28.2.18
diff -u -p -r1.28.2.18 memcache.inc
--- memcache.inc	13 Oct 2010 03:18:49 -0000	1.28.2.18
+++ memcache.inc	19 Nov 2010 16:08:26 -0000
@@ -15,10 +15,11 @@ class MemCacheDrupal implements DrupalCa
     $this->memcache = dmemcache_object($bin);
     $this->bin = $bin;
 
-    $this->wildcard_flushes = variable_get('memcache_wildcard_flushes', array());
+    $this->wildcard_flushes = memcache_variable_get('memcache_wildcard_flushes', array());
     $this->invalidate = variable_get('memcache_wildcard_invalidate', MEMCACHE_WILDCARD_INVALIDATE);
     $this->cache_lifetime = variable_get('cache_lifetime', 0);
-    $this->cache_flush = variable_get('cache_flush_' . $this->bin);
+    $bin_flushes = memcache_variable_get('memcache_bin_flushes', array());
+    $this->cache_flush = isset($bin_flushes[$this->bin]) ? $bin_flushes[$this->bin] : 0;
     $this->flushed = min($this->cache_flush, REQUEST_TIME - $this->cache_lifetime);
   }
   function get($cid) {
@@ -141,7 +142,10 @@ class MemCacheDrupal implements DrupalCa
         // retrieving data from this bin, we will compare the cache creation
         // time minus the cache_flush time to the cache_lifetime to determine
         // whether or not the cached item is still valid.
-        variable_set("cache_flush_$this->bin", REQUEST_TIME);
+        $bin_flushes = memcache_variable_get('memcache_bin_flushes', array());
+        $bin_flushes[$this->bin] = REQUEST_TIME;
+        memcache_variable_set('memcache_bin_flushes', $bin_flushes);
+
         $this->flushed = REQUEST_TIME;
 
         // We store the time in the current user's session which is saved into
@@ -242,7 +246,7 @@ class MemCacheDrupal implements DrupalCa
           ($_SERVER['REQUEST_TIME'] - $this->wildcard_flushes[$this->bin][$length] > $this->invalidate / 4)) {
         $this->wildcard_flushes = variable_get('memcache_wildcard_flushes', array());
         $this->wildcard_flushes[$this->bin][$length] = $_SERVER['REQUEST_TIME'];
-        variable_set('memcache_wildcard_flushes', $this->wildcard_flushes);
+        memcache_variable_set('memcache_wildcard_flushes', $this->wildcard_flushes);
       }
       $wildcard = dmemcache_key('.wildcard-' . $cid, $this->bin);
       if (isset($wildcards[$this->bin][$wildcard]) && $wildcards[$this->bin][$wildcard] != 0) {
@@ -276,3 +280,50 @@ class MemCacheDrupal implements DrupalCa
   }
 }
 
+/**
+ * Implementation of variable_get() that avoids writing to the database.
+ *
+ * This should only be used for data that can be safely discarded if there is
+ * a full memcache flush in the case of a restart or similar, and which are
+ * volatile (and hence prone to stampedes).
+  Also note that each variable requested using this function will add an additional
+ * dmemcache_get() to the request.
+ *
+ * @param $name
+ *   The name of the variable to return.
+ * @param $default
+ *   The default value to use if this variable has never been set.
+ */
+function memcache_variable_get($name, $value) {
+  global $memcache_conf;
+  if (!isset($memcache_conf[$name])) {
+    // Store the variable as array('value' => $value); to differentiate between
+    // variables that aren't set, and variables set to FALSE.
+    $result = dmemcache_get($name, 'semaphore');
+    if ($result) {
+      $memcache_conf[$name] = $result['value'];
+    }
+    else {
+      $memcache_conf[$name] = $value;
+    }
+  }
+
+  return $memcache_conf[$name];
+}
+
+/**
+ * Implementation of variable_set() that avoids writing to the database or clearing the variables cache.
+ *
+ * This should only be used for data that can be safely discarded if there is a
+ * full memcache flush in the case of a restart or similar.
+ *
+ * @param $name
+ *   The name of the variable.
+ * @param $default
+ *   The default value to use if this variable has never been set.
+ */
+function memcache_variable_set($name, $value) {
+  global $memcache_conf;
+  dmemcache_set($name, array('value' => $value), 0, 'semaphore');
+  $memcache_conf[$name] = $value;
+}
