Add "generic" key methods for quickly deriving keys from key component lists in a bijective manor. This is useful for BagOStuff classes that wrap other BagOStuff instances or for parsing keys to get stats. Make the proxy BagOStuff classes (ReplicatedBagOStuff, MultiWriteBagOStuff, CachedBagOStuff) use "generic" keys so that they can convert to appropriate keys when making backing cache instance method calls. Make EmptyBagOStuff, HashBagOStuff, APCUBagOStuff, RedisBagOStuff, and RESTBagOStuff use "generic" keys rather than those of MediumSpecificBagOStuff::makeKeyInternal(). This lets proxy BagOStuff classes bypass key conversions when used with instances of these classes as backing stores. Also: * Fix missing incr(), incrWithInit(), and decr() return values in MultiWriteBagOStuff. * Make MultiWriteBagOfStuff, ReplicatedBagOStuff, and CachedBagOStuff use similar backend method forwarding styles by using a new BagOStuff method. * Improved various related bits of documentation. Bug: T250239 Bug: T235705 Change-Id: I1eb897c2cea3f5b756dd1e3c457b7cbd817599f5
97 lines
2.5 KiB
PHP
97 lines
2.5 KiB
PHP
<?php
|
|
|
|
class ReplicatedBagOStuffTest extends \MediaWikiUnitTestCase {
|
|
/** @var HashBagOStuff */
|
|
private $writeCache;
|
|
/** @var HashBagOStuff */
|
|
private $readCache;
|
|
/** @var ReplicatedBagOStuff */
|
|
private $cache;
|
|
|
|
protected function setUp() : void {
|
|
parent::setUp();
|
|
|
|
$this->writeCache = new HashBagOStuff();
|
|
$this->readCache = new HashBagOStuff();
|
|
$this->cache = new ReplicatedBagOStuff( [
|
|
'keyspace' => 'repl_local',
|
|
'writeFactory' => $this->writeCache,
|
|
'readFactory' => $this->readCache,
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* @covers ReplicatedBagOStuff::set
|
|
*/
|
|
public function testSet() {
|
|
$key = $this->cache->makeKey( 'a', 'key' );
|
|
$value = 'a value';
|
|
|
|
$this->cache->set( $key, $value );
|
|
|
|
$this->assertSame( $value, $this->writeCache->get( $key ), 'Written' );
|
|
$this->assertFalse( $this->readCache->get( $key ), 'Async replication' );
|
|
}
|
|
|
|
/**
|
|
* @covers ReplicatedBagOStuff::get
|
|
*/
|
|
public function testGet() {
|
|
$key = $this->cache->makeKey( 'a', 'key' );
|
|
|
|
$write = 'new value';
|
|
$this->writeCache->set( $key, $write );
|
|
$read = 'old value';
|
|
$this->readCache->set( $key, $read );
|
|
|
|
$this->assertSame( $read, $this->cache->get( $key ), 'Async replication' );
|
|
}
|
|
|
|
/**
|
|
* @covers ReplicatedBagOStuff::get
|
|
*/
|
|
public function testGetAbsent() {
|
|
$key = $this->cache->makeKey( 'a', 'key' );
|
|
$value = 'a value';
|
|
$this->writeCache->set( $key, $value );
|
|
|
|
$this->assertFalse( $this->cache->get( $key ), 'Async replication' );
|
|
}
|
|
|
|
/**
|
|
* @covers ReplicatedBagOStuff::setMulti
|
|
* @covers ReplicatedBagOStuff::getMulti
|
|
*/
|
|
public function testGetSetMulti() {
|
|
$keyA = $this->cache->makeKey( 'key', 'a' );
|
|
$keyB = $this->cache->makeKey( 'key', 'b' );
|
|
$valueAOld = 'one old value';
|
|
$valueBOld = 'another old value';
|
|
$valueANew = 'one new value';
|
|
$valueBNew = 'another new value';
|
|
|
|
$this->writeCache->setMulti( [ $keyA => $valueANew, $keyB => $valueBNew ] );
|
|
$this->readCache->setMulti( [ $keyA => $valueAOld, $keyB => $valueBOld ] );
|
|
|
|
$this->assertEquals(
|
|
[ $keyA => $valueAOld, $keyB => $valueBOld ],
|
|
$this->cache->getMulti( [ $keyA, $keyB ] ),
|
|
'Async replication'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @covers ReplicatedBagOStuff::get
|
|
* @covers ReplicatedBagOStuff::set
|
|
*/
|
|
public function testGetSetRaw() {
|
|
$key = 'a:key';
|
|
$value = 'a value';
|
|
$this->cache->set( $key, $value );
|
|
|
|
// Write to master.
|
|
$this->assertEquals( $value, $this->writeCache->get( $key ) );
|
|
// Don't write to replica. Replication is deferred to backend.
|
|
$this->assertFalse( $this->readCache->get( $key ) );
|
|
}
|
|
}
|