Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Steur <thomas.steur@gmail.com>2015-02-02 05:02:42 +0300
committerThomas Steur <thomas.steur@gmail.com>2015-02-02 05:02:42 +0300
commit832ecfb6041c47e6de618d484b1ba6d678ebdf71 (patch)
tree283bc9cf8694f3184bd30ad5713677590b70a169 /core/Period
parente349e24aa244d7aaaa364cc326cc76285e02b143 (diff)
Cache generated date range periods.
This is a bit experimental and I need to see whether any test will fail. It takes a long time to generate subperiods for period=range. Eg requesting date=2015-01-04,2015-02-02&module=Referrers&action=getSearchEngines&period=range took about 700ms for each request of 1500ms in total. It now takes only 40ms which means it is 30-40% faster. With the other optimizations the request in total does now only take 700-800ms instead of 1500ms.
Diffstat (limited to 'core/Period')
-rw-r--r--core/Period/Range.php62
1 files changed, 62 insertions, 0 deletions
diff --git a/core/Period/Range.php b/core/Period/Range.php
index d88a5e2da7..4f1c5b3c7f 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\Date;
use Piwik\Period;
@@ -33,6 +34,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'`,
@@ -56,6 +62,41 @@ class Range extends Period
$this->today = $today;
}
+ 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.
*
@@ -156,6 +197,12 @@ class Range extends Period
return;
}
+ $this->loadAllFromCache();
+
+ if ($this->subperiodsProcessed) {
+ return;
+ }
+
parent::generate();
if (preg_match('/(last|previous)([0-9]*)/', $this->strDate, $regs)) {
@@ -214,6 +261,7 @@ class Range extends Period
if ($this->strPeriod != 'range') {
$this->fillArraySubPeriods($startDate, $endDate, $this->strPeriod);
+ $this->cacheAll();
return;
}
@@ -221,6 +269,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();
}
/**
@@ -462,4 +511,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");
+ }
}