cacheman-file
cacheman-file copied to clipboard
add cache.inset(key, fetchFn, callbackFn)
A general pattern in code that I often see is...
myvar = cache.get('key' useValue)
function useValue(err, val) {
if (err) ...
if (!val) ... code to load, then set val, then follow through with use val;
//use val
}
It happens over, and over, and over again... I tend to prefer the following:
// if the value is in cache, use the cached value
// otherwise use the loadValue method to load and set the value
// return the error, or the value to useValue method.
cache.inset('mykey', loadValue, useValue);
function loadValue(next) {
//code to load the value
next(null, value); //first param, err, second value
}
function useValue(err, value) {
//same err from load, if there
}
IMHO this is a much cleaner pattern, combined with bind it can be extremely clean, where having to check for a key's value then lookup outside the normal flow, is broken.