Skip to content

Commit

Permalink
fix: avoid query too frequently (#1329)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed May 2, 2018
1 parent 34d3a1e commit 84a3037
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions controllers/total.js
Expand Up @@ -9,13 +9,18 @@ const startTime = '' + Date.now();
let cache = null;

module.exports = function* showTotal() {
if (cache && Date.now() - cache.cache_time < 10000) {
// cache 10 seconds
if (cache && Date.now() - cache.cache_time < 120000) {
// cache 120 seconds
this.body = cache;
return;
}

const r = yield [Total.get(), getDownloadTotal()];
if (cache) {
// set cache_time fisrt, avoid query in next time
cache.cache_time = Date.now();
}

const r = yield [ Total.get(), getDownloadTotal() ];
const total = r[0];
const download = r[1];

Expand All @@ -28,7 +33,7 @@ module.exports = function* showTotal() {
total.sync_model = config.syncModel;

cache = total;
total.cache_time = Date.now();
cache.cache_time = Date.now();

this.body = total;
};

0 comments on commit 84a3037

Please sign in to comment.