Skip to content

Commit c6dac52

Browse files
Merge pull request #68 from fractal-analytics-platform/improve_caching
add ttl and num entries to config
2 parents 767f70e + 641c9c4 commit c6dac52

File tree

3 files changed

+54
-23
lines changed

3 files changed

+54
-23
lines changed

src/fractal_feature_explorer/config.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import streamlit as st
55
from typing import Literal
66
from typing import Annotated
7-
7+
from datetime import timedelta
88
from streamlit.logger import get_logger
99
import os
1010

@@ -22,17 +22,23 @@ def remove_trailing_slash(value: str) -> str:
2222
return value.rstrip("/")
2323

2424

25-
class LocalConfig(BaseModel):
25+
class BaseConfig(BaseModel):
2626
model_config = ConfigDict(extra="forbid")
27-
deployment_type: Literal["local"]
27+
deployment_type: Literal["local", "production"]
28+
allow_local_paths: bool
29+
cache_ttl: float | timedelta | str | None = None
30+
cache_max_entries: int | None = None
31+
32+
33+
class LocalConfig(BaseConfig):
34+
deployment_type: Literal["local"] # type: ignore override type
2835
fractal_data_urls: list[Annotated[str, AfterValidator(remove_trailing_slash)]]
2936
allow_local_paths: bool = True
3037

3138

32-
class ProductionConfig(BaseModel):
33-
model_config = ConfigDict(extra="forbid")
34-
deployment_type: Literal["production"]
35-
allow_local_paths: Literal[False] = False
39+
class ProductionConfig(BaseConfig):
40+
deployment_type: Literal["production"] # type: ignore override type
41+
allow_local_paths: Literal[False] = False # type: ignore override type
3642
fractal_data_url: Annotated[str, AfterValidator(remove_trailing_slash)]
3743
fractal_backend_url: Annotated[str, AfterValidator(remove_trailing_slash)]
3844
fractal_frontend_url: Annotated[str, AfterValidator(remove_trailing_slash)]
@@ -103,3 +109,23 @@ def get_config() -> LocalConfig | ProductionConfig:
103109
logger.debug(f"{config=}")
104110
logger.info(f"Streamlit version: {st.__version__}")
105111
return config
112+
113+
114+
def st_cache_data_wrapper(func):
115+
"""
116+
Wrapper around st.cache_data to set a default ttl.
117+
"""
118+
config = get_config()
119+
return st.cache_data(ttl=config.cache_ttl, max_entries=config.cache_max_entries)(
120+
func
121+
)
122+
123+
124+
def st_cache_resource_wrapper(func):
125+
"""
126+
Wrapper around st.cache_resource to set a default ttl.
127+
"""
128+
config = get_config()
129+
return st.cache_resource(
130+
ttl=config.cache_ttl, max_entries=config.cache_max_entries
131+
)(func)

src/fractal_feature_explorer/pages/setup_page/_tables_io.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
plate_name_from_url,
1818
extras_from_url,
1919
)
20+
from fractal_feature_explorer.config import st_cache_data_wrapper
2021

2122
from streamlit.logger import get_logger
2223

