lisaogren / axios-cache-adapter

Caching adapter for axios. Store request results in a configurable store to prevent unneeded network requests.
MIT License
725 stars 108 forks source link

Set maxAge to forever? #242

Open ablakey opened 3 years ago

ablakey commented 3 years ago

I've tried to set maxAge: Number.POSITIVE_INFINITY but that does not get respected as "forever".

Is there a way to tell the cache to simply never invalidate (because I know I'm serving up immutable records at these URLs)?

ghost commented 3 years ago

Hey @ablakey

I don't understand why it does not work 🤔 maxAge is being added to Date.now() to get the actual expiration date. Doing Date.now() + Number.POSITIVE_INFINITY becomes Infinity and Infinity < Date.now() is always false so the cache should end up never expiring.

I will add a test to actually verify the behavior.

Could you share how you setup axios-cache-adapter? Is it in the browser/node? Using a specific store?

Cheers

ghost commented 3 years ago

You may also put axios-cache-adapter in debug mode by passing debug: true option. You will get extra logs to analyze cache behavior 🙂 More specifically you should get cache-hit logs for your URLs configured with maxAge: Number.POSITIVE_INFINITY. If not then there is definitely an issue somewhere.

ablakey commented 3 years ago

Thanks for the tips.

I can reproduce and with debug I get cache-stale.

Configuration

import { setup } from "axios-cache-adapter";

export const axiosInstance = setup({
  baseURL: BASE_URL,
  withCredentials: true,
  cache: {
    maxAge: Number.POSITIVE_INFINITY,
    debug: true,
    exclude: {
      query: false,
    },
  },
  headers: { Accept: `application/json; version=${API_VERSION}` },
});

Debug Logs

[axios-cache-adapter] uuid http://0.0.0.0:8000/floor_datasets/998d8369-8a1e-418e-94af-754f62e9098b/
[axios-cache-adapter] cache-miss /floor_datasets/998d8369-8a1e-418e-94af-754f62e9098b/
[axios-cache-adapter] uuid http://0.0.0.0:8000/floor_datasets/998d8369-8a1e-418e-94af-754f62e9098b/
[axios-cache-adapter] Request config for /floor_datasets/998d8369-8a1e-418e-94af-754f62e9098b/ {maxAge: Infinity, limit: false, store: MemoryStore, key: ƒ, invalidate: ƒ, …}
[axios-cache-adapter] cache-stale /floor_datasets/998d8369-8a1e-418e-94af-754f62e9098b/

If I keep the exact same config and context, and set maxAge to a finite number like 1000 * 60 * 60 it works fine.

Let me know how else I can help you reproduce and debug this issue.