Can't clear cache keys in dev
Describe the bug
I can't seem to clear the cache of a cached function created using defineCachedFunction in development.
Steps to reproduce Try to clear a cached function in development, e.g.
/server/api/test/cached.get.js
export default defineEventHandler(async (event) => {
return await cachedFunction(event);
});
const cachedFunction = defineCachedFunction(
async (event) => {
return { now: new Date().toISOString() };
},
{
name: "test",
maxAge: 60 * 10,
swr: false,
},
);
/server/api/test/clear.get.js
export default defineEventHandler(async (event) => {
await useStorage("cache").clear("nitro:functions:test");
return { result: "should be cleared now!" };
});
Visiting /api/test/cached in the browser creates an entry in the cache (can be seen in Nuxt DevTools). When refreshing you can see the same timestamp.
However, visiting /api/test/clear does not clear the cache; revisiting /api/test/cached afterwards shows the same timestamp and I can still see the entry in Nuxt DevTools,
Expected behavior
The cache group is cleared in development when clear() is called.
Thanks for the issue @Luca-Sett, I believe this is upstream issue and related to clear function in unstorage
https://github.com/unjs/unstorage/blob/39534366554b24b7fadac4d67a79864079b45192/src/storage.ts#L384-L401
Replacing getMounts(base, false) with getMounts(base, true) will fix the issue. But I'm not sure about the reason behind using false.
//cc @pi0
Interesting spot, thanks @farnabaz! Hopefully this gets fixed in unstorage soon 🤞
@Luca-Sett meanwhile you can use combination of getKeys and removeItem to clear all caches
Thanks, I'll give that a go