Skip to content

Commit

Permalink
pkp/pkp-lib#3304 Switch caching implementation to Stash
Browse files Browse the repository at this point in the history
  • Loading branch information
asmecher committed Apr 16, 2020
1 parent a1dbce9 commit 11b0281
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions UsageStatsPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -773,12 +773,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);
Expand Down Expand Up @@ -861,27 +870,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
Expand Down

0 comments on commit 11b0281

Please sign in to comment.