--- includes/bootstrap.inc.orig 2005-03-25 07:07:49.000000000 -0500 +++ includes/bootstrap.inc 2005-03-25 07:23:19.000000000 -0500 @@ -9,6 +9,10 @@ define('CACHE_PERMANENT', 0); define('CACHE_TEMPORARY', -1); +define('CACHE_DISABLED', 0); +define('CACHE_ENABLED_STRICT', 1); +define('CACHE_ENABLED_LOOSE', 2); + define('WATCHDOG_NOTICE', 0); define('WATCHDOG_WARNING', 1); define('WATCHDOG_ERROR', 2); @@ -196,9 +200,34 @@ * The cache ID of the data to retrieve. */ function cache_get($key) { - $cache = db_fetch_object(db_query("SELECT data, created, headers FROM {cache} WHERE cid = '%s'", $key)); + global $user; + $sid = session_id(); + + // CACHE_ENABLED_LOOSE garbage collection + $cache_flush = variable_get('cache_flush', 0); + if ($cache_flush && ($cache_flush + variable_get('cache_flush_delay', 300) <= time())) { + // time to flush old cache data + db_query("DELETE FROM {cache} WHERE expire != %d AND expire <= %d", CACHE_PERMANENT, $cache_flush); + variable_set('cache_flush', 0); + } + + $cache = db_fetch_object(db_query("SELECT data, created, headers, expire FROM {cache} WHERE cid = '%s'", $key)); if (isset($cache->data)) { - $cache->data = db_decode_blob($cache->data); + // if data is permanent or using strict caching, always return data + if ($cache->expire == CACHE_PERMANENT || variable_get('cache', CACHE_DISABLED) == CACHE_ENABLED_STRICT) { + $cache->data = db_decode_blob($cache->data); + } + // if using loose caching, validate data is current before we return it + else { + $required = db_fetch_object(db_query("SELECT cache FROM {sessions} WHERE uid = %d and sid = '%s'", $user->uid, $sid)); + if ($required->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); + } + } return $cache; } return 0; @@ -235,16 +264,41 @@ * Expire data from the cache. * * @param $cid - * If set, the cache ID to delete. Otherwise, all cache entries that can expire - * are deleted. + * If set, the cache ID to delete. Otherwise, all cache entries that can + * expire are deleted. * * @param $wildcard * If set to true, the $cid is treated as a substring to match rather than a * complete ID. */ function cache_clear_all($cid = NULL, $wildcard = false) { + global $user; + $sid = session_id(); + if (empty($cid)) { - db_query("DELETE FROM {cache} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, time()); + if (variable_get('cache', CACHE_DISABLED) == CACHE_ENABLED_STRICT) { + // strict caching, flush all temporary cache entries + db_query("DELETE FROM {cache} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, time()); + } + else { + $cache_flush = variable_get('cache_flush', 0); + /* loose caching, only flush temporary cache entries that have been + ** invalidated for more than maximum allowable time. + */ + if ($cache_flush && ($cache_flush + variable_get('cache_flush_delay', 300) <= time())) { + /* only flush cache data older than $cache_flush, as newer data may + ** now be valid. + */ + db_query("DELETE FROM {cache} WHERE expire != %d AND expire <= %d", CACHE_PERMANENT, $cache_flush); + variable_set('cache_flush', 0); + } + // invalidate temporary cache data only for current user/session + db_query("UPDATE {sessions} SET cache = %d WHERE uid = %d AND sid = '%s'", time(), $user->uid, $sid); + if (variable_get('cache_flush', 0) == 0) { + // set timestamp to know which cache entries we eventually clear + variable_set('cache_flush', time()); + } + } } else { if ($wildcard) { --- modules/system.module.orig 2005-03-25 07:08:07.000000000 -0500 +++ modules/system.module 2005-03-25 07:06:55.000000000 -0500 @@ -37,7 +37,7 @@
     00 * * * * /home/www/drupal/scripts/cron-lynx.sh
Note that it is essential to access cron.php using a browser on the web site's domain; do not run it using command line PHP and avoid using localhost or 127.0.0.1 or some of the environment variables will not be set correctly and features may not work as expected.

Cache

-

Drupal has a caching mechanism which stores dynamically generated web pages in a database. By caching a web page, Drupal does not have to create the page each time someone wants to view it, instead it takes only one SQL query to display it, reducing response time and the server's load. Only pages requested by \"anonymous\" users are cached. In order to reduce server load and save bandwidth, Drupal stores and sends cached pages compressed.

", array('%base_url' => $base_url, '%cron-link' => "$base_url/cron.php", '%lynx' => 'http://lynx.browser.org', '%wget' => 'http://www.gnu.org/software/wget/wget.html' )); +

Drupal has a caching mechanism which stores dynamically generated web pages in a database. By caching a web page, Drupal does not have to create the page each time someone wants to view it, instead it takes only one SQL query to display it, reducing response time and the server's load. Only pages requested by \"anonymous\" users are cached. In order to reduce server load and save bandwidth, Drupal stores and sends cached pages compressed. Drupal supports by strict caching and loose caching. Strict caching immediately deletes cached data as soon as it becomes invalid for any user. Loose caching delays the deletion of cached data to provide better performance for high traffic sites.

", array('%base_url' => $base_url, '%cron-link' => "$base_url/cron.php", '%lynx' => 'http://lynx.browser.org', '%wget' => 'http://www.gnu.org/software/wget/wget.html' )); case 'admin/modules#description': return t('Configuration system that lets administrators modify the workings of the site.'); } @@ -221,7 +221,7 @@ $output .= form_group(t('Error handling'), $group); // caching: - $group = form_radios(t('Cache support'), 'cache', variable_get('cache', 0), array(t('Disabled'), t('Enabled')), t('Enable or disable the caching of rendered pages. When caching is enabled, Drupal will flush the cache when required to make sure updates take effect immediately. Check the cache documentation for information on Drupal\'s cache system.', array('%documentation' => url('admin/help', NULL, NULL, 'cache')))); + $group = form_radios(t('Cache support'), 'cache', variable_get('cache', CACHE_DISABLED), array(CACHE_DISABLED => t('Disabled'), CACHE_ENABLED_STRICT => t('Strict'), CACHE_ENABLED_LOOSE => t('Loose')), t('Enable or disable the caching of rendered pages. When strict caching is enabled, Drupal will flush the entire cache when required to make sure updates take effect immediately. When loose caching is enabled, Drupal will delay the flushing of the entire cache for several minutes, immediately flushing the cache only for specific users. Loose caching is intended to improve performance on high traffic sites. Check the cache documentation for information on Drupal\'s cache system.', array('%documentation' => url('admin/help', NULL, NULL, 'cache')))); $output .= form_group(t('Cache settings'), $group); --- database/database.mysql.orig 2005-03-25 07:08:17.000000000 -0500 +++ database/database.mysql 2005-03-25 07:06:55.000000000 -0500 @@ -574,6 +574,7 @@ sid varchar(32) NOT NULL default '', hostname varchar(128) NOT NULL default '', timestamp int(11) NOT NULL default '0', + cache int(11) NOT NULL default '0', session longtext, KEY uid (uid), PRIMARY KEY (sid), --- database/database.pgsql.orig 2005-03-25 07:08:24.000000000 -0500 +++ database/database.pgsql 2005-03-25 07:06:55.000000000 -0500 @@ -587,6 +587,7 @@ sid varchar(32) NOT NULL default '', hostname varchar(128) NOT NULL default '', timestamp integer NOT NULL default '0', + cache integer NOT NULL default '0', session text, PRIMARY KEY (sid) ); --- database/updates.inc.orig 2005-03-25 07:18:18.000000000 -0500 +++ database/updates.inc 2005-03-25 07:18:01.000000000 -0500 @@ -105,6 +105,7 @@ "2005-03-03" => "update_126", "2005-03-18" => "update_127", "2005-03-21" => "update_128" + "2005-03-25: first update since Drupal 4.6.0 release" => "update_129", ); function update_32() { @@ -2350,4 +2351,15 @@ return $ret; } +function update_129() { + $ret = array(); + if ($GLOBALS['db_type'] == 'mysql') { + $ret[] = update_sql("ALTER TABLE sessions ADD cache int(11) NOT NULL default '0' AFTER timestamp"); + } + elseif ($GLOBALS['db_type'] == 'pgsql') { + $ret[] = update_sql("ALTER TABLE sessions ADD cache int(11) NOT NULL default '0' AFTER timestamp"); + } + return $ret; +} + ?>