⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions kernelboard/api/leaderboard.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import time
from typing import Any
from flask import Blueprint
from kernelboard.lib.db import get_db_connection
Expand All @@ -9,18 +8,9 @@

leaderboard_bp = Blueprint("leaderboard_bp", __name__, url_prefix="/leaderboard")

# Simple in-memory cache keyed by leaderboard_id
_cache: dict[int, dict] = {}
CACHE_TTL_SECONDS = 60


@leaderboard_bp.route("/<int:leaderboard_id>", methods=["GET"])
def leaderboard(leaderboard_id: int):
now = time.time()
cached = _cache.get(leaderboard_id)
if cached is not None and (now - cached["timestamp"]) < CACHE_TTL_SECONDS:
return http_success(cached["data"])

conn = get_db_connection()
query = _get_query()
with conn.cursor() as cur:
Expand All @@ -37,9 +27,6 @@ def leaderboard(leaderboard_id: int):
data = result[0]

res = to_api_leaderboard_item(data)

_cache[leaderboard_id] = {"data": res, "timestamp": now}

return http_success(res)


Expand Down
22 changes: 3 additions & 19 deletions kernelboard/api/leaderboard_summaries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import time

from flask import Blueprint
from datetime import datetime, timezone
from kernelboard.lib.db import get_db_connection
Expand All @@ -10,13 +8,6 @@
"leaderboard_summaries_bp", __name__, url_prefix="/leaderboard-summaries"
)

# Simple in-memory cache
_cache = {
"data": None,
"timestamp": 0,
}
CACHE_TTL_SECONDS = 60


@leaderboard_summaries_bp.route("", methods=["GET"])
def index():
Expand All @@ -37,10 +28,6 @@ def index():
# ],
# }

now = time.time()
if _cache["data"] is not None and (now - _cache["timestamp"]) < CACHE_TTL_SECONDS:
return http_success(_cache["data"])

conn = get_db_connection()
query = _get_query()
with conn.cursor() as cur:
Expand All @@ -51,12 +38,9 @@ def index():
if lb["gpu_types"] is None:
lb["gpu_types"] = []

result = {"leaderboards": leaderboards, "now": datetime.now(timezone.utc)}

_cache["data"] = result
_cache["timestamp"] = now

return http_success(result)
return http_success(
{"leaderboards": leaderboards, "now": datetime.now(timezone.utc)}
)


def _get_query():
Expand Down
Loading