Skip to content

Commit

Permalink
Add caching decorator to safeapi (TTL 2s)
Browse files Browse the repository at this point in the history
  • Loading branch information
eudoxos committed Oct 19, 2024
1 parent e97b2b5 commit c6d8b82
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
21 changes: 20 additions & 1 deletion mupifDB/api/safeapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@
import mupif as mp
import os

app = fastapi.FastAPI(openapi_tags=[{'name':'Stats'}])
### BEGIN CACHING (should be ported to master later)
from fastapi_cache import FastAPICache
from fastapi_cache.decorator import cache
from fastapi_cache.backends.inmemory import InMemoryBackend
from contextlib import asynccontextmanager
from collections.abc import AsyncIterator
@asynccontextmanager
async def lifespan(_: fastapi.FastAPI) -> AsyncIterator[None]:
FastAPICache.init(InMemoryBackend())
yield

app = fastapi.FastAPI(openapi_tags=[{'name':'Stats'}],lifespan=lifespan)
### END CACHING



mongoClient = pymongo.MongoClient("mongodb://localhost:27017")



@app.get("/status2/", tags=["Stats"])
@cache(expire=2)
def get_status2():
ns = None
try:
Expand Down Expand Up @@ -38,20 +53,24 @@ def get_status2():


@app.get("/scheduler-status2/", tags=["Stats"])
@cache(expire=2)
def get_scheduler_status2():
ns = mp.pyroutil.connectNameserver();
return mp.monitor.schedulerInfo(ns)

@app.get("/ns-status2/", tags=["Stats"])
@cache(expire=2)
def get_ns_status2():
ns = mp.pyroutil.connectNameserver();
return mp.monitor.nsInfo(ns)

@app.get("/vpn-status2/", tags=["Stats"])
@cache(expire=2)
def get_vpn_status2():
return mp.monitor.vpnInfo(hidePriv=False)

@app.get("/jobmans-status2/", tags=["Stats"])
@cache(expire=2)
def get_jobmans_status2():
ns = mp.pyroutil.connectNameserver();
return mp.monitor.jobmanInfo(ns)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Werkzeug==2.2.2
psutil
fastapi
uvicorn[standard]
fastapi-cache2
python-multipart
networkx
astropy
Expand Down

0 comments on commit c6d8b82

Please sign in to comment.