cacheman-file icon indicating copy to clipboard operation
cacheman-file copied to clipboard

add cache.inset(key, fetchFn, callbackFn)

Open tracker1 opened this issue 11 years ago • 0 comments

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.

tracker1 avatar Feb 21 '14 00:02 tracker1