Skip to content

furqanZafar/graceful-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graceful-cache

Install

npm install graceful-cache

Usage

GracefulCache = require("graceful-cache")

// create a new instance of GracefulCache
keywordsCache = new GracefulCache({
    asyncRequest: (keyword, done) => {
        setTimeout(() => {
            done(null, ["term1", "term2"])
        }, 5000)
    },
    lifeSpan: 1000 * 60 * 15, // 15 seconds
    replenish: 1000 * 60 * 7 // revalidate the cache in background when a request happens 7 seconds after the value was cached
    staleWhileRevalidate: true, // in case of a miss, use stale value & update the cache in background
    staleWhileError: true, // in case of an error, use an existing cached value if exists
})

...

app.get("/search/:keyword", (req, res) => {

    // getOrRequestAndCache :: a -> ErrorValueCallback -> ()
    keywordsCache.getOrRequestAndCache(req.params.keyword, (err, results) => {
        if (err) 
            res.status(500).send(err)
        else
            res.send(results)
    })

})

Flow

"Overview" "Performing asyncRequest"

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published