a-luna / fastapi-redis-cache

A simple and robust caching solution for FastAPI that interprets request header values and creates proper response header values (powered by Redis)
MIT License
152 stars 24 forks source link

Can't Cache List of serialized data #72

Open Exganza opened 1 year ago

Exganza commented 1 year ago

Hi thank you for this great module, i just have an issue that i cant cache a list of serialized dict data, i am using tortoise ORM with Pydantic Model .. my query be like `

@app.get("/forcasts")
@cache(expire=120)
async def forcasts():
     today = datetime.today().strftime("%Y-%m-%d")
     return await Forcast_pydantic.from_queryset(Forcast.filter(date=today))

`

and i get this : INFO:fastapi_redis_cache.client: 12/12/2022 07:13:18 PM | FAILED_TO_CACHE_KEY: Object of type <class 'list'> is not JSON-serializable: key=myapi-cache:main.forcasts()

my response that not coached correctly [ { "id": 1, "date": "2022-12-12", "company_ar": "الرياض", "company_en": "RIBL", "code": 1010, "market_cap": 931500, "negative_positive": "negative", "duration": 5, "profit": null, "capital_protection": "-10.2601156069375" }, { "id": 2, "date": "2022-12-12", "company_ar": "الجزيرة", "company_en": "BJAZ", "code": 1020, "market_cap": 15957, "negative_positive": "negative", "duration": 7, "profit": null, "capital_protection": "-15.9395248380128" }]

can you help me with that ? Thanks

anthonymckale-6point6 commented 1 year ago

believe this pull request fixes the issue

using it locally in a forked repo to work for me

https://github.com/a-luna/fastapi-redis-cache/pull/65/files