kwhitley / apicache

Simple API-caching middleware for Express/Node.
MIT License
1.24k stars 193 forks source link

is dynamic duration possible? #248

Open emretoprak opened 1 year ago

emretoprak commented 1 year ago

Hi,

In some cases, it may be necessary to dynamically give the duration parameter.

something like this.

cache((req) => {
  if (req.mycondition) {
    return '1 minute';
  }

  return '1 hour';
});
sycore0 commented 1 year ago

i don't see why you couldn't use a function's output when setting the cache time

sycore0 commented 1 year ago

something like

const dynamicTime = () => {  // ...criteria }

app.use(cache( dynamicTime() ))

 // or with redis

app.get('/will-be-cached', cacheWithRedis( dynamicTime() ), (req, res) => {
  res.json({ success: true })
})
emretoprak commented 1 year ago

Actually, what I want to say is:

I'm using a whole domain as a proxy, but I want to give different cache times for some paths.

const myProxy = createProxyMiddleware({
  target: 'www.my-proxy-url.com'
});

app.use(`/`, apicache.middleware('1 minute'), myProxy);

in this case i want to cache like this.

www.my-proxy-url.com/api/path1 ( 1 minute ) www.my-proxy-url.com/api/path2 ( 1 hour )