Krukov / cashews

Cache with async power
MIT License
371 stars 22 forks source link

CacheRequestControlMiddleware: Backend not found exception with non-default cache prefix #218

Closed sergey-burkov-trailstone closed 2 months ago

sergey-burkov-trailstone commented 2 months ago

In FastAPI app when initializing a cache with a prefix CacheRequestControlMiddleware is causing CacheRequestControlMiddleware when Cache-Control Header is used.

WorkAround:

`

backend = cache.setup(
    settings_url=FAST_API_CACHE_URL,
    suppress=True,
    prefix=FAST_API_CACHE_PREFIX,
)

# CacheRequestControlMiddleware is not using any prefixes, so we need to add the backend to the default prefix
cache._add_backend(backend)

app.add_middleware(CacheDeleteMiddleware)
app.add_middleware(CacheRequestControlMiddleware)

`

Krukov commented 2 months ago

Thanks for reporting

Shortly it is expected behavior. A prefix in setup defines the key prefix at which this setting will be applied, in your case
FAST_API_CACHE_URL will used with keys started from FAST_API_CACHE_PREFIX. Than mean that there are no backends for all other keys. In general you workaround is the same as you will setup cache without prefix

cache.setup(
    settings_url=FAST_API_CACHE_URL,
    suppress=True,
)

But What I did 1) I've added a prefix for etag key 2) I've added a note in readme about it

Maybe I missed something )) anyway feel free to reopen the issue if it still exists or you have any question about it.

sergey-burkov-trailstone commented 2 months ago

There is no possibility to parametrize/configure CacheRequestControlMiddleware, CacheDeleteMiddleware to point to the caches to be invalidated. The prefix is necessary to simplify different cache versions etc.

I'd really like that by default it would work with all backends registered if not configured otherwise. And for sure it should not break the core logic with exceptions.

sergey-burkov-trailstone commented 2 months ago

+-+---------------- 1 ---------------- | Traceback (most recent call last): | File "/home/sburkov/.virtualenvs/tscs-v2/lib/python3.10/site-packages/starlette/middleware/base.py", line 106, in call | response = await self.dispatch_func(request, call_next) | File "/home/sburkov/.virtualenvs/tscs-v2/lib/python3.10/site-packages/cashews/contrib/fastapi.py", line 72, in dispatch | with context, max_age(cache_control_value), self._cache.detect as detector: | File "/home/linuxbrew/.linuxbrew/opt/python@3.10/lib/python3.10/contextlib.py", line 135, in enter | return next(self.gen) | File "/home/sburkov/.virtualenvs/tscs-v2/lib/python3.10/site-packages/cashews/wrapper/disable_control.py", line 36, in disabling | self.disable(cmds, prefix=prefix) | File "/home/sburkov/.virtualenvs/tscs-v2/lib/python3.10/site-packages/cashews/wrapper/disable_control.py", line 29, in disable | return self._get_backend(prefix).disable(cmds) | File "/home/sburkov/.virtualenvs/tscs-v2/lib/python3.10/site-packages/cashews/wrapper/wrapper.py", line 41, in _getbackend | backend, = self._get_backend_and_config(key) | File "/home/sburkov/.virtualenvs/tscs-v2/lib/python3.10/site-packages/cashews/wrapper/transaction.py", line 36, in _get_backend_and_config | backend, config = super()._get_backend_and_config(key) | File "/home/sburkov/.virtualenvs/tscs-v2/lib/python3.10/site-packages/cashews/wrapper/wrapper.py", line 38, in _get_backend_and_config | raise NotConfiguredError("Backend for given key not configured") | cashews.exceptions.NotConfiguredError: Backend for given key not configured