Index: memcache.db.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/memcache/memcache.db.inc,v
retrieving revision 1.7
diff -u -r1.7 memcache.db.inc
--- memcache.db.inc	29 Jan 2008 23:43:34 -0000	1.7
+++ memcache.db.inc	30 Mar 2008 06:39:14 -0000
@@ -6,32 +6,34 @@
 /** Implementation of cache.inc with memcache logic included **/
 
 /**
- * Return data from the persistent cache.
+ * Return data from the persistent cache. Data may be stored as either plain text or as serialized data.
+ * cache_get will automatically return unserialized objects and arrays.
  *
- * @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') {
+function cache_get($cid, $table = 'cache') {
   global $user;
 
   // Garbage collection necessary when enforcing a minimum cache lifetime
   $cache_flush = variable_get('cache_flush', 0);
   if ($cache_flush && ($cache_flush + variable_get('cache_lifetime', 0) <= time())) {
+    // Reset the variable immediately to prevent a meltdown in heavy load situations.
+    variable_set('cache_flush', 0);
     // Time to flush old cache data
     db_query("DELETE FROM {%s} WHERE expire != %d AND expire <= %d", $table, CACHE_PERMANENT, $cache_flush);
-    variable_set('cache_flush', 0);
   }
 
   // If we have a memcache hit for this, return it.
-  if ($cache = dmemcache_get($key, $table)) {
+  if ($cache = dmemcache_get($cid, $table)) {
     return $cache;
   }
 
   // Look for a database cache hit.
-  if ($cache = db_fetch_object(db_query("SELECT data, created, headers, expire, serialized FROM {%s} WHERE cid = '%s'", $table, $key))) {
+  if ($cache = db_fetch_object(db_query("SELECT data, created, headers, expire, serialized FROM {%s} WHERE cid = '%s'", $table, $cid))) {
     if (isset($cache->data)) {
       // If the data is permanent or we're not enforcing a minimum cache lifetime
       // always return the cached data.
@@ -62,7 +64,7 @@
 
     // By calling cache_set with an extra paramater to signify no db storage,
     // we can lazy instantiate memcache that just comes online.
-    cache_set($key, $table, $cache->data, $cache->expire, $cache->headers, FALSE);
+    cache_set($cid, $cache->data, $table, $cache->expire, $cache->headers, FALSE);
     return $cache;
   }
   return 0;
@@ -95,11 +97,12 @@
  *
  * @param $cid
  *   The cache ID of the data to store.
+ * @param $data
+ *   The data to store in the cache. Complex data types will be automatically serialized before insertion.
+ *   Strings will be stored as plain text and not serialized.
  * @param $table
  *   The table $table to store the data in. Valid core values are 'cache_filter',
  *   'cache_menu', 'cache_page', or 'cache'.
- * @param $data
- *   The data to store in the cache. Complex data types must be serialized first.
  * @param $expire
  *   One of the following values:
  *   - CACHE_PERMANENT: Indicates that the item should never be removed unless
@@ -115,14 +118,14 @@
  *   It allows us to do a cache_set and not write to the database, but only
  *   to memcache.
  */
-function cache_set($cid, $table = 'cache', $data, $expire = CACHE_PERMANENT, $headers = NULL, $db_storage = TRUE) {
-  $time = time();
+function cache_set($cid, $data, $table = 'cache', $expire = CACHE_PERMANENT, $headers = NULL, $db_storage = TRUE) {
+  $created = time();
 
   // Create new cache object.
   $cache = new stdClass;
   $cache->cid = $cid;
   $cache->data = is_object($data) ? memcache_clone($data) : $data;
-  $cache->created = $time;
+  $cache->created = $created;
   $cache->expire = $expire;
   $cache->headers = $headers;
 
@@ -136,8 +139,8 @@
     db_query("
        INSERT INTO {$table} (data, created, expire, headers, serialized, cid) VALUES (%b, %d, %d, '%s', %d, '%s') ON DUPLICATE KEY
        UPDATE data = %b, created = %d, expire = %d, headers = '%s', serialized = %d",
-       $data, $time, $expire, $headers, $serialized, $cid,
-       $data, $time, $expire, $headers, $serialized, $cid);
+       $data, $created, $expire, $headers, $serialized, $cid,
+       $data, $created, $expire, $headers, $serialized, $cid);
   }
 
   // Save to memcache
@@ -150,7 +153,7 @@
 /**
  *
  * Expire data from the cache. If called without arguments, expirable
- * entries will be cleared from the cache_page table.
+ * entries will be cleared from the cache_page and cache_block tables.
  *
  * @param $cid
  *   If set, the cache ID to delete. Otherwise, all cache entries that can
@@ -189,7 +192,10 @@
   }
 
   if (!isset($cid) && !isset($table)) {
-    cache_clear_all('*', 'cache_page', TRUE);
+    // Clear the block cache first, so stale data will
+    // not end up in the page cache.
+    cache_clear_all(NULL, 'cache_block');
+    cache_clear_all(NULL, 'cache_page');
     return;
   }
 
Index: memcache.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/memcache/memcache.inc,v
retrieving revision 1.27
diff -u -r1.27 memcache.inc
--- memcache.inc	29 Jan 2008 23:43:34 -0000	1.27
+++ memcache.inc	30 Mar 2008 06:38:40 -0000
@@ -6,16 +6,17 @@
 /** Implementation of cache.inc with memcache logic included **/
 
 /**
- * Return data from the persistent cache.
+ * Return data from the persistent cache. Data may be stored as either plain text or as serialized data.
+ * cache_get will automatically return unserialized objects and arrays.
  *
- * @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') {
+  return dmemcache_get($cid, $table);
 }
 
 /**
@@ -23,11 +24,12 @@
  *
  * @param $cid
  *   The cache ID of the data to store.
+ * @param $data
+ *   The data to store in the cache. Complex data types will be automatically serialized before insertion.
+ *   Strings will be stored as plain text and not serialized.
  * @param $table
  *   The table $table to store the data in. Valid core values are 'cache_filter',
  *   'cache_menu', 'cache_page', or 'cache'.
- * @param $data
- *   The data to store in the cache. Complex data types must be serialized first.
  * @param $expire
  *   One of the following values:
  *   - CACHE_PERMANENT: Indicates that the item should never be removed unless
@@ -38,19 +40,15 @@
  *     the given time, after which it behaves like CACHE_TEMPORARY.
  * @param $headers
  *   A string containing HTTP header information for cached pages.
- * @param $db_storage
- *   This boolean is unique to the memcache.inc implementation of cache set.
- *   It allows us to do a cache_set and not write to the database, but only
- *   to memcache.
  */
-function cache_set($cid, $table = 'cache', $data, $expire = CACHE_PERMANENT, $headers = NULL) {
-  $time = time();
+function cache_set($cid, $data, $table = 'cache', $expire = CACHE_PERMANENT, $headers = NULL) {
+  $created = time();
 
   // Create new cache object.
   $cache = new stdClass;
   $cache->cid = $cid;
   $cache->data = is_object($data) ? memcache_clone($data) : $data;
-  $cache->created = $time;
+  $cache->created = $created;
   $cache->expire = $expire;
   $cache->headers = $headers;
 
@@ -64,7 +62,7 @@
 /**
  *
  * Expire data from the cache. If called without arguments, expirable
- * entries will be cleared from the cache_page table.
+ * entries will be cleared from the cache_page and cache_block tables.
  *
  * @param $cid
  *   If set, the cache ID to delete. Otherwise, all cache entries that can
