From dbffad2cfee877adb18f57cecd6936a989ade247 Mon Sep 17 00:00:00 2001 From: mattab <matthieu.aubry@gmail.com> Date: Tue, 29 Sep 2015 13:11:47 +1300 Subject: [PATCH] Added logging statements for cache hits/miss --- core/Tracker/TableLogAction/Cache.php | 40 ++++++++++++++++++++--- tests/PHPUnit/Integration/SegmentTest.php | 4 +-- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/core/Tracker/TableLogAction/Cache.php b/core/Tracker/TableLogAction/Cache.php index 8d7242f04f..d37d5c4875 100644 --- a/core/Tracker/TableLogAction/Cache.php +++ b/core/Tracker/TableLogAction/Cache.php @@ -11,17 +11,38 @@ namespace Piwik\Tracker\TableLogAction; use Piwik\Common; use Piwik\Config; +use Piwik\Container\StaticContainer; +use Psr\Log\LoggerInterface; class Cache { - public $enable; + /** + * @var bool + */ + public $isEnabled; + + /** + * @var int cache lifetime in seconds + */ protected $lifetime; + + /** + * @var LoggerInterface + */ + private $logger; + + /** + * for tests + * + * @var int + */ static public $hits = 0; public function __construct() { - $this->enable = Config::getInstance()->General['enable_segments_subquery_cache']; + $this->isEnabled = (bool)Config::getInstance()->General['enable_segments_subquery_cache']; $this->lifetime = 60 * 10; + $this->logger = StaticContainer::get('Psr\Log\LoggerInterface'); } /** @@ -32,7 +53,7 @@ class Cache */ public function getIdActionFromSegment($valueToMatch, $sql) { - if (!$this->enable) { + if (!$this->isEnabled) { return array( // mark that the returned value is an sql-expression instead of a literal value 'SQL' => $sql, @@ -64,18 +85,19 @@ class Cache */ private function getIdsFromCache($valueToMatch, $sql) { - $cache = \Piwik\Cache::getLazyCache(); - + $cache = $this->buildCache(); $cacheKey = $this->getCacheKey($valueToMatch, $sql); if ($cache->contains($cacheKey) === true) { self::$hits++; + $this->logger->debug("Segment subquery cache HIT (for '$valueToMatch' and SQL '$sql)"); return $cache->fetch($cacheKey); } $ids = $this->fetchActionIdsFromDb($valueToMatch, $sql); $cache->save($cacheKey, $ids, $this->lifetime); + $this->logger->debug("Segment subquery cache SAVE (for '$valueToMatch' and SQL '$sql')'"); return $ids; } @@ -114,4 +136,12 @@ class Cache return $ids; } + + /** + * @return \Piwik\Cache\Lazy + */ + private function buildCache() + { + return \Piwik\Cache::getLazyCache(); + } } \ No newline at end of file diff --git a/tests/PHPUnit/Integration/SegmentTest.php b/tests/PHPUnit/Integration/SegmentTest.php index 9f63f18f89..53945996f6 100644 --- a/tests/PHPUnit/Integration/SegmentTest.php +++ b/tests/PHPUnit/Integration/SegmentTest.php @@ -700,7 +700,7 @@ class SegmentTest extends IntegrationTestCase )); $cache = new TableLogAction\Cache(); - $this->assertTrue( empty($cache->enable) ); + $this->assertTrue( empty($cache->isEnabled) ); $this->assertCacheWasHit($hit = 0); $this->assertEquals($this->removeExtraWhiteSpaces($expected), $this->removeExtraWhiteSpaces($query)); } @@ -770,7 +770,7 @@ class SegmentTest extends IntegrationTestCase )); $cache = new TableLogAction\Cache(); - $this->assertTrue( !empty($cache->enable) ); + $this->assertTrue( !empty($cache->isEnabled) ); $this->assertEquals($this->removeExtraWhiteSpaces($expected), $this->removeExtraWhiteSpaces($query)); } -- GitLab