Index: memcache.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/memcache/memcache.inc,v
retrieving revision 1.15.2.10
diff -b -u -p -r1.15.2.10 memcache.inc
--- memcache.inc	26 Jan 2009 21:18:10 -0000	1.15.2.10
+++ memcache.inc	4 Aug 2009 08:02:23 -0000
@@ -8,14 +8,32 @@ require_once 'dmemcache.inc';
 /**
  * Return data from the persistent cache.
  *
- * @param $key
+ * @param $cid
  *   The cache ID of the data to retrieve.
  * @param $table
  *   The table $table to store the data in. Valid core values are 'cache_filter',
  *   'cache_menu', 'cache_page', or 'cache' for the default cache.
  */
-function cache_get($key, $table = 'cache') {
-  return dmemcache_get($key, $table);
+function cache_get($cid, $table = 'cache') {
+  // Determine when the current table was last flushed.
+  $cache_flush = variable_get("cache_flush_$table", 0);
+  // Retrieve the item from the cache.
+  $cache = dmemcache_get($cid, $table);
+  if (is_object($cache)) {
+    $cache_tables = isset($_SESSION['cache_flush']) ? $_SESSION['cache_flush'] : NULL;
+    // Items cached before the cache was last flushed are no longer valid.
+    $cache_lifetime = variable_get('cache_lifetime', 0);
+    if ($cache_lifetime && $cache->created && $cache_flush &&
+        ($cache->created < $cache_flush) &&
+        ((time() - $cache->created >= $cache_lifetime)) ||
+        (is_array($cache_tables) && $cache_tables[$table] &&
+        $cache_tables[$table] > $cache->created)) {
+      // Cache item expired, return NULL.
+      return 0;
+    }
+    return $cache;
+  }
+  return 0;
 }
 
 /**
@@ -51,13 +69,17 @@ function cache_set($cid, $table = 'cache
   $cache->cid = $cid;
   $cache->data = is_object($data) ? memcache_clone($data) : $data;
   $cache->created = $time;
+  // Expire time is in seconds if less than 30 days, otherwise is a timestamp.
+  if ($expire != CACHE_PERMANENT && $expire < 2592000) {
+    // Expire is expressed in seconds, convert to the proper future timestamp
+    // as expected in dmemcache_get().
+    $cache->expire = time() + $expire;
+  }
+  else {
   $cache->expire = $expire;
+  }
   $cache->headers = $headers;
 
-  // Save to memcache
-  if ($expire == CACHE_TEMPORARY) {
-    $expire = variable_get('cache_lifetime', 2591999);
-  }
   // We manually track the expire time in $cache->expire.  When the object
   // expires, we only allow one request to rebuild it to avoid cache stampedes.
   // Other requests for the expired object while it is still being rebuilt get
@@ -104,8 +126,30 @@ function cache_clear_all($cid = NULL, $t
     }
   }
   else if ($cid == '*' || $wildcard === TRUE) {
+    if (variable_get('cache_lifetime', 0)) {
+      // Update the timestamp of the last global flushing of this table.  When
+      // retrieving data from this table, 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_$table", time());
+
+      // We store the time in the current user's session which is saved into
+      // the sessions table by sess_write().  We then simulate that the cache
+      // was flushed for this user by not returning cached data to this user
+      // that was cached before the timestamp.
+      if (is_array($_SESSION['cache_flush'])) {
+        $cache_tables = $_SESSION['cache_flush'];
+      }
+      else {
+        $cache_tables = array();
+      }
+      $cache_tables[$table] = time();
+      $_SESSION['cache_flush'] = $cache_tables;
+    }
+    else {
     dmemcache_flush($table);
   }
+  }
   else {
     dmemcache_delete($cid, $table);
   }
