dict cache
Browse files
main.py
CHANGED
@@ -12,6 +12,7 @@ from youtube_transcript_api import YouTubeTranscriptApi
|
|
12 |
from cachetools import TTLCache
|
13 |
import asyncio
|
14 |
import hashlib
|
|
|
15 |
|
16 |
|
17 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
@@ -218,22 +219,62 @@ def extract_transcript(youtube_link):
|
|
218 |
|
219 |
|
220 |
|
221 |
-
cache = TTLCache(maxsize=100, ttl=400)
|
222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
223 |
@app.get("/summary")
|
224 |
async def test(url:str):
|
225 |
try:
|
226 |
cache_key = hashlib.sha256(url.encode()).hexdigest()
|
227 |
-
|
228 |
-
if
|
229 |
-
|
230 |
-
|
231 |
-
|
|
|
|
|
232 |
status,content=scrweb(url)
|
233 |
if status==200:
|
234 |
response=genetate_gemini_content(prompt6,content)
|
235 |
# print(response)
|
236 |
-
cache[cache_key] = response
|
237 |
return response
|
238 |
else:
|
239 |
return status
|
@@ -241,7 +282,6 @@ async def test(url:str):
|
|
241 |
except:
|
242 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
243 |
|
244 |
-
|
245 |
|
246 |
|
247 |
@app.get("/ut")
|
|
|
12 |
from cachetools import TTLCache
|
13 |
import asyncio
|
14 |
import hashlib
|
15 |
+
import time
|
16 |
|
17 |
|
18 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
|
|
219 |
|
220 |
|
221 |
|
222 |
+
# cache = TTLCache(maxsize=100, ttl=400)
|
223 |
|
224 |
+
# @app.get("/summary")
|
225 |
+
# async def test(url:str):
|
226 |
+
# try:
|
227 |
+
# cache_key = hashlib.sha256(url.encode()).hexdigest()
|
228 |
+
# cached_response = cache.get(cache_key)
|
229 |
+
# if cached_response:
|
230 |
+
# print("hii")
|
231 |
+
# return cached_response
|
232 |
+
|
233 |
+
# status,content=scrweb(url)
|
234 |
+
# if status==200:
|
235 |
+
# response=genetate_gemini_content(prompt6,content)
|
236 |
+
# # print(response)
|
237 |
+
# cache[cache_key] = response
|
238 |
+
# return response
|
239 |
+
# else:
|
240 |
+
# return status
|
241 |
+
|
242 |
+
# except:
|
243 |
+
# raise HTTPException(status_code=500, detail="Internal Server Error")
|
244 |
+
|
245 |
+
|
246 |
+
cache={}
|
247 |
+
|
248 |
+
@app.on_event("startup")
|
249 |
+
async def startup_event():
|
250 |
+
# Start a background task to clean up expired entries from the cache
|
251 |
+
asyncio.create_task(clean_up_cache())
|
252 |
+
|
253 |
+
async def clean_up_cache():
|
254 |
+
while True:
|
255 |
+
await asyncio.sleep(1000) # Check cache every 60 seconds
|
256 |
+
|
257 |
+
current_time = time.time()
|
258 |
+
expired_keys = [key for key, value in cache.items() if current_time - value["timestamp"] > 300]
|
259 |
+
for key in expired_keys:
|
260 |
+
del cache[key]
|
261 |
+
|
262 |
@app.get("/summary")
|
263 |
async def test(url:str):
|
264 |
try:
|
265 |
cache_key = hashlib.sha256(url.encode()).hexdigest()
|
266 |
+
|
267 |
+
# Check if the response is already cached
|
268 |
+
if cache_key in cache:
|
269 |
+
cached_response = cache[cache_key]
|
270 |
+
if time.time() - cached_response["timestamp"] <= 300: # Check if cached data is still valid (5 minutes)
|
271 |
+
return cached_response["data"]
|
272 |
+
|
273 |
status,content=scrweb(url)
|
274 |
if status==200:
|
275 |
response=genetate_gemini_content(prompt6,content)
|
276 |
# print(response)
|
277 |
+
cache[cache_key] = {"data": response, "timestamp": time.time()}
|
278 |
return response
|
279 |
else:
|
280 |
return status
|
|
|
282 |
except:
|
283 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
284 |
|
|
|
285 |
|
286 |
|
287 |
@app.get("/ut")
|