Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkp/pkp-lib#3304 Switch caching implementation to Stash #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 14 additions & 26 deletions UsageStatsPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down