Skip to content

@cache: public build_cache_key() helper so custom key builders don't hand-roll the format #264

Description

@allen0099

Problem

A custom key_builder that adds a dimension (user ID, tenant, locale) has to rebuild the whole default key by hand. The per-user example in HTTP_CACHING.md did that and repeated #230: it joined the raw Host header and path with |||. Every hand-rolled builder is one forgotten escape_key_component away from the same key-poisoning bug.

Proposal

Add a public helper that builds the default key and appends extra components, encoding every component:

from fastapi_cachex import build_cache_key

def per_user_key(request: Request) -> str:
    return build_cache_key(request, request.state.user_id)
  • default_key_builder(request) becomes build_cache_key(request).
  • Extra components go through escape_key_component, so they cannot inject the separator either.
  • Update the docs example to use it.

Additive; no key changes for existing routes.

Activity

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

    enhancementNew feature or requesthttp-cacheThe @cache decorator, cache keys and Cache-Control handlingsecuritySecurity vulnerability or hardening

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions