private function loadContent(array $pastes) { $cache = new PhabricatorKeyValueDatabaseCache(); $cache = new PhutilKeyValueCacheProfiler($cache); $cache->setProfiler(PhutilServiceProfiler::getInstance()); $keys = array(); foreach ($pastes as $paste) { $keys[] = $this->getContentCacheKey($paste); } $caches = $cache->getKeys($keys); $results = array(); $need_raw = array(); foreach ($pastes as $key => $paste) { $key = $this->getContentCacheKey($paste); if (isset($caches[$key])) { $paste->attachContent(phutil_safe_html($caches[$key])); $results[$paste->getID()] = $paste; } else { $need_raw[$key] = $paste; } } if (!$need_raw) { return $results; } $write_data = array(); $need_raw = $this->loadRawContent($need_raw); foreach ($need_raw as $key => $paste) { $content = $this->buildContent($paste); $paste->attachContent($content); $write_data[$this->getContentCacheKey($paste)] = (string) $content; $results[$paste->getID()] = $paste; } $cache->setKeys($write_data); return $results; }
private function loadSnippets(array $pastes) { $cache = new PhabricatorKeyValueDatabaseCache(); $cache = new PhutilKeyValueCacheProfiler($cache); $cache->setProfiler(PhutilServiceProfiler::getInstance()); $keys = array(); foreach ($pastes as $paste) { $keys[] = $this->getSnippetCacheKey($paste); } $caches = $cache->getKeys($keys); $need_raw = array(); $have_cache = array(); foreach ($pastes as $paste) { $key = $this->getSnippetCacheKey($paste); if (isset($caches[$key])) { $snippet_data = phutil_json_decode($caches[$key], true); $snippet = new PhabricatorPasteSnippet(phutil_safe_html($snippet_data['content']), $snippet_data['type']); $paste->attachSnippet($snippet); $have_cache[$paste->getPHID()] = true; } else { $need_raw[$key] = $paste; } } if (!$need_raw) { return $pastes; } $write_data = array(); $have_raw = $this->loadRawContent($need_raw); $have_raw = mpull($have_raw, null, 'getPHID'); foreach ($pastes as $key => $paste) { $paste_phid = $paste->getPHID(); if (isset($have_cache[$paste_phid])) { continue; } if (empty($have_raw[$paste_phid])) { unset($pastes[$key]); continue; } $snippet = $this->buildSnippet($paste); $paste->attachSnippet($snippet); $snippet_data = array('content' => (string) $snippet->getContent(), 'type' => (string) $snippet->getType()); $write_data[$this->getSnippetCacheKey($paste)] = phutil_json_encode($snippet_data); } if ($write_data) { $cache->setKeys($write_data); } return $pastes; }
private function loadContent(array $pastes) { $cache = new PhabricatorKeyValueDatabaseCache(); $cache = new PhutilKeyValueCacheProfiler($cache); $cache->setProfiler(PhutilServiceProfiler::getInstance()); $keys = array(); foreach ($pastes as $paste) { $keys[] = $this->getContentCacheKey($paste); } $caches = $cache->getKeys($keys); $need_raw = array(); $have_cache = array(); foreach ($pastes as $paste) { $key = $this->getContentCacheKey($paste); if (isset($caches[$key])) { $paste->attachContent(phutil_safe_html($caches[$key])); $have_cache[$paste->getPHID()] = true; } else { $need_raw[$key] = $paste; } } if (!$need_raw) { return $pastes; } $write_data = array(); $have_raw = $this->loadRawContent($need_raw); $have_raw = mpull($have_raw, null, 'getPHID'); foreach ($pastes as $key => $paste) { $paste_phid = $paste->getPHID(); if (isset($have_cache[$paste_phid])) { continue; } if (empty($have_raw[$paste_phid])) { unset($pastes[$key]); continue; } $content = $this->buildContent($paste); $paste->attachContent($content); $write_data[$this->getContentCacheKey($paste)] = (string) $content; } if ($write_data) { $cache->setKeys($write_data); } return $pastes; }
public static function loadMaps(array $maps) { $keys = array(); foreach ($maps as $map) { list($u, $v) = $map; $keys[self::getCacheKey($u, $v)] = $map; } $cache = new PhabricatorKeyValueDatabaseCache(); $cache = new PhutilKeyValueCacheProfiler($cache); $cache->setProfiler(PhutilServiceProfiler::getInstance()); $results = array(); if ($keys) { $caches = $cache->getKeys(array_keys($keys)); foreach ($caches as $key => $value) { list($u, $v) = $keys[$key]; try { $results[$u][$v] = self::newFromMap(phutil_json_decode($value)); } catch (Exception $ex) { // Ignore, rebuild below. } unset($keys[$key]); } } if ($keys) { $built = self::buildMaps($maps); $write = array(); foreach ($built as $u => $list) { foreach ($list as $v => $map) { $write[self::getCacheKey($u, $v)] = json_encode($map->getMap()); $results[$u][$v] = $map; } } $cache->setKeys($write); } return $results; }