From ae9f19ab524dc3758057a7e9e45450480a1562f7 Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 24 Jan 2013 12:54:44 +0100 Subject: [PATCH] Clear caches automatically when changing site list. This introduces SiteSQLStore::reset() and calls it in all methods that modify the site list. Change-Id: If865ac16f7e79df9999db27ebe22aea5a0bc9a6b --- includes/site/SiteSQLStore.php | 18 +++++++++- .../includes/site/SiteSQLStoreTest.php | 34 +++++++++++++++---- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/includes/site/SiteSQLStore.php b/includes/site/SiteSQLStore.php index 8745cb4f76..587c629501 100644 --- a/includes/site/SiteSQLStore.php +++ b/includes/site/SiteSQLStore.php @@ -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 +} diff --git a/tests/phpunit/includes/site/SiteSQLStoreTest.php b/tests/phpunit/includes/site/SiteSQLStoreTest.php index 58a4e1fecb..4f7889bff1 100644 --- a/tests/phpunit/includes/site/SiteSQLStoreTest.php +++ b/tests/phpunit/includes/site/SiteSQLStoreTest.php @@ -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 ); + } + } -- 2.20.1