/** * @covers ReplicatedBagOStuff::get */ public function testGetAbsent() { $key = wfRandomString(); $value = wfRandomString(); $this->writeCache->set($key, $value); // Don't read from master. No failover if value is absent. $this->assertEquals($this->cache->get($key), false); }
/** * @covers CachedBagOStuff::doGet */ public function testCacheBackendMisses() { $backend = new HashBagOStuff(); $cache = new CachedBagOStuff($backend); // First hit primes the cache with miss from the backend $this->assertEquals(false, $cache->get('foo')); // Change the value in the backend $backend->set('foo', true); // Second hit returns the cached miss $this->assertEquals(false, $cache->get('foo')); // But a fresh value is read from the backend $backend->set('bar', true); $this->assertEquals(true, $cache->get('bar')); }
public function testSetDelayed() { $key = wfRandomString(); $value = wfRandomString(); // XXX: DeferredUpdates bound to transactions in CLI mode $dbw = wfGetDB(DB_MASTER); $dbw->begin(); $this->cache->set($key, $value); // Set in tier 1 $this->assertEquals($value, $this->cache1->get($key), 'Written to tier 1'); // Not yet set in tier 2 $this->assertEquals(false, $this->cache2->get($key), 'Not written to tier 2'); $dbw->commit(); // Set in tier 2 $this->assertEquals($value, $this->cache2->get($key), 'Written to tier 2'); }
public function testWriteCacheOnly() { $backend = new HashBagOStuff(); $cache = new CachedBagOStuff($backend); $cache->set('foo', 'bar', 0, CachedBagOStuff::WRITE_CACHE_ONLY); $this->assertEquals('bar', $cache->get('foo')); $this->assertFalse($backend->get('foo')); $cache->set('foo', 'old'); $this->assertEquals('old', $cache->get('foo')); $this->assertEquals('old', $backend->get('foo')); $cache->set('foo', 'new', 0, CachedBagOStuff::WRITE_CACHE_ONLY); $this->assertEquals('new', $cache->get('foo')); $this->assertEquals('old', $backend->get('foo')); $cache->delete('foo', CachedBagOStuff::WRITE_CACHE_ONLY); $this->assertEquals('old', $cache->get('foo')); // Reloaded from backend }
/** * Get an array of language names, indexed by code. * @param null|string $inLanguage Code of language in which to return the names * Use null for autonyms (native names) * @param string $include One of: * 'all' all available languages * 'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default) * 'mwfile' only if the language is in 'mw' *and* has a message file * @return array Language code => language name * @since 1.20 */ public static function fetchLanguageNames($inLanguage = null, $include = 'mw') { $cacheKey = $inLanguage === null ? 'null' : $inLanguage; $cacheKey .= ":{$include}"; if (self::$languageNameCache === null) { self::$languageNameCache = new HashBagOStuff(array('maxKeys' => 20)); } $ret = self::$languageNameCache->get($cacheKey); if (!$ret) { $ret = self::fetchLanguageNamesUncached($inLanguage, $include); self::$languageNameCache->set($cacheKey, $ret); } return $ret; }
/** * Check if a given user has permission to use this functionality. * @param User $user * @param bool $displayPassword If set, also check whether the user is allowed to reset the * password of another user and see the temporary password. * @return StatusValue */ public function isAllowed(User $user, $displayPassword = false) { $statuses = $this->permissionCache->get($user->getName()); if ($statuses) { list($status, $status2) = $statuses; } else { $resetRoutes = $this->config->get('PasswordResetRoutes'); $status = StatusValue::newGood(); if (!is_array($resetRoutes) || !in_array(true, array_values($resetRoutes), true)) { // Maybe password resets are disabled, or there are no allowable routes $status = StatusValue::newFatal('passwordreset-disabled'); } elseif (($providerStatus = $this->authManager->allowsAuthenticationDataChange(new TemporaryPasswordAuthenticationRequest(), false)) && !$providerStatus->isGood()) { // Maybe the external auth plugin won't allow local password changes $status = StatusValue::newFatal('resetpass_forbidden-reason', $providerStatus->getMessage()); } elseif (!$this->config->get('EnableEmail')) { // Maybe email features have been disabled $status = StatusValue::newFatal('passwordreset-emaildisabled'); } elseif (!$user->isAllowed('editmyprivateinfo')) { // Maybe not all users have permission to change private data $status = StatusValue::newFatal('badaccess'); } elseif ($user->isBlocked()) { // Maybe the user is blocked (check this here rather than relying on the parent // method as we have a more specific error message to use here $status = StatusValue::newFatal('blocked-mailpassword'); } $status2 = StatusValue::newGood(); if (!$user->isAllowed('passwordreset')) { $status2 = StatusValue::newFatal('badaccess'); } $this->permissionCache->set($user->getName(), [$status, $status2]); } if (!$displayPassword || !$status->isGood()) { return $status; } else { return $status2; } }
/** * Ensure maxKeys eviction prefers recently retrieved keys (LRU). */ public function testEvictionGet() { $cache = new HashBagOStuff(array('maxKeys' => 3)); foreach (array('foo', 'bar', 'baz') as $key) { $cache->set($key, 1); } // Get existing key $cache->get('foo', 1); // Add a 4th key (beyond the allowed maximum) $cache->set('quux', 1); // Foo's life should have been extended over Bar foreach (array('foo', 'baz', 'quux') as $key) { $this->assertEquals(1, $cache->get($key), "Kept {$key}"); } $this->assertEquals(false, $cache->get('bar'), 'Evicted bar'); }
/** * Clears cache */ public function clear() { $this->mGoodLinks->clear(); $this->mBadLinks->clear(); }
/** * @covers BagOStuff::__construct * @covers BagOStuff::trackDuplicateKeys */ public function testReportDupes() { $logger = $this->getMock('Psr\\Log\\NullLogger'); $logger->expects($this->once())->method('warning')->with('Duplicate get(): "{key}" fetched {count} times', ['key' => 'foo', 'count' => 2]); $cache = new HashBagOStuff(['reportDupes' => true, 'asyncHandler' => 'DeferredUpdates::addCallableUpdate', 'logger' => $logger]); $cache->get('foo'); $cache->get('bar'); $cache->get('foo'); DeferredUpdates::doUpdates(); }
/** * @dataProvider includeOffsetProvider */ public function testIncludeOffset($message, $expect, $queryOptions) { global $wgFlowCacheVersion; $bag = new \HashBagOStuff(); $innerCache = new LocalBufferedBagOStuff($bag); $cache = new BufferedCache($innerCache); // preload our answer $bag->set(wfWikiId() . ":prefix:1:{$wgFlowCacheVersion}", array(array('foo' => 1, 'bar' => 9), array('foo' => 1, 'bar' => 8), array('foo' => 1, 'bar' => 7), array('foo' => 1, 'bar' => 6), array('foo' => 1, 'bar' => 5), array('foo' => 1, 'bar' => 4), array('foo' => 1, 'bar' => 3), array('foo' => 1, 'bar' => 2), array('foo' => 1, 'bar' => 1))); $storage = $this->getMock('Flow\\Data\\ObjectStorage'); $index = new TopKIndex($cache, $storage, 'prefix', array('foo'), array('sort' => 'bar')); $result = $index->find(array('foo' => '1'), $queryOptions); foreach ($result as $row) { $found[] = $row['bar']; } $this->assertEquals($expect, $found); }
public function deleteObjectsExpiringBefore($date, $progressCallback = false) { parent::deleteObjectsExpiringBefore($date, $progressCallback); return $this->backend->deleteObjectsExpiringBefore($date, $progressCallback); }
/** * @param User $user * @param LinkTarget $target * * @return WatchedItem|null */ private function getCached(User $user, LinkTarget $target) { return $this->cache->get($this->getCacheKey($user, $target)); }