/** * @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); }
public function testGetFromBackend() { $backend = new HashBagOStuff(); $cache = new CachedBagOStuff($backend); $backend->set('foo', 'bar'); $this->assertEquals('bar', $cache->get('foo')); $backend->set('foo', 'baz'); $this->assertEquals('bar', $cache->get('foo'), 'cached'); }
/** * @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')); }
/** * @param Title $title */ public function addBadLinkObj(Title $title) { $dbkey = $title->getPrefixedDBkey(); if (!$this->isBadLink($dbkey)) { $this->mBadLinks->set($dbkey, 1); } }
/** * @param LinkTarget $target */ public function addBadLinkObj(LinkTarget $target) { $dbkey = $this->titleFormatter->getPrefixedDBkey($target); if (!$this->isBadLink($dbkey)) { $this->mBadLinks->set($dbkey, 1); } }
public function set($key, $value, $exptime = 0, $flags = 0) { parent::set($key, $value, $exptime, $flags); if (!($flags & self::WRITE_CACHE_ONLY)) { $this->backend->set($key, $value, $exptime, $flags & ~self::WRITE_CACHE_ONLY); } return true; }
private function cache(WatchedItem $item) { $user = $item->getUser(); $target = $item->getLinkTarget(); $key = $this->getCacheKey($user, $target); $this->cache->set($key, $item); $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key; $this->stats->increment('WatchedItemStore.cache'); }
/** * 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; } }
/** * @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); }
/** * Ensure maxKeys eviction prefers recently retrieved keys (LRU). * * @covers HashBagOStuff::__construct * @covers HashBagOStuff::doGet * @covers HashBagOStuff::hasKey */ public function testEvictionGet() { $cache = new HashBagOStuff(['maxKeys' => 3]); foreach (['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 (['foo', 'baz', 'quux'] as $key) { $this->assertEquals(1, $cache->get($key), "Kept {$key}"); } $this->assertEquals(false, $cache->get('bar'), 'Evicted bar'); }