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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ This major version has been reworked to avoid breaking changes but some edge-cas

## New Features

????
### Cache-Control header

Redis middleware now add a cache-control header with a max-age based on your route expiration, you can opt-out of this behavior by using `res.express_redis_cache_disable_cache_control=true`

# Changes between 0.0.8 and 0.1.x

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ app.get('/index.html',
function (req, res) { ... });
```

You can also specify
## Cache-Control header

Redis middleware add a cache-control header with a max-age based on your route expiration,
you can opt-out of this behavior by using `res.express_redis_cache_disable_cache_control=true`

# Content Type

Expand Down
20 changes: 17 additions & 3 deletions lib/ExpressRedisCache/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,16 @@ function route() {
return next();
}

if (!res._headers["cache-control"] && typeof expire === "number") {
/** set a default cache-control value based on the default expiration **/
if (
!res._headers["cache-control"] &&
typeof expire === "number" &&
!res.express_redis_cache_disable_cache_control
) {
res._headers["cache-control"] = `max-age=${expire}000`;
}

/** if it's cached, display cache **/

if (cache.length && cache[0].body != null) {
res.contentType(cache[0].type || "text/html");
if (binary) {
Expand Down Expand Up @@ -195,13 +199,23 @@ function route() {
return ret;
}

const expire = expirationPolicy(req, res);

/** update the default cache-control value based on the response status **/
if (
!res._headers["cache-control"] &&
typeof expire === "number" &&
!res.express_redis_cache_disable_cache_control
) {
res._headers["cache-control"] = `max-age=${expire}000`;
}
/** Create the new cache **/
self.add(
name,
body,
{
type: this._headers["content-type"],
expire: expirationPolicy(req, res),
expire,
tag
},
function (name, cache) {}); // eslint-disable-line
Expand Down