Skip to content
Extraits de code Groupes Projets
Valider dbffad2c rédigé par mattab's avatar mattab
Parcourir les fichiers

Added logging statements for cache hits/miss

parent 2e09977d
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -11,17 +11,38 @@ namespace Piwik\Tracker\TableLogAction; ...@@ -11,17 +11,38 @@ namespace Piwik\Tracker\TableLogAction;
use Piwik\Common; use Piwik\Common;
use Piwik\Config; use Piwik\Config;
use Piwik\Container\StaticContainer;
use Psr\Log\LoggerInterface;
class Cache class Cache
{ {
public $enable; /**
* @var bool
*/
public $isEnabled;
/**
* @var int cache lifetime in seconds
*/
protected $lifetime; protected $lifetime;
/**
* @var LoggerInterface
*/
private $logger;
/**
* for tests
*
* @var int
*/
static public $hits = 0; static public $hits = 0;
public function __construct() 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->lifetime = 60 * 10;
$this->logger = StaticContainer::get('Psr\Log\LoggerInterface');
} }
/** /**
...@@ -32,7 +53,7 @@ class Cache ...@@ -32,7 +53,7 @@ class Cache
*/ */
public function getIdActionFromSegment($valueToMatch, $sql) public function getIdActionFromSegment($valueToMatch, $sql)
{ {
if (!$this->enable) { if (!$this->isEnabled) {
return array( return array(
// mark that the returned value is an sql-expression instead of a literal value // mark that the returned value is an sql-expression instead of a literal value
'SQL' => $sql, 'SQL' => $sql,
...@@ -64,18 +85,19 @@ class Cache ...@@ -64,18 +85,19 @@ class Cache
*/ */
private function getIdsFromCache($valueToMatch, $sql) private function getIdsFromCache($valueToMatch, $sql)
{ {
$cache = \Piwik\Cache::getLazyCache(); $cache = $this->buildCache();
$cacheKey = $this->getCacheKey($valueToMatch, $sql); $cacheKey = $this->getCacheKey($valueToMatch, $sql);
if ($cache->contains($cacheKey) === true) { if ($cache->contains($cacheKey) === true) {
self::$hits++; self::$hits++;
$this->logger->debug("Segment subquery cache HIT (for '$valueToMatch' and SQL '$sql)");
return $cache->fetch($cacheKey); return $cache->fetch($cacheKey);
} }
$ids = $this->fetchActionIdsFromDb($valueToMatch, $sql); $ids = $this->fetchActionIdsFromDb($valueToMatch, $sql);
$cache->save($cacheKey, $ids, $this->lifetime); $cache->save($cacheKey, $ids, $this->lifetime);
$this->logger->debug("Segment subquery cache SAVE (for '$valueToMatch' and SQL '$sql')'");
return $ids; return $ids;
} }
...@@ -114,4 +136,12 @@ class Cache ...@@ -114,4 +136,12 @@ class Cache
return $ids; return $ids;
} }
/**
* @return \Piwik\Cache\Lazy
*/
private function buildCache()
{
return \Piwik\Cache::getLazyCache();
}
} }
\ No newline at end of file
...@@ -700,7 +700,7 @@ class SegmentTest extends IntegrationTestCase ...@@ -700,7 +700,7 @@ class SegmentTest extends IntegrationTestCase
)); ));
$cache = new TableLogAction\Cache(); $cache = new TableLogAction\Cache();
$this->assertTrue( empty($cache->enable) ); $this->assertTrue( empty($cache->isEnabled) );
$this->assertCacheWasHit($hit = 0); $this->assertCacheWasHit($hit = 0);
$this->assertEquals($this->removeExtraWhiteSpaces($expected), $this->removeExtraWhiteSpaces($query)); $this->assertEquals($this->removeExtraWhiteSpaces($expected), $this->removeExtraWhiteSpaces($query));
} }
...@@ -770,7 +770,7 @@ class SegmentTest extends IntegrationTestCase ...@@ -770,7 +770,7 @@ class SegmentTest extends IntegrationTestCase
)); ));
$cache = new TableLogAction\Cache(); $cache = new TableLogAction\Cache();
$this->assertTrue( !empty($cache->enable) ); $this->assertTrue( !empty($cache->isEnabled) );
$this->assertEquals($this->removeExtraWhiteSpaces($expected), $this->removeExtraWhiteSpaces($query)); $this->assertEquals($this->removeExtraWhiteSpaces($expected), $this->removeExtraWhiteSpaces($query));
} }
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter