Clear caches automatically when changing site list.
authordaniel <daniel.kinzler@wikimedia.de>
Thu, 24 Jan 2013 11:54:44 +0000 (12:54 +0100)
committerdaniel <daniel.kinzler@wikimedia.de>
Thu, 24 Jan 2013 18:10:19 +0000 (19:10 +0100)
This introduces SiteSQLStore::reset() and calls it in all
methods that modify the site list.

Change-Id: If865ac16f7e79df9999db27ebe22aea5a0bc9a6b

includes/site/SiteSQLStore.php
tests/phpunit/includes/site/SiteSQLStoreTest.php

index 8745cb4..587c629 100644 (file)
@@ -320,9 +320,25 @@ class SiteSQLStore implements SiteStore {
                        $dbw->commit( __METHOD__ );
                }
 
+               // purge cache
+               $this->reset();
+
                return $success;
        }
 
+       /**
+        * Purges the internal and external cache of the site list, forcing the list
+        * of sites to be re-read from the database.
+        *
+        * @since 1.21
+        */
+       public function reset() {
+               // purge cache
+               $cache = wfGetMainCache();
+               $cache->delete( $this->getCacheKey() );
+               $this->sites = null;
+       }
+
        /**
         * @since 1.21
         *
@@ -406,4 +422,4 @@ class Sites extends SiteSQLStore {
                return $this->getSites()->getGroup( $group );
        }
 
-}
\ No newline at end of file
+}
index 58a4e1f..4f7889b 100644 (file)
@@ -36,9 +36,9 @@ class SiteSQLStoreTest extends MediaWikiTestCase {
                $expectedSites = TestSites::getSites();
                TestSites::insertIntoDb();
 
-               $sitesTable = SiteSQLStore::newInstance();
+               $store = SiteSQLStore::newInstance();
 
-               $sites = $sitesTable->getSites();
+               $sites = $store->getSites();
 
                $this->assertInstanceOf( 'SiteList', $sites );
 
@@ -57,7 +57,7 @@ class SiteSQLStoreTest extends MediaWikiTestCase {
        }
 
        public function testSaveSites() {
-               $sitesTable = SiteSQLStore::newInstance();
+               $store = SiteSQLStore::newInstance();
 
                $sites = array();
 
@@ -71,15 +71,37 @@ class SiteSQLStoreTest extends MediaWikiTestCase {
                $site->setLanguageCode( 'nl' );
                $sites[] = $site;
 
-               $this->assertTrue( $sitesTable->saveSites( $sites ) );
+               $this->assertTrue( $store->saveSites( $sites ) );
 
-               $site = $sitesTable->getSite( 'ertrywuutr', 'nocache' );
+               $site = $store->getSite( 'ertrywuutr' );
                $this->assertInstanceOf( 'Site', $site );
                $this->assertEquals( 'en', $site->getLanguageCode() );
 
-               $site = $sitesTable->getSite( 'sdfhxujgkfpth', 'nocache' );
+               $site = $store->getSite( 'sdfhxujgkfpth' );
                $this->assertInstanceOf( 'Site', $site );
                $this->assertEquals( 'nl', $site->getLanguageCode() );
        }
 
+       public function testReset() {
+               $store = SiteSQLStore::newInstance();
+
+               // initialize internal cache
+               $this->assertGreaterThan( 0, $store->getSites()->count() );
+
+               // Clear actual data. Will not purge the internal cache in store2.
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->delete( 'sites', '*', __METHOD__ );
+               $dbw->delete( 'site_identifiers', '*', __METHOD__ );
+
+               // sanity check: $store2 should have a stale cache now
+               $this->assertNotNull( $store->getSite( 'enwiki' ) );
+
+               // purge cache
+               $store->reset();
+
+               // ...now the internal cache of $store2 should be updated and thus empty.
+               $site = $store->getSite( 'enwiki' );
+               $this->assertNull( $site );
+       }
+
 }