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