PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Tracker / TableLogAction / Cache.php
matomo / app / core / Tracker / TableLogAction Last commit date
Cache.php 6 years ago
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 }