apicache icon indicating copy to clipboard operation
apicache copied to clipboard

is dynamic duration possible?

Open emretoprak opened this issue 2 years ago • 3 comments

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';
});

emretoprak avatar Dec 06 '22 10:12 emretoprak

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

sycore0 avatar Dec 07 '22 19:12 sycore0

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 })
})

sycore0 avatar Dec 07 '22 19:12 sycore0

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 )

emretoprak avatar Dec 08 '22 08:12 emretoprak