Open ramprasad2018 opened 1 year ago
I have a similar problem, nothing is being saved using the @cache decorator. The configurations seem to be in accordance with the documentation, the initialization as well and I can normally manipulate redis via redis_cli.
I created an API to generate tokens that is cached but it always generates a new one with each request instead of using what is in the cache. Nothing is sent to Redis.
Hi Team, I have followed the steps given in https://pypi.org/project/fastapi-redis-cache/.
I have added the following methods in main.py and observation is as follows: (Please note I am using the Tryout option of FastAPI docs to test the endpoint). Please help to resolve this issue. I am using python 3.11. Observation:
INFO: 127.0.0.1:62749 - "GET /dynamic_data HTTP/1.1" 200 OK INFO:fastapi_redis_cache.client: 05/18/2023 06:53:13 PM | KEY_ADDED_TO_CACHE: key=myapi-cache:main.get_dynamic_data()
Response Header:
cache-control: max-age=30 content-length: 72 content-type: application/json date: Thu,18 May 2023 10:53:13 GMT etag: W/2847227004069749289 expires: Thu,18 May 2023 10:53:43 GMT server: uvicorn x-myapi-cache: Miss
Note:-
When the end point is being executed for second time, the status of x-myapi-cache should be Hit and as follows. But it is not happening
x-myapi-cache: Hit
{ "message": "this data should only be cached temporarily", "success": true }
Method:
Will be cached for thirty seconds
@app.get("/dynamic_data") @cache(expire=30) def get_dynamic_data(request: Request, response: Response): return {"success": True, "message": "this data should only be cached temporarily"}
mani.py
import os
from fastapi import FastAPI, Request, Response from fastapi_redis_cache import FastApiRedisCache, cache
LOCAL_REDIS_URL = "redis://143.42.77.29:6379"
app = FastAPI(title="FastAPI Redis Cache Example")
@app.on_event("startup") def startup(): redis_cache = FastApiRedisCache() redis_cache.init( host_url=os.environ.get("REDIS_URL", LOCAL_REDIS_URL), prefix="myapi-cache", response_header="X-MyAPI-Cache", ignore_arg_types=[Request, Response] )
Will be cached for thirty seconds
@app.get("/dynamic_data") @cache(expire=30) def get_dynamic_data(request: Request, response: Response): return {"success": True, "message": "this data should only be cached temporarily"}