diff options
author | Matthieu Aubry <matt@piwik.org> | 2015-02-09 09:38:22 +0300 |
---|---|---|
committer | Matthieu Aubry <matt@piwik.org> | 2015-02-09 09:38:22 +0300 |
commit | 19acdb5ec0e63b28a1a9029e398a737d093477fc (patch) | |
tree | f7881196a07439a2b321ea0856bd38539ce9fb86 | |
parent | eb29435adb435104ca94d82ba7422f798b729565 (diff) | |
parent | 832ecfb6041c47e6de618d484b1ba6d678ebdf71 (diff) |
Merge pull request #7119 from piwik/performance_improvements
Various performance improvements, especially for Range period and installations with many sites
-rw-r--r-- | core/API/DataTableManipulator.php | 12 | ||||
-rw-r--r-- | core/Common.php | 48 | ||||
-rw-r--r-- | core/Period/Range.php | 62 | ||||
-rw-r--r-- | core/ViewDataTable/Manager.php | 11 |
4 files changed, 119 insertions, 14 deletions
diff --git a/core/API/DataTableManipulator.php b/core/API/DataTableManipulator.php index a1acc6e5b5..862f2db087 100644 --- a/core/API/DataTableManipulator.php +++ b/core/API/DataTableManipulator.php @@ -124,7 +124,7 @@ abstract class DataTableManipulator } } - $method = $this->getApiMethodForSubtable(); + $method = $this->getApiMethodForSubtable($request); return $this->callApiAndReturnDataTable($this->apiModule, $method, $request); } @@ -144,10 +144,16 @@ abstract class DataTableManipulator * @throws Exception * @return string */ - private function getApiMethodForSubtable() + private function getApiMethodForSubtable($request) { if (!$this->apiMethodForSubtable) { - $meta = API::getInstance()->getMetadata('all', $this->apiModule, $this->apiMethod); + if (!empty($request['idSite'])) { + $idSite = $request['idSite']; + } else { + $idSite = 'all'; + } + + $meta = API::getInstance()->getMetadata($idSite, $this->apiModule, $this->apiMethod); if (empty($meta)) { throw new Exception(sprintf( diff --git a/core/Common.php b/core/Common.php index 954aaa1751..47511a5e89 100644 --- a/core/Common.php +++ b/core/Common.php @@ -829,11 +829,20 @@ class Common */ public static function getSearchEngineUrls() { - require_once PIWIK_INCLUDE_PATH . '/core/DataFiles/SearchEngines.php'; + $cacheId = 'Common.getSearchEngineUrls'; + $cache = Cache::getTransientCache(); + $searchEngines = $cache->fetch($cacheId); - $searchEngines = $GLOBALS['Piwik_SearchEngines']; + if (empty($searchEngines)) { - Piwik::postEvent('Referrer.addSearchEngineUrls', array(&$searchEngines)); + require_once PIWIK_INCLUDE_PATH . '/core/DataFiles/SearchEngines.php'; + + $searchEngines = $GLOBALS['Piwik_SearchEngines']; + + Piwik::postEvent('Referrer.addSearchEngineUrls', array(&$searchEngines)); + + $cache->save($cacheId, $searchEngines); + } return $searchEngines; } @@ -847,13 +856,21 @@ class Common */ public static function getSearchEngineNames() { - $searchEngines = self::getSearchEngineUrls(); + $cacheId = 'Common.getSearchEngineNames'; + $cache = Cache::getTransientCache(); + $nameToUrl = $cache->fetch($cacheId); + + if (empty($nameToUrl)) { + + $searchEngines = self::getSearchEngineUrls(); - $nameToUrl = array(); - foreach ($searchEngines as $url => $info) { - if (!isset($nameToUrl[$info[0]])) { - $nameToUrl[$info[0]] = $url; + $nameToUrl = array(); + foreach ($searchEngines as $url => $info) { + if (!isset($nameToUrl[$info[0]])) { + $nameToUrl[$info[0]] = $url; + } } + $cache->save($cacheId, $nameToUrl); } return $nameToUrl; @@ -868,11 +885,20 @@ class Common */ public static function getSocialUrls() { - require_once PIWIK_INCLUDE_PATH . '/core/DataFiles/Socials.php'; + $cacheId = 'Common.getSocialUrls'; + $cache = Cache::getTransientCache(); + $socialUrls = $cache->fetch($cacheId); + + if (empty($socialUrls)) { + + require_once PIWIK_INCLUDE_PATH . '/core/DataFiles/Socials.php'; - $socialUrls = $GLOBALS['Piwik_socialUrl']; + $socialUrls = $GLOBALS['Piwik_socialUrl']; - Piwik::postEvent('Referrer.addSocialUrls', array(&$socialUrls)); + Piwik::postEvent('Referrer.addSocialUrls', array(&$socialUrls)); + + $cache->save($cacheId, $socialUrls); + } return $socialUrls; } diff --git a/core/Period/Range.php b/core/Period/Range.php index b264c8bb6b..77d7afdcbd 100644 --- a/core/Period/Range.php +++ b/core/Period/Range.php @@ -9,6 +9,7 @@ namespace Piwik\Period; use Exception; +use Piwik\Cache; use Piwik\Common; use Piwik\Container\StaticContainer; use Piwik\Date; @@ -34,6 +35,11 @@ class Range extends Period protected $today; /** + * @var null|Date + */ + protected $defaultEndDate; + + /** * Constructor. * * @param string $strPeriod The type of period each subperiod is. Either `'day'`, `'week'`, @@ -59,6 +65,41 @@ class Range extends Period $this->translator = StaticContainer::get('Piwik\Translation\Translator'); } + private function getCache() + { + return Cache::getTransientCache(); + } + + private function getCacheId() + { + $end = ''; + if ($this->defaultEndDate) { + $end = $this->defaultEndDate->getTimestamp(); + } + + $today = $this->today->getTimestamp(); + + return 'range' . $this->strPeriod . $this->strDate . $this->timezone . $end . $today; + } + + private function loadAllFromCache() + { + $range = $this->getCache()->fetch($this->getCacheId()); + + if (!empty($range)) { + foreach ($range as $key => $val) { + $this->$key = $val; + } + } + } + + private function cacheAll() + { + $props = get_object_vars($this); + + $this->getCache()->save($this->getCacheId(), $props); + } + /** * Returns the current period as a localized short string. * @@ -159,6 +200,12 @@ class Range extends Period return; } + $this->loadAllFromCache(); + + if ($this->subperiodsProcessed) { + return; + } + parent::generate(); if (preg_match('/(last|previous)([0-9]*)/', $this->strDate, $regs)) { @@ -212,6 +259,7 @@ class Range extends Period if ($this->strPeriod != 'range') { $this->fillArraySubPeriods($startDate, $endDate, $this->strPeriod); + $this->cacheAll(); return; } @@ -219,6 +267,7 @@ class Range extends Period // When period=range, we want End Date to be the actual specified end date, // rather than the end of the month / week / whatever is used for processing this range $this->endDate = $endDate; + $this->cacheAll(); } /** @@ -460,4 +509,17 @@ class Range extends Period return $isEndOfWeekLaterThanEndDate; } + + /** + * Returns the date range string comprising two dates + * + * @return string eg, `'2012-01-01,2012-01-31'`. + */ + public function getRangeString() + { + $dateStart = $this->getDateStart(); + $dateEnd = $this->getDateEnd(); + + return $dateStart->toString("Y-m-d") . "," . $dateEnd->toString("Y-m-d"); + } } diff --git a/core/ViewDataTable/Manager.php b/core/ViewDataTable/Manager.php index 5d05650657..aff1774368 100644 --- a/core/ViewDataTable/Manager.php +++ b/core/ViewDataTable/Manager.php @@ -8,6 +8,7 @@ */ namespace Piwik\ViewDataTable; +use Piwik\Cache; use Piwik\Common; use Piwik\Option; use Piwik\Piwik; @@ -63,6 +64,14 @@ class Manager */ public static function getAvailableViewDataTables() { + $cache = Cache::getTransientCache(); + $cacheId = 'ViewDataTable.getAvailableViewDataTables'; + $dataTables = $cache->fetch($cacheId); + + if (!empty($dataTables)) { + return $dataTables; + } + $klassToExtend = '\\Piwik\\Plugin\\ViewDataTable'; /** @var string[] $visualizations */ @@ -107,6 +116,8 @@ class Manager $result[$vizId] = $viz; } + $cache->save($cacheId, $result); + return $result; } |