Skip to content
Extraits de code Groupes Projets
Valider bc31046e rédigé par Thomas Steur's avatar Thomas Steur
Parcourir les fichiers

added new staticCache to remove a lot of duplicated code and to have a nice...

added new staticCache to remove a lot of duplicated code and to have a nice interface, by caching the default metrics we reduce the calls to translate from 50k to 2k resulting in good performance improvement...
parent a83037ce
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Cache;
use Piwik\Plugin\Manager as PluginManager;
use Piwik\Translate;
/**
* Caching class used for static caching which is plugin aware. It'll cache the given content depending on the plugins
* that are installed. This prevents you from having to invalidate the cache during tests in case the loaded plugins
* changes etc. The key is language aware as well.
*/
class PluginAwareStaticCache extends StaticCache
{
protected function completeKey($cacheKey)
{
$pluginManager = PluginManager::getInstance();
$pluginNames = $pluginManager->getLoadedPluginsName();
$cacheKey = $cacheKey . md5(implode('', $pluginNames));
return parent::completeKey($cacheKey);
}
}
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Cache;
use Piwik\Translate;
/**
* Caching class used for static caching. The cache automatically caches the key aware of the current loaded language
* to prevent you from having to reset it all the time during tests.
*
* TODO the default static cache should actually not be language aware. Especially since we would end up in classes like
* PluginAwareLanguageAwareStaticCache, PluginAwareStaticCache, PluginAwareXYZStaticCache,... once we have dependency
* injection we could "build" all the caches we need removing duplicated code and extend the static cache by using
* decorators which "enrich" the cache key depending on their awareness.
*/
class StaticCache
{
private static $staticCache = array();
private $cacheKey;
public function __construct($cacheKey)
{
$this->cacheKey = $this->completeKey($cacheKey);
}
public function get()
{
if (self::has()) {
return self::$staticCache[$this->cacheKey];
}
return null;
}
public function has()
{
return array_key_exists($this->cacheKey, self::$staticCache);
}
public function set($content)
{
self::$staticCache[$this->cacheKey] = $content;
}
protected function completeKey($cacheKey)
{
return $cacheKey . Translate::getLanguageLoaded();
}
}
......@@ -9,6 +9,8 @@
namespace Piwik;
use Piwik\Cache\PluginAwareStaticCache;
require_once PIWIK_INCLUDE_PATH . "/core/Piwik.php";
/**
......@@ -219,6 +221,12 @@ class Metrics
static public function getDefaultMetricTranslations()
{
$cache = new PluginAwareStaticCache('DefaultMetricTranslations');
if ($cache->has()) {
return $cache->get();
}
$translations = array(
'label' => 'General_ColumnLabel',
'date' => 'General_Date',
......@@ -264,6 +272,8 @@ class Metrics
$translations = array_map(array('\\Piwik\\Piwik','translate'), $translations);
$cache->set($translations);
return $translations;
}
......@@ -323,6 +333,12 @@ class Metrics
static public function getDefaultMetricsDocumentation()
{
$cache = new PluginAwareStaticCache('DefaultMetricsDocumentation');
if ($cache->has()) {
return $cache->get();
}
$translations = array(
'nb_visits' => 'General_ColumnNbVisitsDocumentation',
'nb_uniq_visitors' => 'General_ColumnNbUniqVisitorsDocumentation',
......@@ -343,7 +359,11 @@ class Metrics
*/
Piwik::postEvent('Metrics.getDefaultMetricDocumentationTranslations', array(&$translations));
return array_map(array('\\Piwik\\Piwik','translate'), $translations);
$translations = array_map(array('\\Piwik\\Piwik','translate'), $translations);
$cache->set($translations);
return $translations;
}
public static function getPercentVisitColumn()
......
......@@ -8,11 +8,12 @@
*/
namespace Piwik\Plugin;
use Piwik\Cache\PluginAwareStaticCache;
use Piwik\Plugin\Manager as PluginManager;
use Piwik\Common;
use Piwik\Db;
use Piwik\Tracker\Action;
use Piwik\Tracker\Request;
use Piwik\Plugin\Manager as PluginManager;
use Piwik\Tracker\Visitor;
use Piwik\Translate;
......@@ -122,12 +123,11 @@ abstract class ActionDimension
/** @return \Piwik\Plugin\ActionDimension[] */
public static function getAllDimensions()
{
$pluginManager = PluginManager::getInstance();
$cacheKey = md5(implode('', $pluginManager->getLoadedPluginsName()) . Translate::getLanguageLoaded());
$cache = new PluginAwareStaticCache('ActionDimensions');
if (!array_key_exists($cacheKey, self::$cachedInstances)) {
if (!$cache->has()) {
$plugins = $pluginManager->getLoadedPlugins();
$plugins = PluginManager::getInstance()->getLoadedPlugins();
$instances = array();
foreach ($plugins as $plugin) {
......@@ -136,10 +136,10 @@ abstract class ActionDimension
}
}
self::$cachedInstances[$cacheKey] = $instances;
$cache->set($instances);
}
return self::$cachedInstances[$cacheKey];
return $cache->get();
}
/** @return \Piwik\Plugin\ActionDimension[] */
......
......@@ -9,6 +9,8 @@
namespace Piwik\Plugin;
use Piwik\API\Proxy;
use Piwik\Cache\PluginAwareStaticCache;
use Piwik\Cache\StaticCache;
use Piwik\Menu\MenuReporting;
use Piwik\Metrics;
use Piwik\Piwik;
......@@ -271,10 +273,9 @@ class Report
public static function getAllReports()
{
$reports = PluginManager::getInstance()->findMultipleComponents('Reports', '\\Piwik\\Plugin\\Report');
$cache = new StaticCache('Reports' . implode('', $reports));
$cacheKey = md5(implode('', $reports) . Translate::getLanguageLoaded());
if (!array_key_exists($cacheKey, self::$cachedInstances)) {
if (!$cache->has()) {
$instances = array();
foreach ($reports as $report) {
......@@ -283,10 +284,10 @@ class Report
usort($instances, array('self', 'sort'));
self::$cachedInstances[$cacheKey] = $instances;
$cache->set($instances);
}
return self::$cachedInstances[$cacheKey];
return $cache->get();
}
/**
......
......@@ -8,6 +8,7 @@
*/
namespace Piwik\Plugin;
use Piwik\Cache\PluginAwareStaticCache;
use Piwik\Common;
use Piwik\Db;
use Piwik\Plugin\Manager as PluginManager;
......@@ -191,12 +192,11 @@ abstract class VisitDimension
/** @return \Piwik\Plugin\VisitDimension[] */
public static function getAllDimensions()
{
$pluginManager = PluginManager::getInstance();
$cacheKey = md5(implode('', $pluginManager->getLoadedPluginsName()) . Translate::getLanguageLoaded());
$cache = new PluginAwareStaticCache('VisitDimensions');
if (!array_key_exists($cacheKey, self::$cachedInstances)) {
if (!$cache->has()) {
$plugins = $pluginManager->getLoadedPlugins();
$plugins = PluginManager::getInstance()->getLoadedPlugins();
$instances = array();
foreach ($plugins as $plugin) {
......@@ -207,10 +207,10 @@ abstract class VisitDimension
usort($instances, array('self', 'sortByRequiredFields'));
self::$cachedInstances[$cacheKey] = $instances;
$cache->set($instances);
}
return self::$cachedInstances[$cacheKey];
return $cache->get();
}
public static function sortByRequiredFields($a, $b)
......
......@@ -21,7 +21,7 @@ class GetDownloads extends Base
$this->dimension = new DownloadUrl();
$this->name = Piwik::translate('General_Downloads');
$this->documentation = Piwik::translate('Actions_DownloadsReportDocumentation', '<br />');
$this->metrics = array_keys($this->getMetrics());
$this->metrics = array('nb_visits', 'nb_hits');
$this->actionToLoadSubTables = $this->action;
$this->order = 9;
......
......@@ -28,7 +28,7 @@ class GetOutlinks extends Base
. Piwik::translate('Actions_OutlinkDocumentation') . '<br />'
. Piwik::translate('General_UsePlusMinusIconsDocumentation');
$this->metrics = array_keys($this->getMetrics());
$this->metrics = array('nb_visits', 'nb_hits');
$this->order = 8;
$this->actionToLoadSubTables = $this->action;
......
......@@ -20,7 +20,7 @@ class GetPageTitlesFollowingSiteSearch extends SiteSearchBase
$this->dimension = new DestinationPage();
$this->name = Piwik::translate('Actions_WidgetPageTitlesFollowingSearch');
$this->documentation = Piwik::translate('Actions_SiteSearchFollowingPagesDoc') . '<br/>' . Piwik::translate('General_UsePlusMinusIconsDocumentation');
$this->metrics = array_keys($this->getMetrics());
$this->metrics = array('nb_hits_following_search', 'nb_hits');
$this->order = 19;
$this->widgetTitle = 'Actions_WidgetPageTitlesFollowingSearch';
}
......
......@@ -22,7 +22,7 @@ class GetSiteSearchCategories extends SiteSearchBase
$this->dimension = new SearchCategory();
$this->name = Piwik::translate('Actions_WidgetSearchCategories');
$this->documentation = Piwik::translate('Actions_SiteSearchCategories1') . '<br/>' . Piwik::translate('Actions_SiteSearchCategories2');
$this->metrics = array_keys($this->getMetrics());
$this->metrics = array('nb_visits', 'nb_pages_per_search', 'exit_rate');
$this->order = 17;
$this->widgetTitle = 'Actions_WidgetSearchCategories';
}
......
......@@ -21,7 +21,7 @@ class GetSiteSearchKeywords extends SiteSearchBase
$this->name = Piwik::translate('Actions_WidgetSearchKeywords');
$this->documentation = Piwik::translate('Actions_SiteSearchKeywordsDocumentation') . '<br/><br/>' . Piwik::translate('Actions_SiteSearchIntro') . '<br/><br/>'
. '<a href="http://piwik.org/docs/site-search/" target="_blank">' . Piwik::translate('Actions_LearnMoreAboutSiteSearchLink') . '</a>';
$this->metrics = array_keys($this->getMetrics());
$this->metrics = array('nb_visits', 'nb_pages_per_search', 'exit_rate');
$this->order = 15;
$this->widgetTitle = 'Actions_WidgetSearchKeywords';
}
......
......@@ -20,7 +20,7 @@ class GetSiteSearchNoResultKeywords extends SiteSearchBase
$this->dimension = new KeywordwithNoSearchResult();
$this->name = Piwik::translate('Actions_WidgetSearchNoResultKeywords');
$this->documentation = Piwik::translate('Actions_SiteSearchIntro') . '<br /><br />' . Piwik::translate('Actions_SiteSearchKeywordsNoResultDocumentation');
$this->metrics = array_keys($this->getMetrics());
$this->metrics = array('nb_visits', 'exit_rate');
$this->order = 16;
$this->widgetTitle = 'Actions_WidgetSearchNoResultKeywords';
}
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter