MediaWiki  master
ProcessCacheLRUTest.php
Go to the documentation of this file.
1 <?php
2 
11 
13 
18  protected function assertCacheEmpty( $cache, $msg = 'Cache should be empty' ) {
19  $this->assertEquals( 0, $cache->getEntriesCount(), $msg );
20  }
21 
25  protected function fillCache( &$cache, $numEntries ) {
26  // Fill cache with three values
27  for ( $i = 1; $i <= $numEntries; $i++ ) {
28  $cache->set( "cache-key-$i", "prop-$i", "value-$i" );
29  }
30  }
31 
36  protected function getExpectedCache( $cacheMaxEntries, $entryToFill ) {
37  $expected = [];
38 
39  if ( $entryToFill === 0 ) {
40  // The cache is empty!
41  return [];
42  } elseif ( $entryToFill <= $cacheMaxEntries ) {
43  // Cache is not fully filled
44  $firstKey = 1;
45  } else {
46  // Cache overflowed
47  $firstKey = 1 + $entryToFill - $cacheMaxEntries;
48  }
49 
50  $lastKey = $entryToFill;
51 
52  for ( $i = $firstKey; $i <= $lastKey; $i++ ) {
53  $expected["cache-key-$i"] = [ "prop-$i" => "value-$i" ];
54  }
55 
56  return $expected;
57  }
58 
63  public function testPhpUnitArrayEquality() {
64  $one = [ 'A' => 1, 'B' => 2 ];
65  $two = [ 'B' => 2, 'A' => 1 ];
66  // ==
67  $this->assertEquals( $one, $two );
68  // ===
69  $this->assertNotSame( $one, $two );
70  }
71 
77  public function testConstructorGivenInvalidValue( $maxSize ) {
78  new ProcessCacheLRUTestable( $maxSize );
79  }
80 
84  public static function provideInvalidConstructorArg() {
85  return [
86  [ null ],
87  [ [] ],
88  [ new stdClass() ],
89  [ 0 ],
90  [ '5' ],
91  [ -1 ],
92  ];
93  }
94 
100  public function testAddAndGetAKey() {
101  $oneCache = new ProcessCacheLRUTestable( 1 );
102  $this->assertCacheEmpty( $oneCache );
103 
104  // First set just one value
105  $oneCache->set( 'cache-key', 'prop1', 'value1' );
106  $this->assertEquals( 1, $oneCache->getEntriesCount() );
107  $this->assertTrue( $oneCache->has( 'cache-key', 'prop1' ) );
108  $this->assertEquals( 'value1', $oneCache->get( 'cache-key', 'prop1' ) );
109  }
110 
115  public function testDeleteOldKey() {
116  $oneCache = new ProcessCacheLRUTestable( 1 );
117  $this->assertCacheEmpty( $oneCache );
118 
119  $oneCache->set( 'cache-key', 'prop1', 'value1' );
120  $oneCache->set( 'cache-key', 'prop1', 'value2' );
121  $this->assertEquals( 'value2', $oneCache->get( 'cache-key', 'prop1' ) );
122  }
123 
134  public function testFillingCache( $cacheMaxEntries, $entryToFill, $msg = '' ) {
135  $cache = new ProcessCacheLRUTestable( $cacheMaxEntries );
136  $this->fillCache( $cache, $entryToFill );
137 
138  $this->assertSame(
139  $this->getExpectedCache( $cacheMaxEntries, $entryToFill ),
140  $cache->getCache(),
141  "Filling a $cacheMaxEntries entries cache with $entryToFill entries"
142  );
143  }
144 
148  public static function provideCacheFilling() {
149  // ($cacheMaxEntries, $entryToFill, $msg='')
150  return [
151  [ 1, 0 ],
152  [ 1, 1 ],
153  // overflow
154  [ 1, 2 ],
155  // overflow
156  [ 5, 33 ],
157  ];
158  }
159 
167  $maxEntries = 3;
168 
169  $cache = new ProcessCacheLRUTestable( $maxEntries );
170  // Fill cache leaving just one remaining slot
171  $this->fillCache( $cache, $maxEntries - 1 );
172 
173  // Set an existing cache key
174  $cache->set( "cache-key-1", "prop-1", "new-value-for-1" );
175 
176  $this->assertSame(
177  [
178  'cache-key-2' => [ 'prop-2' => 'value-2' ],
179  'cache-key-1' => [ 'prop-1' => 'new-value-for-1' ],
180  ],
181  $cache->getCache()
182  );
183  }
184 
190  public function testRecentlyAccessedKeyStickIn() {
191  $cache = new ProcessCacheLRUTestable( 2 );
192  $cache->set( 'first', 'prop1', 'value1' );
193  $cache->set( 'second', 'prop2', 'value2' );
194 
195  // Get first
196  $cache->get( 'first', 'prop1' );
197  // Cache a third value, should invalidate the least used one
198  $cache->set( 'third', 'prop3', 'value3' );
199 
200  $this->assertFalse( $cache->has( 'second', 'prop2' ) );
201  }
202 
213  $maxEntries = 3;
214 
215  $cache = new ProcessCacheLRUTestable( $maxEntries );
216  $this->fillCache( $cache, $maxEntries );
217 
218  // Set an existing cache key
219  $cache->set( "cache-key-2", "prop-2", "new-value-for-2" );
220  $this->assertSame(
221  [
222  'cache-key-1' => [ 'prop-1' => 'value-1' ],
223  'cache-key-3' => [ 'prop-3' => 'value-3' ],
224  'cache-key-2' => [ 'prop-2' => 'new-value-for-2' ],
225  ],
226  $cache->getCache()
227  );
228  $this->assertEquals( 'new-value-for-2',
229  $cache->get( 'cache-key-2', 'prop-2' )
230  );
231  }
232 
236  public function testBumpExistingKeyToTop() {
237  $cache = new ProcessCacheLRUTestable( 3 );
238  $this->fillCache( $cache, 3 );
239 
240  // Set the very first cache key to a new value
241  $cache->set( "cache-key-1", "prop-1", "new value for 1" );
242  $this->assertEquals(
243  [
244  'cache-key-2' => [ 'prop-2' => 'value-2' ],
245  'cache-key-3' => [ 'prop-3' => 'value-3' ],
246  'cache-key-1' => [ 'prop-1' => 'new value for 1' ],
247  ],
248  $cache->getCache()
249  );
250  }
251 }
252 
257  public function getCache() {
258  return $this->cache->toArray();
259  }
260 
261  public function getEntriesCount() {
262  return count( $this->cache->toArray() );
263  }
264 }
testRecentlyAccessedKeyStickIn()
ProcessCacheLRU::get ProcessCacheLRU::set ProcessCacheLRU::has.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static provideCacheFilling()
Provider for testFillingCache.
testDeleteOldKey()
ProcessCacheLRU::set ProcessCacheLRU::get.
Overrides some ProcessCacheLRU methods and properties accessibility.
testReplaceExistingKeyInAFullCacheShouldBumpToTop()
This first create a full cache then update the value for the 2nd filled entry.
testReplaceExistingKeyShouldBumpEntryToTop()
Create a cache with only one remaining entry then update the first inserted entry.
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
$cache
Definition: mcc.php:33
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
testPhpUnitArrayEquality()
Highlight diff between assertEquals and assertNotSame .
testFillingCache( $cacheMaxEntries, $entryToFill, $msg='')
This test that we properly overflow when filling a cache with a sequence of always different cache-ke...
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Note that it uses the ProcessCacheLRUTestable class which extends some properties and methods visibil...
getExpectedCache( $cacheMaxEntries, $entryToFill)
Generates an array of what would be expected in cache for a given cache size and a number of entries ...
static provideInvalidConstructorArg()
Value which are forbidden by the constructor.
testAddAndGetAKey()
ProcessCacheLRU::get ProcessCacheLRU::set ProcessCacheLRU::has.
testConstructorGivenInvalidValue( $maxSize)
provideInvalidConstructorArg Wikimedia\Assert\ParameterAssertionException ProcessCacheLRU::__construc...
fillCache(&$cache, $numEntries)
Helper to fill a cache object passed by reference.
Class for process caching individual properties of expiring items.
testBumpExistingKeyToTop()
ProcessCacheLRU::set.
assertCacheEmpty( $cache, $msg='Cache should be empty')
Helper to verify emptiness of a cache object.