Cache.php
160 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace Piwik\Tracker\TableLogAction; |
| 11 | |
| 12 | use Piwik\Common; |
| 13 | use Piwik\Config; |
| 14 | use Psr\Log\LoggerInterface; |
| 15 | |
| 16 | class Cache |
| 17 | { |
| 18 | /** |
| 19 | * @var bool |
| 20 | */ |
| 21 | public $isEnabled; |
| 22 | |
| 23 | /** |
| 24 | * @var int cache lifetime in seconds |
| 25 | */ |
| 26 | protected $lifetime; |
| 27 | |
| 28 | /** |
| 29 | * @var LoggerInterface |
| 30 | */ |
| 31 | private $logger; |
| 32 | |
| 33 | /** |
| 34 | * @var \Piwik\Cache\Lazy |
| 35 | */ |
| 36 | private $cache; |
| 37 | |
| 38 | public function __construct(LoggerInterface $logger, Config $config, \Piwik\Cache\Lazy $cache) |
| 39 | { |
| 40 | $this->isEnabled = (bool)$config->General['enable_segments_subquery_cache']; |
| 41 | $this->limitActionIds = $config->General['segments_subquery_cache_limit']; |
| 42 | $this->lifetime = $config->General['segments_subquery_cache_ttl']; |
| 43 | $this->logger = $logger; |
| 44 | $this->cache = $cache; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param $valueToMatch |
| 49 | * @param $sql |
| 50 | * @return array|null |
| 51 | * @throws \Exception |
| 52 | */ |
| 53 | public function getIdActionFromSegment($valueToMatch, $sql) |
| 54 | { |
| 55 | if (!$this->isEnabled) { |
| 56 | return array( |
| 57 | // mark that the returned value is an sql-expression instead of a literal value |
| 58 | 'SQL' => $sql, |
| 59 | 'bind' => $valueToMatch, |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | $ids = self::getIdsFromCache($valueToMatch, $sql); |
| 64 | |
| 65 | if(is_null($ids)) { |
| 66 | // Too Big To Cache, issue SQL as subquery instead |
| 67 | return array( |
| 68 | 'SQL' => $sql, |
| 69 | 'bind' => $valueToMatch, |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | if(count($ids) == 0) { |
| 74 | return null; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | $sql = Common::getSqlStringFieldsArray($ids); |
| 79 | $bind = $ids; |
| 80 | |
| 81 | return array( |
| 82 | // mark that the returned value is an sql-expression instead of a literal value |
| 83 | 'SQL' => $sql, |
| 84 | 'bind' => $bind, |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /** |
| 90 | * @param $valueToMatch |
| 91 | * @param $sql |
| 92 | * @return array of IDs, or null if the returnset is too big to cache |
| 93 | */ |
| 94 | private function getIdsFromCache($valueToMatch, $sql) |
| 95 | { |
| 96 | $cacheKey = $this->getCacheKey($valueToMatch, $sql); |
| 97 | |
| 98 | if ($this->cache->contains($cacheKey) === true) { // TODO: hits |
| 99 | $this->logger->debug("Segment subquery cache HIT (for '$valueToMatch' and SQL '$sql)"); |
| 100 | return $this->cache->fetch($cacheKey); |
| 101 | } |
| 102 | |
| 103 | $ids = $this->fetchActionIdsFromDb($valueToMatch, $sql); |
| 104 | |
| 105 | if($this->isTooBigToCache($ids)) { |
| 106 | $this->logger->debug("Segment subquery cache SKIPPED SAVE (too many IDs returned by subquery: %s ids)'", array(count($ids))); |
| 107 | $this->cache->save($cacheKey, $ids = null, $this->lifetime); |
| 108 | return null; |
| 109 | } |
| 110 | |
| 111 | $this->cache->save($cacheKey, $ids, $this->lifetime); |
| 112 | $this->logger->debug("Segment subquery cache SAVE (for '$valueToMatch' and SQL '$sql')'"); |
| 113 | |
| 114 | return $ids; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @param $valueToMatch |
| 119 | * @param $sql |
| 120 | * @return string |
| 121 | * @throws |
| 122 | */ |
| 123 | private function getCacheKey($valueToMatch, $sql) |
| 124 | { |
| 125 | if(is_array($valueToMatch)) { |
| 126 | throw new \Exception("value to match is an array: this is not expected"); |
| 127 | } |
| 128 | |
| 129 | $uniqueKey = md5($sql . $valueToMatch); |
| 130 | $cacheKey = 'TableLogAction.getIdActionFromSegment.' . $uniqueKey; |
| 131 | return $cacheKey; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @param $valueToMatch |
| 136 | * @param $sql |
| 137 | * @return array|null |
| 138 | * @throws \Exception |
| 139 | */ |
| 140 | private function fetchActionIdsFromDb($valueToMatch, $sql) |
| 141 | { |
| 142 | $idActions = \Piwik\Db::fetchAll($sql, $valueToMatch); |
| 143 | |
| 144 | $ids = array(); |
| 145 | foreach ($idActions as $idAction) { |
| 146 | $ids[] = $idAction['idaction']; |
| 147 | } |
| 148 | |
| 149 | return $ids; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @param $ids |
| 154 | * @return bool |
| 155 | */ |
| 156 | private function isTooBigToCache($ids) |
| 157 | { |
| 158 | return count($ids) > $this->limitActionIds; |
| 159 | } |
| 160 | } |