You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Offer function to invalidate cached function #2218
I think a nice option would be to expose a .invalidate() method (or we can find a better name) for it.
We could also expose some .meta() to know more about the cache options, key and next expiration.
constgetAccessToken=defineCachedFunction(()=>{returnString(Date.now())},{maxAge: 10,name: 'getAccessToken',getKey: ()=>'default'})// Get the metaawaitgetAccessToken.meta()// Remove the entry in the cache storageawaitgetAccessToken.invalidate()
I think @pi0 may have some great ideas around it 💚
Reacted by Ilya Semenov
Reacted by Pooya Parsa, Honza Pobořil, Alexander Lichter, Max, leex, Gaetan SENN, Vicente Matus, Sandro Circi and Dennis
constgetAccessToken=defineCachedFunction((param: string)=>{returnencode(param+String(Date.now()))},{maxAge: 10,})awaitgetAccessToken("foo")// cache missawaitgetAccessToken("foo")// cache hitawaitgetAccessToken.withBypass("foo")// same as if shouldBypassCache() returned trueawaitgetAccessToken.withInvalidate("foo")// same as if shouldInvalidateCache() returned true
I am using a wrapper for defineCachedFunction adding the above:
constgetAccessToken=defineCachedFunctionWithInvalidate((param: string)=>{ ... })awaitgetAccessToken("foo")// normal useawaitgetAccessToken.withInvalidate("foo")// force invalidate
I'm dealing with this today. Would like to expire/invalidate a cached function, such that the SWR=true is respected. Using removeItem('my_key') will remove the cache'd item altogether, not serve stale data.
So it seems a invalidateItem('my_key') would be the expected function.
Activity
adrienZ commentedon Mar 5, 2024
Is this what you're looking for ?
https://github.com/unjs/nitro/blob/45f212bea448c69e5e9e8b069d0036611b80899f/examples/cached-handler/routes/index.ts#L5
but
shouldInvalidateCacheinstead ofshouldBypassCache?iBobik commentedon Mar 5, 2024
atinux commentedon Mar 6, 2024
When using
defineCahedFunction, Nitro will generate a specific key (learn more):`${options.group}:${options.name}:${options.getKey(...args)}.json`groupis'nitro:functions'nameis'_'So if you have the following:
It will save in the storage the following key:
You can access the cache storage using:
Then if you want to invalidate the cached function:
Hope that helps 😊
I think a nice option would be to expose a
.invalidate()method (or we can find a better name) for it.We could also expose some
.meta()to know more about the cache options, key and next expiration.I think @pi0 may have some great ideas around it 💚
IlyaSemenov commentedon May 12, 2025
Another practical way would be:
I am using a wrapper for
defineCachedFunctionadding the above:then:
drewbaker commentedon Feb 4, 2026
I'm dealing with this today. Would like to expire/invalidate a cached function, such that the SWR=true is respected. Using
removeItem('my_key')will remove the cache'd item altogether, not serve stale data.So it seems a
invalidateItem('my_key')would be the expected function.pi0 commentedon Mar 19, 2026
This is now available in ocache (used by Nitro v3) via
invalidateCache()and.invalidate()on cached functions:Or standalone when you don't have the function reference:
See unjs/ocache#8