Skip to content

Way to dynamically disable/deactivate caching functionality? #37

Open
@joeflack4

Description

@joeflack4

Description

I'm having an issue where I want to debug certain endpoints locally. But since those responses are cached, my breakpoints are never getting reached.

What's the best way to temporarily disable if I want to set to development/debug mode?

What I've tried

I added this condition to only add FastApiCache if in production:

@app.on_event("startup")
async def startup():
    """Start up FastAPI server"""
    app.state.graph = TccmGraph()
    app.state.graph.connect()
    redis = aioredis.from_url(
        url=get_settings().redis_url,
        encoding="utf8",
        decode_responses=True)
    if get_settings().environment_name == 'production':
        FastAPICache.init(
            backend=RedisBackend(redis),
            prefix="fastapi-cache")

But I'm getting the following error, expectedly:

  File "/Users/joeflack4/virtualenvs/ccdh-terminology-service/lib/python3.9/site-packages/fastapi_cache/__init__.py", line 35, in get_backend
    assert cls._backend, "You must call init first!"  # nosec: B101
AssertionError: You must call init first!

Possible solutions

What's the best way to handle this?

  1. Create my own cache decorator wrapper w/ custom logic around FastAPI's?
  2. Initialize FastAPICache.init() but do it differently?

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationquestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions