fastapi-cache icon indicating copy to clipboard operation
fastapi-cache copied to clipboard

Calling FastAPICache.clear when not initialized

Open den4uk opened this issue 3 years ago • 0 comments

__version__ = "0.1.9"

The .clear method needs to be made possible to call without having to have called .init before.

You can have a genuine user case, such as when a third party package may have called or not called the FastAPICache.init.

Therefore, when calling FastAPICache.clear, it should not rely on properties set, such as cls._prefix or even needing the namespace. The parameter for the namespace defaults to None, however, it is not handled in the code.

Currently, calling FastAPICache.clear results in this error:

await FastAPICache.clear()
  File ".../lib/python3.9/site-packages/fastapi_cache/__init__.py", line 64, in clear
    return await cls._backend.clear(namespace, key)
AttributeError: 'NoneType' object has no attribute 'clear'

Could we discuss modifying the behaviour, so:

  • Calling FastAPICache.clear can pass silently if it has not been initialised before.
  • Not passing any params (leaving defaults), would result in a global clearance of the cache.

On a side note, an alternative way would be not to have class FastAPICache acting as a singleton, but rather to be able to handle multiple caches.

Thoughts, please?

den4uk avatar Aug 30 '22 10:08 den4uk