File tree Expand file tree Collapse file tree 2 files changed +3
-32
lines changed
Expand file tree Collapse file tree 2 files changed +3
-32
lines changed Original file line number Diff line number Diff line change 1- import time
21from typing import Any
32from flask import Blueprint
43from kernelboard .lib .db import get_db_connection
98
109leaderboard_bp = Blueprint ("leaderboard_bp" , __name__ , url_prefix = "/leaderboard" )
1110
12- # Simple in-memory cache keyed by leaderboard_id
13- _cache : dict [int , dict ] = {}
14- CACHE_TTL_SECONDS = 60
15-
1611
1712@leaderboard_bp .route ("/<int:leaderboard_id>" , methods = ["GET" ])
1813def leaderboard (leaderboard_id : int ):
19- now = time .time ()
20- cached = _cache .get (leaderboard_id )
21- if cached is not None and (now - cached ["timestamp" ]) < CACHE_TTL_SECONDS :
22- return http_success (cached ["data" ])
23-
2414 conn = get_db_connection ()
2515 query = _get_query ()
2616 with conn .cursor () as cur :
@@ -37,9 +27,6 @@ def leaderboard(leaderboard_id: int):
3727 data = result [0 ]
3828
3929 res = to_api_leaderboard_item (data )
40-
41- _cache [leaderboard_id ] = {"data" : res , "timestamp" : now }
42-
4330 return http_success (res )
4431
4532
Original file line number Diff line number Diff line change 1- import time
2-
31from flask import Blueprint
42from datetime import datetime , timezone
53from kernelboard .lib .db import get_db_connection
108 "leaderboard_summaries_bp" , __name__ , url_prefix = "/leaderboard-summaries"
119)
1210
13- # Simple in-memory cache
14- _cache = {
15- "data" : None ,
16- "timestamp" : 0 ,
17- }
18- CACHE_TTL_SECONDS = 60
19-
2011
2112@leaderboard_summaries_bp .route ("" , methods = ["GET" ])
2213def index ():
@@ -37,10 +28,6 @@ def index():
3728 # ],
3829 # }
3930
40- now = time .time ()
41- if _cache ["data" ] is not None and (now - _cache ["timestamp" ]) < CACHE_TTL_SECONDS :
42- return http_success (_cache ["data" ])
43-
4431 conn = get_db_connection ()
4532 query = _get_query ()
4633 with conn .cursor () as cur :
@@ -51,12 +38,9 @@ def index():
5138 if lb ["gpu_types" ] is None :
5239 lb ["gpu_types" ] = []
5340
54- result = {"leaderboards" : leaderboards , "now" : datetime .now (timezone .utc )}
55-
56- _cache ["data" ] = result
57- _cache ["timestamp" ] = now
58-
59- return http_success (result )
41+ return http_success (
42+ {"leaderboards" : leaderboards , "now" : datetime .now (timezone .utc )}
43+ )
6044
6145
6246def _get_query ():
You can’t perform that action at this time.
0 commit comments