forked from spacebar/SplatNet-Backend
feat: better cache system
This commit is contained in:
parent
ae1173e268
commit
c0c40e9350
4 changed files with 15 additions and 3 deletions
7
main.py
7
main.py
|
|
@ -3,7 +3,9 @@ from routes import sessionid_check, sso
|
|||
from config import settings
|
||||
import uvicorn
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from database import init_db, SessionLocal, PlayerRank # Added for reset logic
|
||||
from fastapi_cache import FastAPICache
|
||||
from fastapi_cache.backends.inmemory import InMemoryBackend
|
||||
from database import init_db, SessionLocal, PlayerRank
|
||||
from routes import logout
|
||||
from routes import boss
|
||||
#yes meow meow import me please... i want to be embedded into my code... :pleading_face:
|
||||
|
|
@ -60,6 +62,8 @@ async def weekly_rank_reset_loop():
|
|||
db.execute(delete(PlayerRank))
|
||||
db.commit()
|
||||
print("Ranking table cleared successfully.")
|
||||
await FastAPICache.clear()
|
||||
print("Cache cleared")
|
||||
|
||||
await asyncio.sleep(60)
|
||||
|
||||
|
|
@ -71,6 +75,7 @@ async def weekly_rank_reset_loop():
|
|||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
FastAPICache.init(InMemoryBackend())
|
||||
global judd_process
|
||||
|
||||
print("starting judd server")
|
||||
|
|
|
|||
|
|
@ -31,4 +31,5 @@ typing-inspection==0.4.2
|
|||
typing_extensions==4.15.0
|
||||
urllib3==2.6.3
|
||||
uvicorn==0.41.0
|
||||
tweepy==4.16.0
|
||||
tweepy==4.16.0
|
||||
fastapi-cache2==0.2.2
|
||||
|
|
@ -2,6 +2,9 @@ from fastapi import APIRouter, Depends
|
|||
from sqlalchemy.orm import Session as DBSession
|
||||
from sqlalchemy import desc
|
||||
from database import SessionLocal, PlayerRank, EquipmentLast
|
||||
from fastapi_cache import FastAPICache
|
||||
from fastapi_cache.backends.inmemory import InMemoryBackend
|
||||
from fastapi_cache.decorator import cache
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
|
@ -13,6 +16,7 @@ def get_db():
|
|||
db.close()
|
||||
|
||||
@router.get("/leaderboard")
|
||||
@cache(expire=60)
|
||||
async def get_leaderboard(db: DBSession = Depends(get_db)):
|
||||
try:
|
||||
top_100_query = (
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ from fastapi import APIRouter, Request, Response
|
|||
import yaml
|
||||
import json
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from fastapi_cache.decorator import cache
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
|
@ -33,6 +34,7 @@ RULE_NAMES = {
|
|||
}
|
||||
|
||||
@router.api_route('/boss', methods=['GET', 'POST'])
|
||||
@cache(expire=3660)
|
||||
async def boss_rotation(request: Request):
|
||||
try:
|
||||
with open("boss.yaml", "r") as f:
|
||||
|
|
@ -78,7 +80,7 @@ async def boss_rotation(request: Request):
|
|||
}
|
||||
}
|
||||
|
||||
return Response(content=json.dumps(response_data), media_type="application/json")
|
||||
return response_data
|
||||
|
||||
except Exception as e:
|
||||
return Response(content=json.dumps({"error": str(e)}), status_code=500, media_type="application/json")
|
||||
Loading…
Reference in a new issue