Only cache IF conditional
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Start by tracing the st.cache_resource decorator and its existing cache behavior; the issue does not name specific implementation files or tests. Define how a cache_if callback should control storing the decorated function's return value, then add coverage for successful and None results.
Written by the indexing model from the issue text.
Description
Checklist
- I have searched the existing issues for similar feature requests.
- I added a descriptive title and summary to this issue.
Summary
It would be useful if there is more control over the st.cache_* decorators, such as only caching the the result based on a given condition.
Why?
I have a resource cache that loads a transformers deep learning model. This week, the HF services went down, which meant that the model could not be loaded and yielded None. So now the cache for that model has been set to None and subsequent users cannot run the app because the model is not defined.
How?
Perhaps by adding an argument cache_if, that is passed to the constructor which must be a function that returns a bool. If True, the cacher will cache, otherwise it will not. The argument that will be passed to the defined cache_if-function is the return value of the decorated function.
def model_downloaded(model) -> bool:
if model is None:
return False
else:
return True
@st.cache_resource(show_spinner=False, max_entries=6, cache_if=model_downloaded)
def get_model():
model = model_download()
return model
Additional Context
No response
- Dominant language
- Python
- Stars
- 45.8k
- Forks
- 4.4k
- Avg merge
- 21h 22m
- Merged PRs (30d)
- 316
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from streamlit/streamlit
-
area:backend area:network feature:embedding feature:runtime priority:P3 status:confirmed type:bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
area:frontend area:security feature:st.file_uploader status:unlikely type:enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
area:backend area:security feature:cache feature:cache-hash-func priority:P4 status:confirmed type:bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
area:mobile feature:st.camera_input papercut type:enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
area:ai type:enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
All issues in streamlit/streamlit
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
bancolombia/sentinel#23 ·
-
test md OpenCI
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100