Skip to content

Commit 94b952f

Browse files
author
Daniel Precioso, PhD
committed
Refactor app.py for improved structure and readability
1 parent 0259ec1 commit 94b952f

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

app.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from functools import lru_cache
23

34
import geopandas as gpd
45
import pandas as pd
@@ -19,17 +20,14 @@
1920
"AT": os.path.join(BASE_DIR, "data", "austria.csv"),
2021
}
2122

22-
# Simple in-memory cache for CSVs to avoid re-reading on each request
23-
CSV_CACHE: dict[str, pd.DataFrame] = {}
24-
2523

24+
@lru_cache(maxsize=4)
2625
def _get_df(map_id: str) -> pd.DataFrame:
27-
"""Return cached DataFrame for a map_id, loading from disk if needed."""
26+
"""Return cached DataFrame for a map_id, loading from disk if needed.
27+
Use an LRU cache for CSVs to avoid unbounded memory usage"""
2828
map_id = map_id.upper()
2929
path = CSV_MAP[map_id]
30-
if map_id not in CSV_CACHE:
31-
CSV_CACHE[map_id] = pd.read_csv(path).round(1)
32-
return CSV_CACHE[map_id]
30+
return pd.read_csv(path).round(1)
3331

3432

3533
META_MAP = {

0 commit comments

Comments
 (0)