From 0d7b51944e9b1c18859dcb99ed6397859ca17e07 Mon Sep 17 00:00:00 2001 From: sgiehl Date: Sat, 31 May 2014 20:50:25 +0200 Subject: added special DeviceDetectorCache class used for caching DeviceDetector. This cache combines file and static caching to speed up detections as much as possible --- core/CacheFile.php | 2 +- core/DeviceDetectorCache.php | 65 +++++++++++++++++++++++++++ core/Tracker/Settings.php | 4 +- core/Tracker/Visit.php | 1 - piwik.php | 3 -- plugins/DevicesDetection/DevicesDetection.php | 4 +- 6 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 core/DeviceDetectorCache.php diff --git a/core/CacheFile.php b/core/CacheFile.php index f75657f176..5d9000332f 100644 --- a/core/CacheFile.php +++ b/core/CacheFile.php @@ -16,7 +16,7 @@ use Exception; * It is for example used by the Tracker process to cache various settings and websites attributes in tmp/cache/tracker/* * */ -class CacheFile implements \DeviceDetector\Cache\CacheInterface +class CacheFile { // for testing purposes since tests run on both CLI/FPM (changes in CLI can't invalidate // opcache in FPM, so we have to invalidate before reading) diff --git a/core/DeviceDetectorCache.php b/core/DeviceDetectorCache.php new file mode 100644 index 0000000000..744560df92 --- /dev/null +++ b/core/DeviceDetectorCache.php @@ -0,0 +1,65 @@ +cleanupId($id); + + if (array_key_exists($id, self::$staticCache)) { + return self::$staticCache[$id]; + } + + return parent::get($id); + } + + + /** + * A function to store content a cache entry. + * + * @param string $id The cache entry ID + * @param array $content The cache content + * @throws \Exception + * @return bool True if the entry was succesfully stored + */ + public function set($id, $content) + { + if (empty($id)) { + return false; + } + + $id = $this->cleanupId($id); + + self::$staticCache[$id] = $content; + + return parent::set($id, $content); + } +} diff --git a/core/Tracker/Settings.php b/core/Tracker/Settings.php index 251046ca29..8aa5b9a0df 100644 --- a/core/Tracker/Settings.php +++ b/core/Tracker/Settings.php @@ -8,7 +8,7 @@ */ namespace Piwik\Tracker; -use Piwik\CacheFile; +use Piwik\DeviceDetectorCache; use Piwik\Tracker; use DeviceDetector\DeviceDetector; @@ -39,7 +39,7 @@ class Settings $deviceDetector = new DeviceDetector($userAgent); $deviceDetector->discardBotInformation(); - $deviceDetector->setCache(new CacheFile('tracker', 86400)); + $deviceDetector->setCache(new DeviceDetectorCache('tracker', 86400)); $deviceDetector->parse(); $aBrowserInfo = $deviceDetector->getClient(); diff --git a/core/Tracker/Visit.php b/core/Tracker/Visit.php index 75a11e128a..643c6bf31b 100644 --- a/core/Tracker/Visit.php +++ b/core/Tracker/Visit.php @@ -9,7 +9,6 @@ namespace Piwik\Tracker; -use DeviceDetector\DeviceDetector; use Piwik\Common; use Piwik\Config; use Piwik\IP; diff --git a/piwik.php b/piwik.php index 03e405cdd9..2552867936 100644 --- a/piwik.php +++ b/piwik.php @@ -40,9 +40,6 @@ if (!defined('PIWIK_INCLUDE_PATH')) { @ignore_user_abort(true); -/* - * Manually require needed vendor libraries, as composers autorequire would do too much - */ if (file_exists(PIWIK_INCLUDE_PATH . '/vendor/autoload.php')) { $vendorDirectory = PIWIK_INCLUDE_PATH . '/vendor'; } else { diff --git a/plugins/DevicesDetection/DevicesDetection.php b/plugins/DevicesDetection/DevicesDetection.php index dc915e359f..c6c9262f05 100644 --- a/plugins/DevicesDetection/DevicesDetection.php +++ b/plugins/DevicesDetection/DevicesDetection.php @@ -13,9 +13,9 @@ use DeviceDetector\Parser\Device\DeviceParserAbstract AS DeviceParser; use DeviceDetector\DeviceDetector; use Exception; use Piwik\ArchiveProcessor; -use Piwik\CacheFile; use Piwik\Common; use Piwik\Db; +use Piwik\DeviceDetectorCache; use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; use Piwik\WidgetsList; @@ -245,7 +245,7 @@ class DevicesDetection extends \Piwik\Plugin $UAParser = new DeviceDetector($userAgent); $UAParser->discardBotInformation(); - $UAParser->setCache(new CacheFile('tracker', 86400)); + $UAParser->setCache(new DeviceDetectorCache('tracker', 86400)); $UAParser->parse(); $deviceInfo['config_browser_name'] = $UAParser->getClient("type") == 'browser' ? $UAParser->getClient("short_name") : 'UNK'; $deviceInfo['config_browser_version'] = $UAParser->getClient("type") == 'browser' ? $UAParser->getClient("version") : 'UNK'; -- cgit v1.2.3