long2ice/fastapi-cache

recent merge causing coder execption

lb-ronyeh opened this issue · 1 comments

hi, an issue appears on the main branch,
the first call does work, and the json response persisted to redis.
the second, where there's a hit, failing with the below error.

can be reproduced

@projects_router.get("")
@cached(expire=60)
def get_projects(
        request: Request,
        page: int = 1,
        limit: int = 20,
        start_date: str = None,
        end_date: str = None
):
    return {"foo":2}

2023-05-14T17:55:30.151362251Z {"asctime": "2023-05-14 17:55:30.145", "levelname": "ERROR", "message": "'str' object has no attribute 'decode'", "exc_info": "Traceback (most recent call last):\n File "/usr/local/lib/python3.9/site-packages/anyio/streams/memory.py", line 94, in receive\n return self.receive_nowait()\n File "/usr/local/lib/python3.9/site-packages/anyio/streams/memory.py", line 89, in receive_nowait\n raise WouldBlock\nanyio.WouldBlock\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 78, in call_next\n message = await recv_stream.receive()\n File "/usr/local/lib/python3.9/site-packages/anyio/streams/memory.py", line 114, in receive\n raise EndOfStream\nanyio.EndOfStream\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File "/usr/local/lib/python3.9/site-packages/starlette/middleware/errors.py", line 162, in call\n await self.app(scope, receive, send)\n File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 108, in call\n response = await self.dispatch_func(request, call_next)\n File "/app/pdt_api/main.py", line 66, in add_headers_and_context\n response = await call_next(request)\n File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 84, in call_next\n raise app_exc\n File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 70, in coro\n await self.app(scope, receive_or_disconnect, send_no_error)\n File "/usr/local/lib/python3.9/site-packages/starlette/middleware/cors.py", line 84, in call\n await self.app(scope, receive, send)\n File "/usr/local/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 79, in call\n raise exc\n File "/usr/local/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 68, in call\n await self.app(scope, receive, sender)\n File "/usr/local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in call\n raise e\n File "/usr/local/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in call\n await self.app(scope, receive, send)\n File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 718, in call\n await route.handle(scope, receive, send)\n File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 276, in handle\n await self.app(scope, receive, send)\n File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 66, in app\n response = await func(request)\n File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 237, in app\n raw_response = await run_endpoint_function(\n File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 163, in run_endpoint_function\n return await dependant.call(**values)\n File "/usr/local/lib/python3.9/site-packages/fastapi_cache/decorator.py", line 205, in inner\n result = cast(R, coder.decode_as_type(cached, type=return_type))\n File "/usr/local/lib/python3.9/site-packages/fastapi_cache/coder.py", line 82, in decode_as_type\n result = cls.decode(value)\n File "/usr/local/lib/python3.9/site-packages/fastapi_cache/coder.py", line 110, in decode\n return json.loads(value.decode(), object_hook=object_hook)\nAttributeError: 'str' object has no attribute 'decode'", "ctx": { "x-request-id": null}}

def decode(cls, value: bytes) -> Any:

Maybe, shouldn't decode whats persisted as json string ( from json response) and not bytes