Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore the original behavior of 'x-apicache-force-fetch' header #220

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ routes to be cached from your public client, but NOT cached when from the admin
is achieved by sending a `"x-apicache-bypass": true` header along with the requst from the admin.
The presence of this header flag will bypass the cache, ensuring you aren't looking at stale data.


You can also send a `"x-apicache-force-fetch": true` header which will force a data fetch without bypassing the update of the cache.
So, the next time, the newest data will be returned by the middleware.
Useful for optimizing overall performance of your API by minimizing latency.


## Contributors

Special thanks to all those that use this library and report issues, but especially to the following active users that have helped add to the core functionality!
Expand Down
6 changes: 3 additions & 3 deletions src/apicache.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ function ApiCache() {

// initial bypass chances
if (!opt.enabled) return bypass()
if (req.headers['x-apicache-bypass'] || req.headers['x-apicache-force-fetch']) return bypass()
if (req.headers['x-apicache-bypass']) return bypass()

// REMOVED IN 0.11.1 TO CORRECT MIDDLEWARE TOGGLE EXECUTE ORDER
// if (typeof middlewareToggle === 'function') {
Expand Down Expand Up @@ -635,7 +635,7 @@ function ApiCache() {
var cached = !redis ? memCache.getValue(key) : null

// send if cache hit from memory-cache
if (cached) {
if (cached && !req.headers['x-apicache-force-fetch']) {
var elapsed = new Date() - req.apicacheTimer
debug('sending cached (memory-cache) version of', key, logDuration(elapsed))

Expand All @@ -644,7 +644,7 @@ function ApiCache() {
}

// send if cache hit from redis
if (redis && redis.connected) {
if (redis && redis.connected && !req.headers['x-apicache-force-fetch']) {
try {
redis.hgetall(key, function(err, obj) {
if (!err && obj && obj.response) {
Expand Down