Index: includes/bootstrap.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v
retrieving revision 1.257
diff -u -p -r1.257 bootstrap.inc
--- includes/bootstrap.inc	3 Dec 2008 14:51:53 -0000	1.257
+++ includes/bootstrap.inc	13 Dec 2008 02:18:40 -0000
@@ -657,20 +657,28 @@ function variable_del($name) {
  * Note: we do not serve cached pages when status messages are waiting (from
  * a redirected form submission which was completed).
  */
-function page_get_cache() {
+function page_get_cache($set_status = NULL) {
   global $user, $base_root;
+  static $cache = NULL;
+  static $status = FALSE;
 
-  $cache = NULL;
-
-  if (!$user->uid && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD') && count(drupal_set_message()) == 0) {
-    $cache = cache_get($base_root . request_uri(), 'cache_page');
+  if (!isset($cache)) {
+    if (!$user->uid && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD') && count(drupal_set_message()) == 0) {
+      $cache = cache_get($base_root . request_uri(), 'cache_page');
+    }
 
     if (empty($cache)) {
+      $status = TRUE;
       ob_start();
     }
+    
+    return $cache;
+  }
+  elseif (isset($set_status)) {
+    $status = $set_status;
   }
 
-  return $cache;
+  return $status;
 }
 
 /**
@@ -947,6 +955,7 @@ function drupal_set_message($message = N
 
     if ($repeat || !in_array($message, $_SESSION['messages'][$type])) {
       $_SESSION['messages'][$type][] = $message;
+      page_get_cache(FALSE);
     }
   }
 
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.837
diff -u -p -r1.837 common.inc
--- includes/common.inc	12 Dec 2008 16:07:13 -0000	1.837
+++ includes/common.inc	13 Dec 2008 02:18:45 -0000
@@ -2911,7 +2911,7 @@ function _drupal_bootstrap_full() {
 function page_set_cache() {
   global $user, $base_root;
 
-  if (!$user->uid && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD') && count(drupal_get_messages(NULL, FALSE)) == 0) {
+  if (!$user->uid && page_get_cache()) {
     // This will fail in some cases, see page_get_cache() for the explanation.
     if ($data = ob_get_contents()) {
       $cache = TRUE;
Index: modules/simpletest/tests/cache.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/cache.test,v
retrieving revision 1.3
diff -u -p -r1.3 cache.test
--- modules/simpletest/tests/cache.test	8 Dec 2008 21:41:54 -0000	1.3
+++ modules/simpletest/tests/cache.test	13 Dec 2008 02:18:46 -0000
@@ -101,6 +101,85 @@ class CacheTestCase extends DrupalWebTes
   }
 }
 
+/**
+ * Test page caching.
+ */
+class CachePageCase extends CacheTestCase {
+
+  function getInfo() {
+    return array(
+      'name' => t('Cache page test'),
+      'description' => t('Check caching of pages and conditions for not caching pages.'),
+      'group' => t('Cache'),
+    );
+  }
+
+  function setUp() {
+    parent::setUp('system_test');
+
+    // Enable caching and clear the page cache.
+    variable_set('cache', CACHE_NORMAL);
+    cache_clear_all('*', 'cache_page');
+  }
+
+  /**
+   * Test that pages with messages are not cached.
+   */
+  function testNoCacheMessages() {
+    // View the front page with a message on it and test it was not cached.
+    variable_set('front_page_output', 1);
+    $this->drupalGet('');
+    $this->assertText(t('On front page.'), t('Message found.'));
+    $cache = cache_get(url('', array('absolute' => TRUE)), 'cache_page');
+    $this->assertFalse($cache, t('Page has not been cached.'));
+
+    // View the front page without a message on it and test it was cached.
+    variable_set('front_page_output', 0);
+    $this->drupalGet('');
+    $this->assertNoText(t('On front page.'), t('Message not found.'));
+    $cache = cache_get(url('', array('absolute' => TRUE)), 'cache_page');
+    $this->assertTrue($cache, t('Page has been cached.'));
+  }
+
+  /**
+   * Test that pages viewed by logged in users are not cached.
+   */
+  function testNoCacheLoggedIn() {
+    // Create a user, log in, and test that the request was not cached.
+    $this->user = $this->drupalCreateUser(array('access content'));
+    $this->drupalLogin($this->user);
+    $this->drupalGet('');
+    $cache = cache_get(url('', array('absolute' => TRUE)), 'cache_page');
+    $this->assertFalse($cache, t('Page has not been cached.'));
+
+    // Logout and check that the next request is cached.
+    $this->drupalLogout();
+    $this->drupalGet('');
+    $cache = cache_get(url('', array('absolute' => TRUE)), 'cache_page');
+    $this->assertTrue($cache, t('Page has been cached.'));
+  }
+
+  /**
+   * Test that POST and HEAD requests are not cached.
+   */
+  function testNoCachePostHead() {
+    // Make a POST request and test it was not cached.
+    $this->drupalPost(NULL, array(), t('Log in'));
+    $cache = cache_get(url('', array('absolute' => TRUE)), 'cache_page');
+    $this->assertFalse($cache, t('Page has not been cached.'));
+
+    // Make a HEAD request and test it was not cached.
+    $this->drupalHead('');
+    $cache = cache_get(url('', array('absolute' => TRUE)), 'cache_page');
+    $this->assertFalse($cache, t('Page has not been cached.'));
+
+    // Make a GET request and test it was cached.
+    $this->drupalGet('');
+    $cache = cache_get(url('', array('absolute' => TRUE)), 'cache_page');
+    $this->assertTrue($cache, t('Page has been cached.'));
+  }
+}
+
 class CacheSavingCase extends CacheTestCase {
   function getInfo() {
     return array(
@@ -224,4 +303,4 @@ class CacheClearCase extends CacheTestCa
                       || $this->checkCacheExists('test_cid_clear2', $this->default_value),
                       t('Two caches removed after clearing cid substring with wildcard true.'));
   }
-}
\ No newline at end of file
+}
