From 984976ce2029619fd4d32c94d55e45e2bf6c7a2b Mon Sep 17 00:00:00 2001 From: epriestley Date: Wed, 17 Jun 2015 10:43:44 -0700 Subject: [PATCH] Cache viewer spaces Summary: Ref T8575. Although we cache spaces as a whole, we don't cache viewer spaces. This can still do a lot of work if they have complex policies. Instead, cache them in the request cache. Test Plan: Saw this account for 37% of a page in produciton (303ms on on 835ms). Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T8575 Differential Revision: https://secure.phabricator.com/D13324 --- .../query/PhabricatorSpacesNamespaceQuery.php | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/applications/spaces/query/PhabricatorSpacesNamespaceQuery.php b/src/applications/spaces/query/PhabricatorSpacesNamespaceQuery.php index 65ac146bd0..96125b6aef 100644 --- a/src/applications/spaces/query/PhabricatorSpacesNamespaceQuery.php +++ b/src/applications/spaces/query/PhabricatorSpacesNamespaceQuery.php @@ -5,6 +5,7 @@ final class PhabricatorSpacesNamespaceQuery const KEY_ALL = 'spaces.all'; const KEY_DEFAULT = 'spaces.default'; + const KEY_VIEWER = 'spaces.viewer'; private $ids; private $phids; @@ -141,17 +142,25 @@ final class PhabricatorSpacesNamespaceQuery } public static function getViewerSpaces(PhabricatorUser $viewer) { - $spaces = self::getAllSpaces(); + $cache = PhabricatorCaches::getRequestCache(); + $cache_key = self::KEY_VIEWER.'('.$viewer->getPHID().')'; - $result = array(); - foreach ($spaces as $key => $space) { - $can_see = PhabricatorPolicyFilter::hasCapability( - $viewer, - $space, - PhabricatorPolicyCapability::CAN_VIEW); - if ($can_see) { - $result[$key] = $space; + $result = $cache->getKey($cache_key); + if ($result === null) { + $spaces = self::getAllSpaces(); + + $result = array(); + foreach ($spaces as $key => $space) { + $can_see = PhabricatorPolicyFilter::hasCapability( + $viewer, + $space, + PhabricatorPolicyCapability::CAN_VIEW); + if ($can_see) { + $result[$key] = $space; + } } + + $cache->setKey($cache_key, $result); } return $result;