Sunrusojsis commited on
Commit
d471223
1 Parent(s): 36dad4f

Implemented caching

Browse files
Files changed (1) hide show
  1. main.py +10 -3
main.py CHANGED
@@ -2,16 +2,23 @@ from fastapi import FastAPI, Request, HTTPException
2
  from fastapi.responses import Response
3
  import aiohttp
4
  from urllib.parse import urlparse
 
5
 
6
  app = FastAPI()
7
 
8
  # Initialize the aiohttp ClientSession
9
- http_client = aiohttp.ClientSession()
10
 
11
  @app.on_event("startup")
12
  async def startup_event():
 
 
 
 
 
 
13
  global http_client
14
- http_client = aiohttp.ClientSession()
15
 
16
  @app.on_event("shutdown")
17
  async def shutdown_event():
@@ -45,7 +52,7 @@ async def proxy(request: Request, url: str):
45
  headers=headers,
46
  data=body,
47
  allow_redirects=False,
48
- ssl=False
49
  ) as upstream_response:
50
  # Prepare the response headers, excluding some that might cause issues
51
  response_headers = {k: v for k, v in upstream_response.headers.items()
 
2
  from fastapi.responses import Response
3
  import aiohttp
4
  from urllib.parse import urlparse
5
+ from aiohttp_client_cache import CachedSession,CacheBackend
6
 
7
  app = FastAPI()
8
 
9
  # Initialize the aiohttp ClientSession
10
+ #http_client = aiohttp.CachedSession()
11
 
12
  @app.on_event("startup")
13
  async def startup_event():
14
+ cache = CacheBackend(
15
+ allowed_methods=('GET', 'POST'),
16
+ allowed_codes=(200,503,502),
17
+ include_headers=True,
18
+ expire_after = 50
19
+ )
20
  global http_client
21
+ http_client = CachedSession(cache=cache)
22
 
23
  @app.on_event("shutdown")
24
  async def shutdown_event():
 
52
  headers=headers,
53
  data=body,
54
  allow_redirects=False,
55
+ ssl = False
56
  ) as upstream_response:
57
  # Prepare the response headers, excluding some that might cause issues
58
  response_headers = {k: v for k, v in upstream_response.headers.items()