⭐️ bestfetch

Invalidating the Cache

Out of the box, bestfech will never invalidate your cached responses. What this means is that a particular request will only ever hit the server once, unless you configure bestfetch to behave differently.

The reason this library works this way is because cache invalidation is a difficult problem, and each application has its own particular needs. It would be impossible for this library to include a default invalidation strategy and expect it to work for every use case in every app.

Defining Your Invalidation Strategy

You can configure an invalidation strategy that works for your application using the responseCache.useCachedResponse() method.

You only need to call this method a single time; typically before you start your application.

To get started, import the responseCache object:

import { responseCache } from 'bestfetch';

responseCache.useCachedResponse() accepts a single argument: a function.

It works like this: each time that a request is made that has a cached entry, the function that you pass will be called with a single argument: a cacheObject (described below). Return true from the function to use the cached response, or false to immediately invalidate it.

import { responseCache } from 'bestfetch';

// Call this method a single time: before your app mounts.
responseCache.useCachedResponse(() => /* return true or false */);

cacheObject

The function that you pass to useCachedResponse will be passed a single argument: cacheObject. You can use this object to decide whether or not to use the cached reponse or not.

A cacheObject has the following shape:

  • createdAt: A timestamp (in milliseconds) when the response was added to the cache.
  • lastAccessedAt: A timecode (in milliseconds) when this response was last read from the cache.
  • accessCount: How many times the response has been read from the cache.
  • res: The cached response.

Example: Invalidating Every Cached Entry

The simplest example is to define a strategy that rejects every cached entry.

import { responseCache } from 'bestfetch';

responseCache.useCachedResponse(() => false);

With this invalidation strategy in place, it is as if this library doesn't cache any responses at all. This isn't particularly useful, so let's look at more realistic examples.

Example: Invalidate After 10 Minutes

In the following example, we reject cached responses that are older than 10 minutes.

import { responseCache } from 'bestfetch';

// 1000 = 1 second in milliseconds
// * 60 = 1 minute
// * 10 = 10 minutes
const TEN_MINUTES = 1000 * 60 * 10;

responseCache.useCachedResponse(cacheObject => {
  const currentTimestamp = Date.now();
  return currentTimestamp - cacheObject.createdAt <= TEN_MINUTES;
});

Example: Invalidate After 10 Times

In the following example, we only allow a cached response to be used up to 10 times.

import { responseCache } from 'bestfetch';

responseCache.useCachedResponse(cacheObject => {
  return cacheObject.accessCount <= 10;
});