Skip to content

Add easy cache invalidation helper to defineCachedFunction/defineCachedHandler #3935

Closed

Description

@schplitt

Describe the feature

When using defineCachedFunction or defineCachedHandler in Nitro, manually invalidating cache entries is complex and error-prone. The cache key depends on the function group, name, and often function parameters, requiring users to reconstruct the cache key string for invalidation:

nitro:functions:getAccessToken:default.json

and call:

await useStorage('cache').removeItem('nitro:functions:getAccessToken:default.json');

This process is not ergonomic, especially in larger projects or when multiple cached functions exist with dynamic keys.

Proposal:
Add a built-in cache invalidation method, such as .invalidateCache(), to the function returned by defineCachedFunction/defineCachedHandler. This helper would automatically generate the correct cache key (using the provided arguments) and invalidate it, making cache management simpler for users.

Example usage:

const getAccessToken = defineCachedFunction(/* ...  */);
await getAccessToken.invalidateCache();

This reduces developer error, avoids code duplication, and improves cache ergonomics in user projects.

Additional information

  • Would you be willing to help implement this feature?

Activity

  1. added
    pending triage
    on Jan 9, 2026
  2. MickL commented on Mar 16, 2026

    @MickL
    Contributor

    I was just about to open the same issue. Setting the key is easy:

    export default defineCachedHandler(async () => {
        // ...
      },
      {
        getKey: () => 'fancy-key',
      },
    );

    Will generate: cache:nitro/functions:getAccessToken:fancy-key.json (according to the docs)

    But to invalidate, we have to write the string ourself and hope that it is correct (probably not):

    await useStorage('cache').removeItem('nitro/handlers:_:fancy-key.json')

    @pi0 Possibly there is already a helper function but it is just not documented?

    Btw. If anyone is searching for the key, one can use await useStorage('cache').getKeys()

  3. schplitt commented on Mar 16, 2026

    @schplitt
    ContributorAuthor

    I was just about to open the same issue. Setting the key is easy:

    export default defineCachedHandler(async () => {
        // ...
      },
      {
        getKey: () => 'fancy-key',
      },
    );

    Will generate: cache:nitro/functions:getAccessToken:fancy-key.json (according to the docs)

    But to invalidate, we have to write the string ourself and hope that it is correct (probably not):

    await useStorage('cache').removeItem('nitro/handlers:_:fancy-key.json')

    @pi0 Possibly there is already a helper function but it is just not documented?

    Btw. If anyone is searching for the key, one can use await useStorage('cache').getKeys()

    @MickL
    I completely forgot about this.
    This can be closed as this will be available soon.
    See https://github.com/unjs/ocache

  4. MickL commented on Mar 16, 2026

    @MickL
    Contributor

    What do you mean by "will be available soon"? You mean it will me used within Nitro?

    Can we leave the issue open until it is merged and published to npm + docs are updated?

    Also, I dont see a function to get the key in ocache, is it not documented?

  5. reopened this on Mar 16, 2026
  6. pi0 commented on Mar 16, 2026

    @pi0
    Member

    Nitro v3 already switched to ocache. We need to move this issue there.

  7. MickL commented on Mar 17, 2026

    @MickL
    Contributor

    Here it is: unjs/ocache#4

  8. pi0 commented on Mar 19, 2026

    @pi0
    Member

    This is now available in ocache (used by Nitro v3) — cached functions now have an .invalidate(...args) method and there's a standalone invalidateCache() helper:

    const getUser = defineCachedFunction(fetchUser, {
      name: "getUser",
      getKey: (id: string) => id,
    });
    
    await getUser.invalidate("user-123");

    See unjs/ocache#8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions