Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,39 @@ cache.route('index', 5000);
cache.route({ prefix: 'test' }, 5000);
```

Also, you can use `res.express_redis_cache_expire` to specify the expiration of the entry such as:

```js
app.get('/statistic/:range',

// middleware to define cache expiration

function (req, res, next) {
const dayInSeconds = 86400;
const rangeExpirationMappings = {
"past-year": dayInSeconds * 30, // Expires in 1 month
"past-month": dayInSeconds * 7, // Expires in 7 days
"past-week": dayInSeconds // Expires in 1 day
}
// set cache expiration
if ( rangeExpirationMappings[req.params.range] ) {
res.express_redis_cache_expire = rangeExpirationMappings[req.params.range];
}
next();
},

// cache middleware

cache.route(),

// content middleware

function (req, res) {
res.render('stats');
}

);
```
You can also provide a hash of status code / expiration values if you for example want to retry much sooner in failure cases (403/404/500/etc). Status ranges can be specified via `4xx`/`5xx`. You must provide a default value (`xxx`). The most specific rule will be used. For example, if the status code is 200, and there are expirations set for 200, 2xx, and xxx, the expiration for 200 will be used.

```js
Expand Down
9 changes: 8 additions & 1 deletion lib/ExpressRedisCache/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,14 @@ module.exports = (function () {
else if ( typeof options[1] === 'number' || typeof options[1] === 'object') {
expire = options[1];
}



// If a cache expiration has been explicitly attached to `res` then use it as expiration

if ( res.express_redis_cache_expire ) {
expire = res.express_redis_cache_expire;
}

var binary = false;
if ( typeof options[0] === 'object' && typeof options[0].binary === 'boolean' ) {
binary = options[0].binary;
Expand Down