ObjectCache is already doing a lot of factory pattern logic like creating instances of the various BagOStuff, this should really be the responsibility of the factory servicet. This patch introduces a proper factory (ObjectCacheFactory) to handle the responsibility of creating various instances of BagOStuff. Since `newFromParams()` is a static function that gets passed in configuration of $wgObjectCaches, that can stay that way (to keep supporting how we do this in prod today). Technical Breaking Change: `ObjectCache::makeLocalServerCache()` now has a parameter and requires it but there are no callers of this method outside MW core hence it is safe to change (and this patch update all callers) to work correctly. Cache prefix is gotten from global state because sometimes at this stage, the services container is not available. Bug: T358346 Change-Id: I3179a387486377c6a575d173f39f82870c49c321
128 lines
3.3 KiB
PHP
128 lines
3.3 KiB
PHP
<?php
|
|
|
|
use MediaWiki\MainConfigNames;
|
|
|
|
/**
|
|
* @covers \ObjectCache
|
|
* @group BagOStuff
|
|
* @group Database
|
|
*/
|
|
class ObjectCacheTest extends MediaWikiIntegrationTestCase {
|
|
|
|
protected function setUp(): void {
|
|
// Parent calls ObjectCache::clear() among other things
|
|
parent::setUp();
|
|
|
|
$this->setCacheConfig();
|
|
$this->setMainCache( CACHE_NONE );
|
|
$this->overrideConfigValues( [
|
|
MainConfigNames::MessageCacheType => CACHE_NONE,
|
|
MainConfigNames::ParserCacheType => CACHE_NONE,
|
|
] );
|
|
}
|
|
|
|
protected function tearDown(): void {
|
|
ObjectCache::$localServerCacheClass = null;
|
|
}
|
|
|
|
private function setCacheConfig( $arr = [] ) {
|
|
$defaults = [
|
|
CACHE_NONE => [ 'class' => EmptyBagOStuff::class ],
|
|
CACHE_DB => [ 'class' => SqlBagOStuff::class ],
|
|
'hash' => [ 'class' => HashBagOStuff::class ],
|
|
CACHE_ANYTHING => [ 'class' => HashBagOStuff::class ],
|
|
];
|
|
$this->overrideConfigValue( MainConfigNames::ObjectCaches, $arr + $defaults );
|
|
// Mock ACCEL with 'hash' as being installed.
|
|
// This makes tests deterministic regardless of APC.
|
|
ObjectCache::$localServerCacheClass = 'HashBagOStuff';
|
|
}
|
|
|
|
public function testNewAnythingNothing() {
|
|
$this->assertInstanceOf(
|
|
SqlBagOStuff::class,
|
|
ObjectCache::newAnything(),
|
|
'No available types. Fallback to DB'
|
|
);
|
|
}
|
|
|
|
public function testNewAnythingHash() {
|
|
$this->setMainCache( CACHE_HASH );
|
|
|
|
$this->assertInstanceOf(
|
|
HashBagOStuff::class,
|
|
ObjectCache::newAnything(),
|
|
'Use an available type (hash)'
|
|
);
|
|
}
|
|
|
|
public function testNewAnythingAccel() {
|
|
$this->setMainCache( CACHE_ACCEL );
|
|
|
|
$this->assertInstanceOf(
|
|
HashBagOStuff::class,
|
|
ObjectCache::newAnything(),
|
|
'Use an available type (CACHE_ACCEL)'
|
|
);
|
|
}
|
|
|
|
public function testNewAnythingNoAccel() {
|
|
// Mock APC not being installed (T160519, T147161)
|
|
ObjectCache::$localServerCacheClass = EmptyBagOStuff::class;
|
|
$this->setMainCache( CACHE_ACCEL );
|
|
|
|
$this->assertInstanceOf(
|
|
SqlBagOStuff::class,
|
|
ObjectCache::newAnything(),
|
|
'Fallback to DB if available types fall back to Empty'
|
|
);
|
|
}
|
|
|
|
public function testNewAnythingNoAccelNoDb() {
|
|
$this->setCacheConfig( [
|
|
// Mock APC not being installed (T160519, T147161)
|
|
CACHE_ACCEL => [ 'class' => EmptyBagOStuff::class ]
|
|
] );
|
|
$this->setMainCache( CACHE_ACCEL );
|
|
|
|
$this->getServiceContainer()->disableStorage();
|
|
|
|
$this->assertInstanceOf(
|
|
EmptyBagOStuff::class,
|
|
ObjectCache::newAnything(),
|
|
'Fallback to none if available types and DB are unavailable'
|
|
);
|
|
}
|
|
|
|
public function testNewAnythingNothingNoDb() {
|
|
$this->getServiceContainer()->disableStorage();
|
|
|
|
$this->assertInstanceOf(
|
|
EmptyBagOStuff::class,
|
|
ObjectCache::newAnything(),
|
|
'No available types or DB. Fallback to none.'
|
|
);
|
|
}
|
|
|
|
public static function provideIsDatabaseId() {
|
|
return [
|
|
[ CACHE_DB, CACHE_NONE, true ],
|
|
[ CACHE_ANYTHING, CACHE_DB, true ],
|
|
[ CACHE_ANYTHING, 'hash', false ],
|
|
[ CACHE_ANYTHING, CACHE_ANYTHING, true ]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideIsDatabaseId
|
|
* @param string|int $id
|
|
* @param string|int $mainCacheType
|
|
* @param bool $expected
|
|
*/
|
|
public function testIsDatabaseId( $id, $mainCacheType, $expected ) {
|
|
$this->overrideConfigValues( [
|
|
MainConfigNames::MainCacheType => $mainCacheType
|
|
] );
|
|
$this->assertSame( $expected, ObjectCache::isDatabaseId( $id ) );
|
|
}
|
|
}
|