Created
September 15, 2015 04:21
-
-
Save rochnyak-d-i/d02ff8d9320ee23587d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function aop(method, params, moduleName, functionName) { | |
var that = this; | |
// Такое формирование ключа кеширования приведено для простоты примера | |
var key = moduleName + '_' + functionName + '_' + params[0]; | |
cache.get(key, function(error, cachedResult) { | |
// Получаем ссылку на callback-функцию (всегда передаётся последним параметром) | |
var callback = params[params.length - 1]; | |
if (error || !cachedResult) { | |
// Результата в кеше не нашли, передаём управление в метод, подменяя callback-функцию | |
params[params.length - 1] = function(error, result) { | |
callback(error, result); | |
if (!error) cache.set(key, result); | |
}; | |
method.apply(that, params); | |
} else { | |
callback(undefined, cachedResult); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment