Giter VIP home page Giter VIP logo

Comments (7)

uncedric avatar uncedric commented on August 19, 2024 2

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.

kwhitley avatar kwhitley commented on August 19, 2024 1

See docs and/or Wiki for statusCode caching bypasses. This has been added!

from apicache.

kwhitley avatar kwhitley commented on August 19, 2024

@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.

thom-nic avatar thom-nic commented on August 19, 2024

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.

kwhitley avatar kwhitley commented on August 19, 2024

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.

aaronryden avatar aaronryden commented on August 19, 2024

👍

from apicache.

kwhitley avatar kwhitley commented on August 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.