pvanand commited on
Commit
ab0b795
·
verified ·
1 Parent(s): cefbf27

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +9 -17
main.py CHANGED
@@ -6,8 +6,8 @@ from fastapi.middleware.cors import CORSMiddleware
6
  from typing import Optional
7
  from pytrends.request import TrendReq
8
  from datetime import datetime, timedelta
9
- from fastapi_cache import caches, close_caches
10
- from fastapi_cache.backends.inmemory import InMemoryCacheBackend
11
  from fastapi_cache.decorator import cache
12
 
13
  app = FastAPI()
@@ -73,25 +73,17 @@ app.add_middleware(
73
  allow_headers=["*"],
74
  )
75
 
76
- # Set up in-memory cache
77
- cache_backend = InMemoryCacheBackend()
78
- caches.set("default", cache_backend)
79
-
80
  pytrends = TrendReq()
81
 
82
  @app.on_event("startup")
83
- async def on_startup():
84
- pass
85
-
86
- @app.on_event("shutdown")
87
- async def on_shutdown():
88
- await close_caches()
89
 
90
- @app.get("/trending_searches")
91
- @cache(expire=3600) # Cache for 1 hour
92
- async def get_trending_searches(country: str = Query("US", description="Country code (e.g., 'US' for United States)")):
93
- trending_searches = pytrends.realtime_trending_searches(pn=country)
94
- return trending_searches.to_dict(orient="records")
95
 
96
  @app.get("/", tags=["Home"])
97
  def api_home():
 
6
  from typing import Optional
7
  from pytrends.request import TrendReq
8
  from datetime import datetime, timedelta
9
+ from fastapi_cache import FastAPICache
10
+ from fastapi_cache.backends.inmemory import InMemoryBackend
11
  from fastapi_cache.decorator import cache
12
 
13
  app = FastAPI()
 
73
  allow_headers=["*"],
74
  )
75
 
 
 
 
 
76
  pytrends = TrendReq()
77
 
78
  @app.on_event("startup")
79
+ async def startup():
80
+ FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache")
 
 
 
 
81
 
82
+ @app.get("/realtime_trending_searches")
83
+ @cache(expire=3600)
84
+ async def get_realtime_trending_searches(pn: str = Query('US', description="Country code for trending searches")):
85
+ trending_searches = pytrends.realtime_trending_searches(pn=pn)
86
+ return trending_searches.to_dict(orient='records')
87
 
88
  @app.get("/", tags=["Home"])
89
  def api_home():