core icon indicating copy to clipboard operation
core copied to clipboard

Can't clear cache keys in dev

Open Luca-Sett opened this issue 6 months ago • 4 comments

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.

Luca-Sett avatar Jun 26 '25 14:06 Luca-Sett

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

farnabaz avatar Jul 18 '25 11:07 farnabaz

Interesting spot, thanks @farnabaz! Hopefully this gets fixed in unstorage soon 🤞

Luca-Sett avatar Jul 21 '25 11:07 Luca-Sett

@Luca-Sett meanwhile you can use combination of getKeys and removeItem to clear all caches

farnabaz avatar Jul 21 '25 13:07 farnabaz

Thanks, I'll give that a go

Luca-Sett avatar Jul 22 '25 13:07 Luca-Sett