4 ms·
Unless it’s a one-off thing, it’s also worth considering adding a cache class. This can have the advantages of further decoupling the cache and computation logi
by sigotirandolas 8y ago
Unless it’s a one-off thing, it’s also worth considering adding a cache class. This can have the advantages of further decoupling the cache and computation logic, and being able to switch the cache implementation (e.g. to use a no-op cache for debugging). It also hides the “if” by introducing a “cache pattern”:
return dbcache.getOrCompute(key, function() {
v = db.query("..")
expiry = calcexpiry(v)
return (v, expiry)
});