Index: sites/all/modules/memcache/memcache.inc
===================================================================
--- sites/all/modules/memcache/memcache.inc	(revision 2019)
+++ sites/all/modules/memcache/memcache.inc	(working copy)
@@ -18,6 +18,9 @@
  *   'cache_menu', 'cache_page', or 'cache' for the default cache.
  */
 function cache_get($cid, $table = 'cache') {
+  if ($table == 'cache_form') {
+    return db_cache_get($cid, $table);
+  }
   return dmemcache_get($cid, $table);
 }
 
@@ -45,6 +48,11 @@
  *   A string containing HTTP header information for cached pages.
  */
 function cache_set($cid, $data, $table = 'cache', $expire = CACHE_PERMANENT, $headers = NULL) {
+
+  if ($table == 'cache_form') {
+    return db_cache_set($cid, $data, $table, $expire, $headers);
+  }
+
   $created = time();
 
   // Create new cache object.
@@ -86,6 +94,11 @@
  *   match. If '*' is given as $cid, the table $table will be emptied.
  */
 function cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) {
+
+  if ($table == 'cache_form') {
+    return db_cache_clear_all($cid, $table, $wildcard);
+  }
+
   // 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)) {
@@ -118,3 +131,112 @@
 function memcache_clone($object) {
   return version_compare(phpversion(), '5.0') < 0 ? $object : clone($object);
 }
+
+function db_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 {". $table ."} WHERE expire != %d AND expire <= %d", CACHE_PERMANENT, $cache_flush);
+  }
+
+  $cache = db_fetch_object(db_query("SELECT data, created, headers, expire, serialized FROM {". $table ."} WHERE cid = '%s'", $cid));
+  if (isset($cache->data)) {
+    // If the data is permanent or we're not enforcing a minimum cache lifetime
+    // always return the cached data.
+    if ($cache->expire == CACHE_PERMANENT || !variable_get('cache_lifetime', 0)) {
+      $cache->data = db_decode_blob($cache->data);
+      if ($cache->serialized) {
+        $cache->data = unserialize($cache->data);
+      }
+    }
+    // If enforcing a minimum cache lifetime, validate that the data is
+    // currently valid for this user before we return it by making sure the
+    // cache entry was created before the timestamp in the current session's
+    // cache timer. The cache variable is loaded into the $user object by
+    // sess_read() in session.inc.
+    else {
+      if ($user->cache > $cache->created) {
+        // This cache data is too old and thus not valid for us, ignore it.
+        return 0;
+      }
+      else {
+        $cache->data = db_decode_blob($cache->data);
+        if ($cache->serialized) {
+          $cache->data = unserialize($cache->data);
+        }
+      }
+    }
+    return $cache;
+  }
+  return 0;
+}
+
+function db_cache_set($cid, $data, $table = 'cache', $expire = CACHE_PERMANENT, $headers = NULL) {
+  $serialized = 0;
+  if (is_object($data) || is_array($data)) {
+    $data = serialize($data);
+    $serialized = 1;
+  }
+  $created = time();
+  db_query("UPDATE {". $table ."} SET data = %b, created = %d, expire = %d, headers = '%s', serialized = %d WHERE cid = '%s'", $data, $created, $expire, $headers, $serialized, $cid);
+  if (!db_affected_rows()) {
+    @db_query("INSERT INTO {". $table ."} (cid, data, created, expire, headers, serialized) VALUES ('%s', %b, %d, %d, '%s', %d)", $cid, $data, $created, $expire, $headers, $serialized);
+  }
+}
+
+function db_cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) {
+  global $user;
+
+  if (!isset($cid) && !isset($table)) {
+    // 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;
+  }
+
+  if (empty($cid)) {
+    if (variable_get('cache_lifetime', 0)) {
+      // We store the time in the current user's $user->cache variable which
+      // will be saved into the sessions table by sess_write(). We then
+      // simulate that the cache was flushed for this user by not returning
+      // cached data that was cached before the timestamp.
+      $user->cache = time();
+
+      $cache_flush = variable_get('cache_flush', 0);
+      if ($cache_flush == 0) {
+        // This is the first request to clear the cache, start a timer.
+        variable_set('cache_flush', time());
+      }
+      else if (time() > ($cache_flush + variable_get('cache_lifetime', 0))) {
+        // Clear the cache for everyone, cache_flush_delay seconds have
+        // passed since the first request to clear the cache.
+        db_query("DELETE FROM {". $table ."} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, time());
+        variable_set('cache_flush', 0);
+      }
+    }
+    else {
+      // No minimum cache lifetime, flush all temporary cache entries now.
+      db_query("DELETE FROM {". $table ."} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, time());
+    }
+  }
+  else {
+    if ($wildcard) {
+      if ($cid == '*') {
+        db_query("DELETE FROM {". $table ."}");
+      }
+      else {
+        db_query("DELETE FROM {". $table ."} WHERE cid LIKE '%s%%'", $cid);
+      }
+    }
+    else {
+      db_query("DELETE FROM {". $table ."} WHERE cid = '%s'", $cid);
+    }
+  }
+}
+
