From b869e742b9dc5ec8584b488934d7dcb51ac5a5e5 Mon Sep 17 00:00:00 2001 From: epriestley Date: Tue, 6 Dec 2016 09:34:01 -0800 Subject: [PATCH] Cache README content for repositories Summary: Ref T11954. Especially with higher-latency file stores like S3, we can spend a lot of time reading README data and then pulling it out of file storage. Instead, cache it. Test Plan: Browsed a repostory with a README, saw faster pages. Reviewers: chad Reviewed By: chad Maniphest Tasks: T11954 Differential Revision: https://secure.phabricator.com/D17002 --- .../controller/DiffusionController.php | 71 ++++++++++++------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/src/applications/diffusion/controller/DiffusionController.php b/src/applications/diffusion/controller/DiffusionController.php index f2249811c1..8db2d3a75d 100644 --- a/src/applications/diffusion/controller/DiffusionController.php +++ b/src/applications/diffusion/controller/DiffusionController.php @@ -340,41 +340,60 @@ abstract class DiffusionController extends PhabricatorController { $drequest = $this->getDiffusionRequest(); $viewer = $this->getViewer(); + $repository = $drequest->getRepository(); + $repository_phid = $repository->getPHID(); + $stable_commit = $drequest->getStableCommit(); - try { - $result = $this->callConduitWithDiffusionRequest( - 'diffusion.filecontentquery', - array( - 'path' => $readme_path, - 'commit' => $drequest->getStableCommit(), - )); - } catch (Exception $ex) { - return null; + $cache = PhabricatorCaches::getMutableStructureCache(); + $cache_key = "diffusion". + ".repository({$repository_phid})". + ".commit({$stable_commit})". + ".readme({$readme_path})"; + + $readme_cache = $cache->getKey($cache_key); + if (!$readme_cache) { + try { + $result = $this->callConduitWithDiffusionRequest( + 'diffusion.filecontentquery', + array( + 'path' => $readme_path, + 'commit' => $drequest->getStableCommit(), + )); + } catch (Exception $ex) { + return null; + } + + $file_phid = $result['filePHID']; + if (!$file_phid) { + return null; + } + + $file = id(new PhabricatorFileQuery()) + ->setViewer($viewer) + ->withPHIDs(array($file_phid)) + ->executeOne(); + if (!$file) { + return null; + } + + $corpus = $file->loadFileData(); + + $readme_cache = array( + 'corpus' => $corpus, + ); + + $cache->setKey($cache_key, $readme_cache); } - $file_phid = $result['filePHID']; - if (!$file_phid) { - return null; - } - - $file = id(new PhabricatorFileQuery()) - ->setViewer($viewer) - ->withPHIDs(array($file_phid)) - ->executeOne(); - if (!$file) { - return null; - } - - $corpus = $file->loadFileData(); - - if (!strlen($corpus)) { + $readme_corpus = $readme_cache['corpus']; + if (!strlen($readme_corpus)) { return null; } return id(new DiffusionReadmeView()) ->setUser($this->getViewer()) ->setPath($readme_path) - ->setContent($corpus); + ->setContent($readme_corpus); } }