Comments (7)
it would be great if we could have an option to save in cache only responses with status 200, or add a handler function on the start configuration
from apicache.
See docs and/or Wiki for statusCode caching bypasses. This has been added!
from apicache.
@tomstrummer I'm about to document this feature now, but I've previously added a pseudo middleware "toggle" param to the existing middleware call. This toggle function has access to the req/res just before the cache pass - so it's the last possible chance to intercept and cancel the cache pass (by returning false). Because it has access to these shared resources, embed any flags you need into one of them and check from within the toggle function.
A quick example:
function noErrors(req, res) {
return !res.customFailure;
}
app.get('/myroute', cache('1 hour', noErrors), function(req, res) {
res.customFailure = true;
res.send('ok!'); // this response will not be cached because noErrors() will return false;
});
Personally I use it to prevent caching on private/admin requests (which should never be stale). This way I can share a single API between public and private requests, and simply detect which type of request is coming through to route it (not not) to the cache layer.
That help I hope? If not, let me know and I'll try to help as best I can.
from apicache.
Hey that's great that you've already worked on this! Is this present in 0.0.12? If so, it's not working for me. What I'm seeing right now is my noErrors
function being called before my request handler.
But if it does work the way you've described, I think it's acceptable. If I called res.status(500)
in my route handler I can then check for res.statusCode < 400
in my cache control function. So it sounds like it would meet my needs.
from apicache.
You know, @tomstrummer, you're absolutely right... this toggle allows the cache bypass before the "work" gets done, not after (to prevent caching). I was using it to check headers, and outcomes/injections from previous middleware layers.
I'll take a look at it and see if I can refactor into a pattern that's more flexible.
from apicache.
👍
from apicache.
Will sort this when I return to the States (and reliable internet) in a few weeks! Thanks for the patience guys!
~
Kevin
On 02 Nov 2015, at 00:17, Aaron Ryden [email protected] wrote:
—
Reply to this email directly or view it on GitHub.
from apicache.
Related Issues (20)
- Browser ignoring cleared cache group HOT 3
- How to ignore cache but still save the result at the end HOT 2
- How do you manage a memory?
- Help with clearing cache HOT 1
- Dont serve cache if API authorization fails HOT 1
- apicache not supported on Node 14 HOT 7
- Having issues using ioredis and typescript
- How can I use stale-while-revalidate with max-age?
- Is there any limit to url length ?
- MAINTAINERS WANTED: Looking for active authors to help with the maintenance/evolution of apicache! HOT 6
- cache-control global options not working correctly HOT 2
- Event when cache is expired HOT 1
- Advice on preventing race conditions caused by multiple calls to the same endpoint made simultaneously (using Redis)? HOT 1
- Add Redis version 4 support HOT 5
- Load balancer + restify bug resolution
- Redis cached data for the same key is never returned, only continuously added on subsequent requests HOT 2
- is dynamic duration possible? HOT 3
- redisClient option not able to store apicache data in redis database HOT 4
- Set X-Cache in response header
- Possible memory leak in MemoryCache
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apicache.