We have a wrapper around unstable cache:
import { unstable_cache as next_unstable_cache } from 'next/cache'
import { isTest } from '@/lib/config'
type UnstableCache = typeof next_unstable_cache
/**
* Allows you to cache the results of expensive operations, like database queries, and reuse them across multiple
* requests.
*
* @param callback This is an asynchronous function that fetches the data you want to cache.
* It must be a function that returns a Promise.
* @param keyParts This is an array that identifies the cached key. It must contain globally unique values that together
* identify the key of the data being cached. The cache key also includes the arguments passed to the function.
* @param [options] This is an object that controls how the cache behaves. It can contain the following properties:
* @param [options.tags] An array of tags that can be used to control cache invalidation.
* @param [options.revalidate] The number of seconds after which the cache should be revalidated. Default is 86400
* (24 hours in seconds)
* @returns a function that when invoked, returns a Promise that resolves to the cached data. If the data is not in the
* cache, the provided function will be invoked, and its result will be cached and returned.
*/
export const unstable_cache: UnstableCache = (callback, keyParts?, options?) => {
// in test we bypass the cache, return the callback as is
if (isTest) return callback
// provide a saner default ttl
return next_unstable_cache(callback, keyParts, { revalidate: 24 * 60 * 60, ...options })
}
Hopefully we agree that this should by default cache result for 24 hours.
It seems however, as best I have been able to verify, that when I use this wrapper, the data never updates from the originally cached value. In once case it seems to have been cached for at least 21 days.
import { unstable_cache } from '@/lib/api/unstable_cache'
const cachedHubspotOwnerForUserId = unstable_cache(
(userId, mock) => hubspotOwnerForUserId(userId, mock),
['account/executive'],
)
I can control the mock passed in to verify that given differing keys (but same userId) it does return an updated value.
I haven’t found a good way to inspect the data cached using unstable_cache, so I’m hoping you have some insight here.
Best regards,
Christian Sonne