@@ -93,7 +94,7 @@ def list_images_tables(
9394
# ====================================================================
9495

9596

96-
@st.cache_data
97+
@st_cache_data_wrapper
9798
def _load_single_plate_condition_table(
9899
url: str,
99100
table_name: str,
@@ -122,7 +123,7 @@ def _load_single_plate_condition_table(
122123
return table_df
123124

124125

125-
@st.cache_data
126+
@st_cache_data_wrapper
126127
def _collect_condition_table_from_plates_cached(
127128
list_urls: list[str],
128129
table_name: str,
@@ -139,7 +140,7 @@ def _collect_condition_table_from_plates_cached(
139140
return condition_table
140141

141142

142-
@st.cache_data
143+
@st_cache_data_wrapper
143144
def _collect_condition_table_from_images_cached(
144145
list_urls: list[str],
145146
table_name: str,
@@ -214,7 +215,7 @@ def collect_condition_table_from_images(
214215
# ====================================================================
215216

216217

217-
@st.cache_data
218+
@st_cache_data_wrapper
218219
def _load_plate_feature_table(
219220
url: str,
220221
table_name: str,
@@ -249,7 +250,7 @@ def _load_plate_feature_table(
249250
return table_df
250251

251252

252-
@st.cache_data
253+
@st_cache_data_wrapper
253254
def _collect_feature_table_from_plates_cached(
254255
list_urls: list[str],
255256
table_name: str,
@@ -264,7 +265,7 @@ def _collect_feature_table_from_plates_cached(
264265
return feature_table
265266

266267

267-
@st.cache_data
268+
@st_cache_data_wrapper
268269
def _collect_feature_table_from_images_cached(
269270
list_urls: list[str],
270271
table_name: str,

src/fractal_feature_explorer/utils/ngio_io_caches.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
from fractal_feature_explorer.utils import get_fractal_token
2727
from streamlit.logger import get_logger
2828

29-
from fractal_feature_explorer.config import get_config
29+
from fractal_feature_explorer.config import (
30+
get_config,
31+
st_cache_data_wrapper,
32+
st_cache_resource_wrapper,
33+
)
3034

3135
logger = get_logger(__name__)
3236

@@ -78,7 +82,7 @@ def is_http_url(url: str) -> bool:
7882
return url.startswith("http://") or url.startswith("https://")
7983

8084

81-
@st.cache_data
85+
@st_cache_resource_wrapper
8286
def _get_http_store(
8387
url: str, fractal_token: str | None = None
8488
) -> fsspec.mapping.FSMap | None:
@@ -140,7 +144,7 @@ def get_and_validate_store(url: str) -> fsspec.mapping.FSMap | str | None:
140144
return _get_and_validate_store(url, fractal_token=fractal_token)
141145

142146

143-
@st.cache_resource
147+
@st_cache_resource_wrapper
144148
def _get_ome_zarr_plate(url: str, fractal_token: str | None = None) -> OmeZarrPlate:
145149
store = _get_and_validate_store(url, fractal_token=fractal_token)
146150
if store is None:
@@ -154,7 +158,7 @@ def get_ome_zarr_plate(url: str) -> OmeZarrPlate:
154158
return _get_ome_zarr_plate(url, fractal_token=fractal_token)
155159

156160

157-
@st.cache_resource
161+
@st_cache_resource_wrapper
158162
def _get_ome_zarr_image_container(
159163
url: str, fractal_token: str | None = None
160164
) -> OmeZarrContainer:
@@ -176,7 +180,7 @@ def _get_ome_zarr_container_in_plate(
176180
return images[path]
177181

178182

179-
@st.cache_resource
183+
@st_cache_resource_wrapper
180184
def _get_ome_zarr_container(
181185
url: str,
182186
fractal_token: str | None = None,
@@ -198,7 +202,7 @@ def get_ome_zarr_container(
198202
return _get_ome_zarr_image_container(url, fractal_token=fractal_token)
199203

200204

201-
@st.cache_data
205+
@st_cache_data_wrapper
202206
def _list_image_tables(
203207
urls: list[str],
204208
fractal_token: str | None = None,
@@ -212,7 +216,7 @@ def _list_image_tables(
212216
return image_list
213217

214218

215-
@st.cache_data
219+
@st_cache_data_wrapper
216220
def list_image_tables(
217221
urls: list[str],
218222
fractal_token: str | None = None,
@@ -245,7 +249,7 @@ def roi_to_slice_kwargs(
245249
return raster_roi # type: ignore
246250

247251

248-
@st.cache_resource
252+
@st_cache_resource_wrapper
249253
def _get_masking_roi(
250254
image_url: str,
251255
ref_label: str,
@@ -268,7 +272,7 @@ def _get_masking_roi(
268272
return masking_roi
269273

270274

271-
@st.cache_data
275+
@st_cache_data_wrapper
272276
def _get_image_array(
273277
image_url: str,
274278
ref_label: str,
@@ -309,7 +313,7 @@ def _get_image_array(
309313
return image_array
310314

311315

312-
@st.cache_data
316+
@st_cache_data_wrapper
313317
def _get_label_array(
314318
image_url: str,
315319
ref_label: str,

0 commit comments

Comments
 (0)