Commit a166c390 authored by nanahira's avatar nanahira

make cache customizable

parent e0734dc5
......@@ -110,6 +110,7 @@
"challonge": {
"enabled": false,
"post_detailed_score": true,
"cache_ttl": 60000,
"api_key": "123",
"tournament_id": "456"
},
......
......@@ -271,12 +271,13 @@ if settings.modules.challonge.enabled
challonge = require('challonge').createClient({
apiKey: settings.modules.challonge.api_key
})
if settings.modules.challonge.cache_ttl
challonge_cache = []
challonge_queue_callbacks = [[], []]
is_requesting = [false, false]
get_callback = (challonge_type, _callback) ->
return ((err, data) ->
if !err and data
if settings.modules.challonge.cache_ttl and !err and data
challonge_cache[challonge_type] = data
_callback(err, data)
while challonge_queue_callbacks[challonge_type].length
......@@ -286,7 +287,7 @@ if settings.modules.challonge.enabled
return
)
challonge.participants._index = (_data) ->
if challonge_cache[0]
if settings.modules.challonge.cache_ttl and challonge_cache[0]
_data.callback(null, challonge_cache[0])
else if is_requesting[0]
challonge_queue_callbacks[0].push(_data.callback)
......@@ -296,7 +297,7 @@ if settings.modules.challonge.enabled
challonge.participants.index(_data)
return
challonge.matches._index = (_data) ->
if challonge_cache[1]
if settings.modules.challonge.cache_ttl and challonge_cache[1]
_data.callback(null, challonge_cache[1])
else if is_requesting[1]
challonge_queue_callbacks[1].push(_data.callback)
......@@ -306,6 +307,7 @@ if settings.modules.challonge.enabled
challonge.matches.index(_data)
return
refresh_challonge_cache = () ->
if settings.modules.challonge.cache_ttl
challonge_cache[0] = null
challonge_cache[1] = null
return
......@@ -322,7 +324,8 @@ if settings.modules.challonge.enabled
# return
# )
# })
setInterval(refresh_challonge_cache, 60000)
if settings.modules.challonge.cache_ttl
setInterval(refresh_challonge_cache, settings.modules.challonge.cache_ttl)
# 获取可用内存
memory_usage = 0
......
......@@ -318,13 +318,15 @@
challonge = require('challonge').createClient({
apiKey: settings.modules.challonge.api_key
});
if (settings.modules.challonge.cache_ttl) {
challonge_cache = [];
}
challonge_queue_callbacks = [[], []];
is_requesting = [false, false];
get_callback = function(challonge_type, _callback) {
return (function(err, data) {
var cur_callback;
if (!err && data) {
if (settings.modules.challonge.cache_ttl && !err && data) {
challonge_cache[challonge_type] = data;
}
_callback(err, data);
......@@ -336,7 +338,7 @@
});
};
challonge.participants._index = function(_data) {
if (challonge_cache[0]) {
if (settings.modules.challonge.cache_ttl && challonge_cache[0]) {
_data.callback(null, challonge_cache[0]);
} else if (is_requesting[0]) {
challonge_queue_callbacks[0].push(_data.callback);
......@@ -347,7 +349,7 @@
}
};
challonge.matches._index = function(_data) {
if (challonge_cache[1]) {
if (settings.modules.challonge.cache_ttl && challonge_cache[1]) {
_data.callback(null, challonge_cache[1]);
} else if (is_requesting[1]) {
challonge_queue_callbacks[1].push(_data.callback);
......@@ -358,11 +360,15 @@
}
};
refresh_challonge_cache = function() {
if (settings.modules.challonge.cache_ttl) {
challonge_cache[0] = null;
challonge_cache[1] = null;
}
};
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;
......
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