Skip to content

Add easy cache invalidation helper to defineCachedFunction/defineCachedHandler #3935

@schplitt

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions