/** * @since 2.2 * * @return array */ public function fetchList() { list($key, $optionsKey) = $this->getCacheKey($this->listLookup->getLookupIdentifier()); if ($this->cacheOptions->useCache && $this->cache->contains($key) && ($result = $this->tryFetchFromCache($key, $optionsKey)) !== null) { return $result; } $list = $this->listLookup->fetchList(); $this->saveToCache($key, $optionsKey, $list, $this->listLookup->getTimestamp(), $this->cacheOptions->ttl); return $list; }
/** * Returns an id for a redirect if no redirect is found 0 is returned * * @since 2.1 * * @param string $title DB key * @param integer $namespace * * @return integer */ public function findRedirectIdFor($title, $namespace) { $hash = HashBuilder::createHashIdFromSegments($title, $namespace); if ($this->cache->contains($hash)) { return $this->cache->fetch($hash); } $id = $this->select($title, $namespace); $this->cache->save($hash, $id); return $id; }
/** * @since 2.2 * * @return array */ public function fetchList() { $key = $this->getCacheKey($this->listLookup->getLookupIdentifier()); if ($this->cache->contains($key) && $this->cacheOptions->useCache) { return $this->retrieveFromCache($key); } $list = $this->listLookup->fetchList(); $this->saveToCache($key, $list, $this->listLookup->getTimestamp(), $this->cacheOptions->ttl); return $list; }
private function hasMatchFor($id, $key, DIWikiPage $subject) { $key = $id . '#' . $key; if ($this->cache->contains($key)) { return $this->cache->fetch($key); } $requestOptions = new RequestOptions(); $requestOptions->limit = 1; $result = $this->store->getPropertySubjects(new DIProperty($id), $subject, $requestOptions); $this->cache->save($key, $result !== array()); return $result !== array(); }
/** * @since 2.1 * * @param integer $id * * @return DIWikiPage|null */ public function getDataItemForId($id) { if (!$this->cache->contains($id)) { $row = $this->connection->selectRow($this->tableName, array('smw_title', 'smw_namespace', 'smw_iw', 'smw_subobject'), array('smw_id' => $id), __METHOD__); if ($row === false) { return null; } $hash = HashBuilder::createHashIdFromSegments($row->smw_title, $row->smw_namespace, $row->smw_iw, $row->smw_subobject); $this->saveToCache($id, $hash); } return HashBuilder::newDiWikiPageFromHash($this->cache->fetch($id)); }
private function retrieveFromCache($key) { if (!$this->cache->contains($key) || !$this->cacheOptions->useCache) { return array(); } $data = $this->cache->fetch($key); $this->isCached = true; $this->timestamp = $data['time']; return unserialize($data['content']); }
private function findMatchesWith($id, $key, DIWikiPage $subject, $requestOptions) { $key = $id . '#' . $key . '#' . $requestOptions->getHash(); if ($this->cache->contains($key)) { return $this->cache->fetch($key); } $result = $this->store->getPropertySubjects(new DIProperty($id), $subject, $requestOptions); $this->cache->save($key, $result); wfDebugLog('smw', __METHOD__ . " {$id} and " . $subject->getDBKey() . "\n"); return $result; }
private function findMatchesFor($id, $key, DIWikiPage $subject) { $key = 'f#' . $id . '#' . $key; if ($this->cache->contains($key)) { return unserialize($this->cache->fetch($key)); } $requestOptions = new RequestOptions(); $result = $this->store->getPropertySubjects(new DIProperty($id), $subject, $requestOptions); $this->cache->save($key, serialize($result)); wfDebugLog('smw', __METHOD__ . " {$id} and " . $subject->getDBKey() . "\n"); return $result; }
private function tryFetchFromCache($key, $optionsKey) { if (!$this->cache->contains($key)) { return null; } $data = unserialize($this->cache->fetch($optionsKey)); if ($data === array()) { return null; } $this->isFromCache = true; $this->timestamp = $data['time']; return $data['list']; }
/** * @since 2.5 * * @param string $languageCode * @param boolean $readFromFile * * @return array * @throws RuntimeException */ public function readByLanguageCode($languageCode, $readFromFile = false) { $languageCode = strtolower(trim($languageCode)); if (!$readFromFile && isset(self::$contents[$languageCode])) { return self::$contents[$languageCode]; } $cacheKey = $this->getCacheKeyFrom($languageCode); if (!$readFromFile && !$this->skipCache && !isset(self::$contents[$languageCode]) && $this->cache->contains($cacheKey)) { self::$contents[$languageCode] = $this->cache->fetch($cacheKey); } if ($readFromFile || !isset(self::$contents[$languageCode])) { self::$contents[$languageCode] = $this->doReadJsonContentsFromFileBy($languageCode, $cacheKey); } return self::$contents[$languageCode]; }
/** * @since 2.4 * * @param DIProperty $property * * @return integer|false */ public function getAllowedValuesBy(DIProperty $property) { $allowsValues = array(); $key = 'al:' . $property->getKey(); // Guard against high frequency lookup if ($this->intermediaryMemoryCache->contains($key)) { return $this->intermediaryMemoryCache->fetch($key); } $dataItems = $this->cachedPropertyValuesPrefetcher->getPropertyValues($property->getCanonicalDiWikiPage(), new DIProperty('_PVAL')); if (is_array($dataItems) && $dataItems !== array()) { $allowsValues = $dataItems; } $this->intermediaryMemoryCache->save($key, $allowsValues); return $allowsValues; }
/** * @since 1.0 * * @return mixed */ public function execute() { list($key, $expiry) = $this->getKeysFromOptions(); $this->isFromCache = false; if ($this->cache->contains($key)) { $this->isFromCache = true; return $this->cache->fetch($key); } $response = parent::execute(); // Do not cache any failed response if ($this->getLastErrorCode() !== 0) { return $response; } $this->cache->save($key, $response, $expiry); return $response; }
private function loadListFromCache($location, $languages) { $id = self::CACHE . md5(json_encode($languages) . $this->ttl . self::VERSION); if ($this->cache->contains($id)) { return self::$internalLookupCache = $this->cache->fetch($id); } self::$internalLookupCache = array(); foreach ($languages as $languageCode) { // We silently ignore any error on purpose $contents = json_decode(@file_get_contents($location . $languageCode . '.json'), true); if ($contents === null || json_last_error() !== JSON_ERROR_NONE || !isset($contents[$languageCode])) { continue; } self::$internalLookupCache += array_fill_keys($contents[$languageCode], true); } $this->cache->save($id, self::$internalLookupCache); }
/** * @since 1.0 * * @param string $id * * @return Container */ public function read($id) { $id = $this->getKey($id); // If possible use the raw data from the internal cache // without unserialization if ($this->internalCache->contains($id)) { $data = $this->internalCache->fetch($id); } elseif ($this->cache->contains($id)) { $data = unserialize($this->cache->fetch($id)); $this->internalCache->save($id, $data); } else { $data = array(); } $container = new Container($id, (array) $data); $container->setExpiryInSeconds($this->expiry); return $container; }
/** * Create an ExpElement for some internal resource, given by an * DIWikiPage object. This is the one place in the code where URIs * of wiki pages and user-defined properties are determined. A modifier * can be given to make variants of a URI, typically done for * auxiliary properties. In this case, the URI is modiied by appending * "-23$modifier" where "-23" is the URI encoding of "#" (a symbol not * occuring in MW titles). * * @param DIWikiPage $diWikiPage * @param boolean $markForAuxiliaryUsage * * @return ExpResource */ public function mapWikiPageToResourceElement(DIWikiPage $diWikiPage, $markForAuxiliaryUsage = false) { $modifier = $markForAuxiliaryUsage ? self::AUX_MARKER : ''; $hash = $this->cachePrefix . $diWikiPage->getHash() . $modifier; // If a persistent cache is injected use the ExpElement serializer because // not all cache layers support object de/serialization // ExpElement::newFromSerialization if ($this->cache->contains($hash)) { return $this->cache->fetch($hash); } if ($diWikiPage->getSubobjectName() !== '') { $modifier = $diWikiPage->getSubobjectName(); } $importDataItem = $this->tryToFindImportDataItem($diWikiPage, $modifier); if ($importDataItem instanceof DataItem) { list($localName, $namespace, $namespaceId) = $this->defineElementsForImportDataItem($importDataItem); } else { list($localName, $namespace, $namespaceId) = $this->defineElementsForDiWikiPage($diWikiPage, $modifier); } $resource = new ExpNsResource($localName, $namespace, $namespaceId, $diWikiPage); $this->cache->save($hash, $resource); return $resource; }