Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
fix: add cache on total
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed May 24, 2019
1 parent 3ffe576 commit 8d57216
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions controllers/total.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const Total = require('../services/total');
const version = require('../package.json').version;
const config = require('../config');
const getDownloadTotal = require('./utils').getDownloadTotal;
const cacheClient = require('../common/cache');
const logger = require('../common/logger');

const startTime = '' + Date.now();
let cache = null;
Expand All @@ -15,6 +17,15 @@ module.exports = function* showTotal() {
return;
}

const cacheKey = 'registry_total';
if (cacheClient) {
const result = yield cacheClient.get(cacheKey);
if (result) {
this.body = JSON.parse(result);
return;
}
}

if (cache) {
// set cache_time fisrt, avoid query in next time
cache.cache_time = Date.now();
Expand All @@ -36,4 +47,14 @@ module.exports = function* showTotal() {
cache.cache_time = Date.now();

this.body = total;
if (cacheClient) {
cacheClient.pipeline()
.set(cacheKey, JSON.stringify(total))
// cache 120s
.expire(cacheKey, 120)
.exec()
.catch(err => {
logger.error(err);
});
}
};

0 comments on commit 8d57216

Please sign in to comment.