Commit a166c390 authored by nanahira's avatar nanahira

make cache customizable

parent e0734dc5
...@@ -110,6 +110,7 @@ ...@@ -110,6 +110,7 @@
"challonge": { "challonge": {
"enabled": false, "enabled": false,
"post_detailed_score": true, "post_detailed_score": true,
"cache_ttl": 60000,
"api_key": "123", "api_key": "123",
"tournament_id": "456" "tournament_id": "456"
}, },
......
...@@ -271,12 +271,13 @@ if settings.modules.challonge.enabled ...@@ -271,12 +271,13 @@ if settings.modules.challonge.enabled
challonge = require('challonge').createClient({ challonge = require('challonge').createClient({
apiKey: settings.modules.challonge.api_key apiKey: settings.modules.challonge.api_key
}) })
challonge_cache = [] if settings.modules.challonge.cache_ttl
challonge_cache = []
challonge_queue_callbacks = [[], []] challonge_queue_callbacks = [[], []]
is_requesting = [false, false] is_requesting = [false, false]
get_callback = (challonge_type, _callback) -> get_callback = (challonge_type, _callback) ->
return ((err, data) -> return ((err, data) ->
if !err and data if settings.modules.challonge.cache_ttl and !err and data
challonge_cache[challonge_type] = data challonge_cache[challonge_type] = data
_callback(err, data) _callback(err, data)
while challonge_queue_callbacks[challonge_type].length while challonge_queue_callbacks[challonge_type].length
...@@ -286,7 +287,7 @@ if settings.modules.challonge.enabled ...@@ -286,7 +287,7 @@ if settings.modules.challonge.enabled
return return
) )
challonge.participants._index = (_data) -> challonge.participants._index = (_data) ->
if challonge_cache[0] if settings.modules.challonge.cache_ttl and challonge_cache[0]
_data.callback(null, challonge_cache[0]) _data.callback(null, challonge_cache[0])
else if is_requesting[0] else if is_requesting[0]
challonge_queue_callbacks[0].push(_data.callback) challonge_queue_callbacks[0].push(_data.callback)
...@@ -296,7 +297,7 @@ if settings.modules.challonge.enabled ...@@ -296,7 +297,7 @@ if settings.modules.challonge.enabled
challonge.participants.index(_data) challonge.participants.index(_data)
return return
challonge.matches._index = (_data) -> challonge.matches._index = (_data) ->
if challonge_cache[1] if settings.modules.challonge.cache_ttl and challonge_cache[1]
_data.callback(null, challonge_cache[1]) _data.callback(null, challonge_cache[1])
else if is_requesting[1] else if is_requesting[1]
challonge_queue_callbacks[1].push(_data.callback) challonge_queue_callbacks[1].push(_data.callback)
...@@ -306,8 +307,9 @@ if settings.modules.challonge.enabled ...@@ -306,8 +307,9 @@ if settings.modules.challonge.enabled
challonge.matches.index(_data) challonge.matches.index(_data)
return return
refresh_challonge_cache = () -> refresh_challonge_cache = () ->
challonge_cache[0] = null if settings.modules.challonge.cache_ttl
challonge_cache[1] = null challonge_cache[0] = null
challonge_cache[1] = null
return return
refresh_challonge_cache() refresh_challonge_cache()
# challonge.participants._index({ # challonge.participants._index({
...@@ -322,7 +324,8 @@ if settings.modules.challonge.enabled ...@@ -322,7 +324,8 @@ if settings.modules.challonge.enabled
# return # return
# ) # )
# }) # })
setInterval(refresh_challonge_cache, 60000) if settings.modules.challonge.cache_ttl
setInterval(refresh_challonge_cache, settings.modules.challonge.cache_ttl)
# 获取可用内存 # 获取可用内存
memory_usage = 0 memory_usage = 0
......
...@@ -318,13 +318,15 @@ ...@@ -318,13 +318,15 @@
challonge = require('challonge').createClient({ challonge = require('challonge').createClient({
apiKey: settings.modules.challonge.api_key apiKey: settings.modules.challonge.api_key
}); });
challonge_cache = []; if (settings.modules.challonge.cache_ttl) {
challonge_cache = [];
}
challonge_queue_callbacks = [[], []]; challonge_queue_callbacks = [[], []];
is_requesting = [false, false]; is_requesting = [false, false];
get_callback = function(challonge_type, _callback) { get_callback = function(challonge_type, _callback) {
return (function(err, data) { return (function(err, data) {
var cur_callback; var cur_callback;
if (!err && data) { if (settings.modules.challonge.cache_ttl && !err && data) {
challonge_cache[challonge_type] = data; challonge_cache[challonge_type] = data;
} }
_callback(err, data); _callback(err, data);
...@@ -336,7 +338,7 @@ ...@@ -336,7 +338,7 @@
}); });
}; };
challonge.participants._index = function(_data) { challonge.participants._index = function(_data) {
if (challonge_cache[0]) { if (settings.modules.challonge.cache_ttl && challonge_cache[0]) {
_data.callback(null, challonge_cache[0]); _data.callback(null, challonge_cache[0]);
} else if (is_requesting[0]) { } else if (is_requesting[0]) {
challonge_queue_callbacks[0].push(_data.callback); challonge_queue_callbacks[0].push(_data.callback);
...@@ -347,7 +349,7 @@ ...@@ -347,7 +349,7 @@
} }
}; };
challonge.matches._index = function(_data) { challonge.matches._index = function(_data) {
if (challonge_cache[1]) { if (settings.modules.challonge.cache_ttl && challonge_cache[1]) {
_data.callback(null, challonge_cache[1]); _data.callback(null, challonge_cache[1]);
} else if (is_requesting[1]) { } else if (is_requesting[1]) {
challonge_queue_callbacks[1].push(_data.callback); challonge_queue_callbacks[1].push(_data.callback);
...@@ -358,11 +360,15 @@ ...@@ -358,11 +360,15 @@
} }
}; };
refresh_challonge_cache = function() { refresh_challonge_cache = function() {
challonge_cache[0] = null; if (settings.modules.challonge.cache_ttl) {
challonge_cache[1] = null; challonge_cache[0] = null;
challonge_cache[1] = null;
}
}; };
refresh_challonge_cache(); refresh_challonge_cache();
setInterval(refresh_challonge_cache, 60000); if (settings.modules.challonge.cache_ttl) {
setInterval(refresh_challonge_cache, settings.modules.challonge.cache_ttl);
}
} }
memory_usage = 0; memory_usage = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment