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

is dynamic duration possible? #248

Open
emretoprak opened this issue Dec 6, 2022 · 3 comments
Open

is dynamic duration possible? #248

emretoprak opened this issue Dec 6, 2022 · 3 comments

Comments

@emretoprak
Copy link

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 emretoprak changed the title Dynamic duration is possible? is dynamic duration possible? Dec 6, 2022
@sycore0
Copy link

sycore0 commented Dec 7, 2022

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

@sycore0
Copy link

sycore0 commented Dec 7, 2022

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
Copy link
Author

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 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants