#730060: remove minimum cache lifetime. Better strategies can be implemented in contrib.

From: Damien Tournoud <damien@tournoud.net>


---
 bootstrap.inc               |    1 -
 cache.inc                   |   80 ++++---------------------------------------
 session.inc                 |    1 -
 simpletest/tests/cache.test |   11 ------
 system/system.admin.inc     |    9 -----
 system/system.install       |    6 ---
 user/user.install           |    7 ++--
 7 files changed, 12 insertions(+), 103 deletions(-)

diff --git includes/bootstrap.inc includes/bootstrap.inc
index cdf584e..cb149cb 100644
--- includes/bootstrap.inc
+++ includes/bootstrap.inc
@@ -1721,7 +1721,6 @@ function drupal_anonymous_user($session = '') {
   $user->roles = array();
   $user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
   $user->session = $session;
-  $user->cache = 0;
   return $user;
 }
 
diff --git includes/cache.inc includes/cache.inc
index 5e6716b..355529a 100644
--- includes/cache.inc
+++ includes/cache.inc
@@ -310,8 +310,6 @@ class DrupalDatabaseCache implements DrupalCacheInterface {
 
   function get($cid) {
     try {
-      // Garbage collection necessary when enforcing a minimum cache lifetime.
-      $this->garbageCollection($this->bin);
       $cache = db_query("SELECT data, created, headers, expire, serialized FROM {" . $this->bin . "} WHERE cid = :cid", array(':cid' => $cid))->fetchObject();
       return $this->prepareItem($cache);
     }
@@ -324,8 +322,6 @@ class DrupalDatabaseCache implements DrupalCacheInterface {
 
   function getMultiple(&$cids) {
     try {
-      // Garbage collection necessary when enforcing a minimum cache lifetime.
-      $this->garbageCollection($this->bin);
       $query = db_select($this->bin);
       $query->fields($this->bin, array('cid', 'data', 'created', 'headers', 'expire', 'serialized'));
       $query->condition($this->bin . '.cid', $cids, 'IN');
@@ -348,28 +344,6 @@ class DrupalDatabaseCache implements DrupalCacheInterface {
   }
 
   /**
-   * Garbage collection for get() and getMultiple().
-   *
-   * @param $bin
-   *   The bin being requested.
-   */
-  protected function garbageCollection() {
-    global $user;
-
-    // Garbage collection necessary when enforcing a minimum cache lifetime.
-    $cache_flush = variable_get('cache_flush_' . $this->bin, 0);
-    if ($cache_flush && ($cache_flush + variable_get('cache_lifetime', 0) <= REQUEST_TIME)) {
-      // Reset the variable immediately to prevent a meltdown in heavy load situations.
-      variable_set('cache_flush_' . $this->bin, 0);
-      // Time to flush old cache data
-      db_delete($this->bin)
-        ->condition('expire', CACHE_PERMANENT, '<>')
-        ->condition('expire', $cache_flush, '<=')
-        ->execute();
-    }
-  }
-
-  /**
    * Prepare a cached item.
    *
    * Checks that items are either permanent or did not expire, and unserializes
@@ -387,22 +361,9 @@ class DrupalDatabaseCache implements DrupalCacheInterface {
     if (!isset($cache->data)) {
       return FALSE;
     }
-    // If the data is permanent or we are not enforcing a minimum cache lifetime
-    // always return the cached data.
-    if ($cache->expire == CACHE_PERMANENT || !variable_get('cache_lifetime', 0)) {
-      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 _drupal_session_read()
-    // in session.inc. 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) && $user->cache > $cache->created) {
-      // This cache data is too old and thus not valid for us, ignore it.
-      return FALSE;
+
+    if ($cache->serialized) {
+      $cache->data = unserialize($cache->data);
     }
 
     if (isset($cache->headers)) {
@@ -443,35 +404,11 @@ class DrupalDatabaseCache implements DrupalCacheInterface {
     global $user;
 
     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 bin by _drupal_session_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 = REQUEST_TIME;
-
-        $cache_flush = variable_get('cache_flush_' . $this->bin, 0);
-        if ($cache_flush == 0) {
-          // This is the first request to clear the cache, start a timer.
-          variable_set('cache_flush_' . $this->bin, REQUEST_TIME);
-        }
-        elseif (REQUEST_TIME > ($cache_flush + variable_get('cache_lifetime', 0))) {
-          // Clear the cache for everyone, cache_lifetime seconds have
-          // passed since the first request to clear the cache.
-          db_delete($this->bin)
-            ->condition('expire', CACHE_PERMANENT, '<>')
-            ->condition('expire', REQUEST_TIME, '<')
-            ->execute();
-          variable_set('cache_flush_' . $this->bin, 0);
-        }
-      }
-      else {
-        // No minimum cache lifetime, flush all temporary cache entries now.
-        db_delete($this->bin)
-          ->condition('expire', CACHE_PERMANENT, '<>')
-          ->condition('expire', REQUEST_TIME, '<')
-          ->execute();
-      }
+      // Flush all temporary cache entries now.
+      db_delete($this->bin)
+        ->condition('expire', CACHE_PERMANENT, '<>')
+        ->condition('expire', REQUEST_TIME, '<')
+        ->execute();
     }
     else {
       if ($wildcard) {
@@ -502,7 +439,6 @@ class DrupalDatabaseCache implements DrupalCacheInterface {
   }
 
   function isEmpty() {
-    $this->garbageCollection();
     $query = db_select($this->bin);
     $query->addExpression('1');
     $result = $query->range(0, 1)
diff --git includes/session.inc includes/session.inc
index ff9fd21..0b5e2e6 100644
--- includes/session.inc
+++ includes/session.inc
@@ -146,7 +146,6 @@ function _drupal_session_write($sid, $value) {
 
   $fields = array(
     'uid' => $user->uid,
-    'cache' => isset($user->cache) ? $user->cache : 0,
     'hostname' => ip_address(),
     'session' => $value,
     'timestamp' => REQUEST_TIME,
diff --git modules/simpletest/tests/cache.test modules/simpletest/tests/cache.test
index d5a9c5f..2f6c0f9 100644
--- modules/simpletest/tests/cache.test
+++ modules/simpletest/tests/cache.test
@@ -88,17 +88,6 @@ class CacheTestCase extends DrupalWebTestCase {
 
     cache_clear_all(NULL, $bin);
   }
-
-  /**
-   * Setup the lifetime settings for caching.
-   *
-   * @param $time
-   *   The time in seconds the cache should minimal live.
-   */
-  protected function setupLifetime($time) {
-    variable_set('cache_lifetime', $time);
-    variable_set('cache_flush', 0);
-  }
 }
 
 class CacheSavingCase extends CacheTestCase {
diff --git modules/system/system.admin.inc modules/system/system.admin.inc
index 9df596d..bdc7af4 100644
--- modules/system/system.admin.inc
+++ modules/system/system.admin.inc
@@ -1635,15 +1635,6 @@ function system_performance_settings() {
     '#default_value' => $cache,
     '#options' => array(CACHE_DISABLED => t('Disabled'), CACHE_NORMAL => t('Normal (recommended)')),
   );
-  $period = drupal_map_assoc(array(0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400), 'format_interval');
-  $period[0] = '<' . t('none') . '>';
-  $form['caching']['cache_lifetime'] = array(
-    '#type' => 'select',
-    '#title' => t('Minimum cache lifetime'),
-    '#default_value' => variable_get('cache_lifetime', 0),
-    '#options' => $period,
-    '#description' => t('The minimum amount of time that will elapse before the caches are recreated.')
-  );
 
   $directory = 'public://';
   $is_writable = is_dir($directory) && is_writable($directory);
diff --git modules/system/system.install modules/system/system.install
index 235010d..9528f96 100644
--- modules/system/system.install
+++ modules/system/system.install
@@ -1371,12 +1371,6 @@ function system_schema() {
         'not null' => TRUE,
         'default' => 0,
       ),
-      'cache' => array(
-        'description' => "The time of this user's last post. This is used when the site has specified a minimum_cache_lifetime. See cache_get().",
-        'type' => 'int',
-        'not null' => TRUE,
-        'default' => 0,
-      ),
       'session' => array(
         'description' => 'The serialized contents of $_SESSION, an array of name/value pairs that persists across page requests by this session ID. Drupal loads $_SESSION from here at the start of each request and saves it at the end.',
         'type' => 'text',
diff --git modules/user/user.install modules/user/user.install
index 803d190..5ef6448 100644
--- modules/user/user.install
+++ modules/user/user.install
@@ -353,15 +353,16 @@ function user_update_7000(&$sandbox) {
 }
 
 /**
- * Remove the 'threshold', 'mode' and 'sort' columns from the {users} table.
+ * Remove the threshold, mode, sort and cache columns from the {users} table.
  *
- * These fields were previously used to store per-user comment settings.
+ * These fields were previously used to store per-user comment settings
+ * and cache flush timestamp.
  */
-
 function user_update_7001() {
   db_drop_field('users', 'threshold');
   db_drop_field('users', 'mode');
   db_drop_field('users', 'sort');
+  db_drop_field('users', 'cache');
 }
 
 /**
