diff --git a/sites/default/modules/memcache/memcache.inc b/sites/default/modules/memcache/memcache.inc
index 61fd101..526149a 100644
--- a/sites/default/modules/memcache/memcache.inc
+++ b/sites/default/modules/memcache/memcache.inc
@@ -3,6 +3,9 @@
 
 require_once 'dmemcache.inc';
 
+// How long to hold lock when rebuilding cached content.
+define('CACHE_REBUILD_MAX', 5);
+
 /** Implementation of cache.inc with memcache logic included **/
 
 /**
@@ -15,6 +18,32 @@ require_once 'dmemcache.inc';
  *   'cache_menu', 'cache_page', or 'cache' for the default cache.
  */
 function cache_get($key, $table = 'cache') {
+  static $lock = 0;
+
+  // We manually track expire times for cached items, allowing us to serve
+  // stale copies while a new copy is being built.
+  $expire = (int)dmemcache_get("E:$key:E", $table);
+  if (time() > $expire) {
+    if (($lock === 0) &&
+        !dmemcache_get("L:$key-$table", 'cache_lock')) {
+      // No other process is rebuilding this content -- grab a lock and we'll
+      // rebuild it ourselves.
+      dmemcache_set("L:$key-$table", 1, time() + CACHE_REBUILD_MAX, 'cache_lock');
+      $lock = TRUE;
+      // By returning nothing, we tell the application to rebuild this content.
+      return NULL;
+    }
+    else if ($lock === TRUE) {
+      // For cache coherency, once we grab one lock, we assume all locks.  We
+      // intentionally don't bother setting subsequent locks as it's extra 
+      // overhead and most often will be unnecessary.
+      return NULL;
+    }
+    else {
+      $lock = FALSE;
+      // Fall through and return cached version, if any.
+    }
+  }
   return dmemcache_get($key, $table);
 }
 
@@ -55,10 +84,22 @@ function cache_set($cid, $table = 'cache', $data, $expire = CACHE_PERMANENT, $he
   $cache->headers = $headers;
 
   // Save to memcache
-  if ($expire == CACHE_TEMPORARY) {
-    $expire = variable_get('cache_lifetime', 2591999);
+  if ($expire == CACHE_TEMPORARY || $expire == CACHE_PERMANENT) {
+    $expire = time() + variable_get('cache_lifetime', 2591999);
   }
-  dmemcache_set($cid, $cache, $expire, $table);
+  // During early bootstrap, variable_get can return 0.
+  if ($expire < 1) {
+    $expire = time() + 2591999;
+  }
+
+  // Manually track expirations, instead of having memcache track expirations.
+  // This allows us to gracefully avoid cache stampedes.  We store the
+  // expiratoin timestamp in the same $table as the cached data so if the
+  // table is flushed we also flush any expiration timestamps.
+  dmemcache_set("E:$cid:E", $expire, 0, $table);
+
+  // Tell memcache to cache this content as long as possible.
+  dmemcache_set($cid, $cache, 0, $table);
 }
 
 /**
@@ -66,13 +107,17 @@ function cache_set($cid, $table = 'cache', $data, $expire = CACHE_PERMANENT, $he
  * Expire data from the cache. If called without arguments, expirable
  * entries will be cleared from the cache_page table.
  *
+ * Memcache logic is simpler than the core cache because memcache doesn't have
+ * a minimum cache lifetime consideration (it handles it internally), and 
+ * doesn't support wildcards.  Wildcard flushes result in the entire table
+ * being flushed.
+ *
  * @param $cid
  *   If set, the cache ID to delete. Otherwise, all cache entries that can
- *   expire are deleted.
+ *   expire are deleted from the specified table.
  *
  * @param $table
- *   If set, the table $table to delete from. Mandatory
- *   argument if $cid is set.
+ *   If set, the table to delete from.
  *
  * @param $wildcard
  *   If set to TRUE, the $cid is treated as a substring
@@ -80,10 +125,12 @@ function cache_set($cid, $table = 'cache', $data, $expire = CACHE_PERMANENT, $he
  *   match. If '*' is given as $cid, the table $table will be emptied.
  */
 function cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) {
-  // Memcache logic is simpler because memcache doesn't have a minimum cache
-  // lifetime consideration (it handles it internally), and doesn't support
-  // wildcards.
-  $bin = empty($table) ? 'cache_page' : $table;
+  // If cid and table are not set, we should flush the cache_page table.
+  if (!isset($cid) && !isset($table)) {
+    $cid = '*';
+    $wildcard = TRUE;
+    $table = 'cache_page';
+  }
   if (empty($cid) || ($cid == '*' && $wildcard !== TRUE)) {
     // don't do anything if cid is unset. this matches the default drupal behavior...
     if ($wildcard && $cid != '*') {
@@ -97,7 +144,7 @@ function cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) {
     dmemcache_flush($table);
   }
   else {
-    dmemcache_delete($cid, $table);
+    dmemcache_delete("E:$cid:E", $table);
   }
 }
 
