PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.0.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.0.2
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 / Cache.php
matomo / app / core / Tracker Last commit date
Db 5 years ago Handler 5 years ago TableLogAction 5 years ago Visit 5 years ago Action.php 5 years ago ActionPageview.php 5 years ago Cache.php 5 years ago Db.php 5 years ago Failures.php 6 years ago FingerprintSalt.php 6 years ago GoalManager.php 5 years ago Handler.php 5 years ago IgnoreCookie.php 5 years ago LogTable.php 5 years ago Model.php 5 years ago PageUrl.php 5 years ago Request.php 5 years ago RequestProcessor.php 5 years ago RequestSet.php 5 years ago Response.php 5 years ago ScheduledTasksRunner.php 5 years ago Settings.php 5 years ago TableLogAction.php 5 years ago TrackerCodeGenerator.php 5 years ago TrackerConfig.php 5 years ago Visit.php 5 years ago VisitExcluded.php 5 years ago VisitInterface.php 5 years ago Visitor.php 5 years ago VisitorNotFoundInDb.php 5 years ago VisitorRecognizer.php 5 years ago
Cache.php
254 lines
1 <?php
2 /**
3 * Matomo - 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 namespace Piwik\Tracker;
10
11 use Piwik\Access;
12 use Piwik\ArchiveProcessor\Rules;
13 use Piwik\Cache as PiwikCache;
14 use Piwik\Config;
15 use Piwik\Container\StaticContainer;
16 use Piwik\Option;
17 use Piwik\Piwik;
18 use Piwik\Tracker;
19 use Psr\Log\LoggerInterface;
20
21 /**
22 * Simple cache mechanism used in Tracker to avoid requesting settings from mysql on every request
23 *
24 */
25 class Cache
26 {
27 private static $cacheIdGeneral = 'general';
28
29 /**
30 * Public for tests only
31 * @var \Matomo\Cache\Lazy
32 */
33 public static $cache;
34
35 /**
36 * @return \Matomo\Cache\Lazy
37 */
38 private static function getCache()
39 {
40 if (is_null(self::$cache)) {
41 self::$cache = PiwikCache::getLazyCache();
42 }
43
44 return self::$cache;
45 }
46
47 private static function getTtl()
48 {
49 return Config::getInstance()->Tracker['tracker_cache_file_ttl'];
50 }
51
52 /**
53 * Returns array containing data about the website: goals, URLs, etc.
54 *
55 * @param int $idSite
56 * @return array
57 */
58 public static function getCacheWebsiteAttributes($idSite)
59 {
60 if ('all' === $idSite) {
61 return array();
62 }
63
64 $idSite = (int) $idSite;
65 if ($idSite <= 0) {
66 return array();
67 }
68
69 $cache = self::getCache();
70 $cacheId = self::getCacheKeyWebsiteAttributes($idSite);
71 $cacheContent = $cache->fetch($cacheId);
72
73 if (false !== $cacheContent) {
74 return $cacheContent;
75 }
76
77 return self::updateCacheWebsiteAttributes($idSite);
78 }
79
80 private static function getCacheKeyWebsiteAttributes($idSite)
81 {
82 return $idSite;
83 }
84
85 /**
86 * Updates the website specific tracker cache containing data about the website: goals, URLs, etc.
87 *
88 * @param int $idSite
89 *
90 * @return array
91 */
92 public static function updateCacheWebsiteAttributes($idSite)
93 {
94 $cache = self::getCache();
95 $cacheId = self::getCacheKeyWebsiteAttributes($idSite);
96
97 Tracker::initCorePiwikInTrackerMode();
98
99 $content = array();
100 Access::doAsSuperUser(function () use (&$content, $idSite) {
101 /**
102 * Triggered to get the attributes of a site entity that might be used by the
103 * Tracker.
104 *
105 * Plugins add new site attributes for use in other tracking events must
106 * use this event to put those attributes in the Tracker Cache.
107 *
108 * **Example**
109 *
110 * public function getSiteAttributes($content, $idSite)
111 * {
112 * $sql = "SELECT info FROM " . Common::prefixTable('myplugin_extra_site_info') . " WHERE idsite = ?";
113 * $content['myplugin_site_data'] = Db::fetchOne($sql, array($idSite));
114 * }
115 *
116 * @param array &$content Array mapping of site attribute names with values.
117 * @param int $idSite The site ID to get attributes for.
118 */
119 Piwik::postEvent('Tracker.Cache.getSiteAttributes', array(&$content, $idSite));
120
121 $logger = StaticContainer::get(LoggerInterface::class);
122 $logger->debug("Website $idSite tracker cache was re-created.");
123 });
124
125 // if nothing is returned from the plugins, we don't save the content
126 // this is not expected: all websites are expected to have at least one URL
127 if (!empty($content)) {
128 $cache->save($cacheId, $content, self::getTtl());
129 }
130
131 Tracker::restoreTrackerPlugins();
132
133 return $content;
134 }
135
136 /**
137 * Clear general (global) cache
138 */
139 public static function clearCacheGeneral()
140 {
141 self::getCache()->delete(self::$cacheIdGeneral);
142 }
143
144 /**
145 * Returns contents of general (global) cache.
146 * If the cache file tmp/cache/tracker/general.php does not exist yet, create it
147 *
148 * @return array
149 */
150 public static function getCacheGeneral()
151 {
152 $cache = self::getCache();
153 $cacheContent = $cache->fetch(self::$cacheIdGeneral);
154
155 if (false !== $cacheContent) {
156 return $cacheContent;
157 }
158
159 return self::updateGeneralCache();
160 }
161
162 /**
163 * Updates the contents of the general (global) cache.
164 *
165 * @return array
166 */
167 public static function updateGeneralCache()
168 {
169 Tracker::initCorePiwikInTrackerMode();
170 $cacheContent = array(
171 'isBrowserTriggerEnabled' => Rules::isBrowserTriggerEnabled(),
172 'lastTrackerCronRun' => Option::get('lastTrackerCronRun'),
173 );
174
175 /**
176 * Triggered before the [general tracker cache](/guides/all-about-tracking#the-tracker-cache)
177 * is saved to disk. This event can be used to add extra content to the cache.
178 *
179 * Data that is used during tracking but is expensive to compute/query should be
180 * cached to keep tracking efficient. One example of such data are options
181 * that are stored in the option table. Querying data for each tracking
182 * request means an extra unnecessary database query for each visitor action. Using
183 * a cache solves this problem.
184 *
185 * **Example**
186 *
187 * public function setTrackerCacheGeneral(&$cacheContent)
188 * {
189 * $cacheContent['MyPlugin.myCacheKey'] = Option::get('MyPlugin_myOption');
190 * }
191 *
192 * @param array &$cacheContent Array of cached data. Each piece of data must be
193 * mapped by name.
194 */
195 Piwik::postEvent('Tracker.setTrackerCacheGeneral', array(&$cacheContent));
196 self::setCacheGeneral($cacheContent);
197
198 $logger = StaticContainer::get(LoggerInterface::class);
199 $logger->debug("General tracker cache was re-created.");
200
201 Tracker::restoreTrackerPlugins();
202
203 return $cacheContent;
204 }
205
206 /**
207 * Store data in general (global cache)
208 *
209 * @param mixed $value
210 * @return bool
211 */
212 public static function setCacheGeneral($value)
213 {
214 $cache = self::getCache();
215
216 return $cache->save(self::$cacheIdGeneral, $value, self::getTtl());
217 }
218
219 /**
220 * Regenerate Tracker cache files
221 *
222 * @param array|int $idSites Array of idSites to clear cache for
223 */
224 public static function regenerateCacheWebsiteAttributes($idSites = array())
225 {
226 if (!is_array($idSites)) {
227 $idSites = array($idSites);
228 }
229
230 foreach ($idSites as $idSite) {
231 self::deleteCacheWebsiteAttributes($idSite);
232 self::getCacheWebsiteAttributes($idSite);
233 }
234 }
235
236 /**
237 * Delete existing Tracker cache
238 *
239 * @param string $idSite (website ID of the site to clear cache for
240 */
241 public static function deleteCacheWebsiteAttributes($idSite)
242 {
243 self::getCache()->delete((int) $idSite);
244 }
245
246 /**
247 * Deletes all Tracker cache files
248 */
249 public static function deleteTrackerCache()
250 {
251 self::getCache()->flushAll();
252 }
253 }
254