Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@cache Decorator Not Working #77

Open
ramprasad2018 opened this issue May 18, 2023 · 0 comments
Open

@cache Decorator Not Working #77

ramprasad2018 opened this issue May 18, 2023 · 0 comments

Comments

@ramprasad2018
Copy link

ramprasad2018 commented May 18, 2023

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"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant