From df263f01da885e91f49f97deacb6aa890ddec309 Mon Sep 17 00:00:00 2001 From: Alec Smecher Date: Thu, 16 Apr 2020 11:11:11 -0700 Subject: [PATCH] pkp/pkp-lib#3304 Switch caching implementation to Stash --- UsageStatsPlugin.inc.php | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/UsageStatsPlugin.inc.php b/UsageStatsPlugin.inc.php index 1fc1b03..b397966 100644 --- a/UsageStatsPlugin.inc.php +++ b/UsageStatsPlugin.inc.php @@ -774,12 +774,21 @@ function _hashIp($ip, $salt) { * @return array */ function _getDownloadStats($pubObjectId) { - $cache = CacheManager::getManager()->getCache('downloadStats', $pubObjectId, array($this, '_downloadStatsCacheMiss')); - if (time() - $cache->getCacheTime() > 60 * 60 * 24) { - // Cache is older than one day, erase it. - $cache->flush(); + $pool = new Stash\Pool(Core::getStashDriver()); + $item = $pool->getItem('downloadStats-' . md5($pubObjectId)); + if ($item->isMiss()) { + $reportPlugin = $this->getReportPlugin(); + $application = Application::get(); + $item->set($application->getMetrics( + current($reportPlugin->getMetricTypes()), + [STATISTICS_DIMENSION_MONTH, STATISTICS_DIMENSION_REPRESENTATION_ID], + [STATISTICS_DIMENSION_SUBMISSION_ID => $pubObjectId, STATISTICS_DIMENSION_ASSOC_TYPE => ASSOC_TYPE_SUBMISSION_FILE], + [STATISTICS_DIMENSION_MONTH => STATISTICS_ORDER_ASC] + )); + $item->expiresAfter(60 * 60 * 24); // Valid for one day + $pool->save($item); } - $statsReports = $cache->get($pubObjectId); + $statsReports = $item->get(); $currentYear = date("Y"); $months = range(1, 12); @@ -862,27 +871,6 @@ function getAllDownloadsStats($pubObjectId, $stats = array()) { return $allDownloadStats; } - /** - * Callback to fill cache with data, if empty. - * @param $cache FileCache - * @param $pubObjectId int - * @return array - */ - function _downloadStatsCacheMiss($cache, $pubObjectId) { - $filter = array( - STATISTICS_DIMENSION_SUBMISSION_ID => $pubObjectId, - STATISTICS_DIMENSION_ASSOC_TYPE => ASSOC_TYPE_SUBMISSION_FILE - ); - $orderBy = array(STATISTICS_DIMENSION_MONTH => STATISTICS_ORDER_ASC); - $reportPlugin = $this->getReportPlugin(); - - $application = Application::get(); - - $statsReports = $application->getMetrics(current($reportPlugin->getMetricTypes()), array(STATISTICS_DIMENSION_MONTH, STATISTICS_DIMENSION_REPRESENTATION_ID), $filter, $orderBy); - $cache->setEntireCache(array($pubObjectId => $statsReports)); - return $statsReports; - } - /** * Return a color RGB code to be used in the graph. * @private