ptarjan/node-cache — pull request #28 by Colex (2014-12-28 22:42:30+00:00)
Title: Fetch or resolve flow

Added the option use the cache within a non blocking friendly flow. It is based on Rails' cache fetch function but adapted to javascript and a non-blocking architecture.

The current API is for the **fetch** function is:
```javascript
cache.fetch("key").from(expensive_callback).then(result_callback)
````
**expensive_callback** is a non blocking function that *calculates* the value for the given *key*. It is **only** called if there is a **cache miss**. In the case there is **no** cache miss, **result_callback** will be called instantaneously with the value.

Here is one example of usage:
```javascript
function welcome_user(userid) {
cache.fetch("username-"+userid, 60000).from(username_from_database).then(print_welcome)

function print_welcome(username) {
console.log("Welcome " + username)
})

function username_from_database(cache) {
pg.connect("postgres://localhost:3000", function(err, client, done) {
query = "SELECT username FROM users WHERE id = " + userid
client.query(query, function(err, result) {
done()
username = result.rows[0].username
cache.resolve(username)
})
})
}
````