2 1 Caching Functions Explained
Key Concepts
- Caching: Storing the results of expensive function calls to avoid redundant computations.
- st.cache: A Streamlit decorator to cache function outputs.
- Cache Expiry: Mechanisms to invalidate cached data after a certain period or condition.
- Cache Keys: Unique identifiers used to store and retrieve cached data.
Caching
Caching in Streamlit involves storing the results of expensive function calls so that they can be reused without recomputation. This improves performance by reducing the time spent on redundant calculations.
st.cache
st.cache
is a decorator provided by Streamlit to cache the output of a function. When a cached function is called with the same arguments, Streamlit returns the cached result instead of re-executing the function.
Example:
import streamlit as st @st.cache def expensive_computation(a, b): return a * b result = expensive_computation(10, 20) st.write(result)
Cache Expiry
Cache expiry refers to the mechanism that invalidates cached data after a certain period or condition. This ensures that the cached data remains up-to-date and relevant.
Example:
import streamlit as st import time @st.cache(ttl=60) def get_data(): time.sleep(5) # Simulate an expensive operation return "Data fetched at " + time.ctime() data = get_data() st.write(data)
Cache Keys
Cache keys are unique identifiers used to store and retrieve cached data. Streamlit automatically generates cache keys based on the function's arguments, but you can also specify custom keys for more control.
Example:
import streamlit as st @st.cache(hash_funcs={list: id}) def process_data(data): return [item * 2 for item in data] data = [1, 2, 3, 4] processed_data = process_data(data) st.write(processed_data)
Analogies
Think of caching as a memo pad where you jot down the results of complex calculations. The next time you need the same result, you can simply look it up on the memo pad instead of recalculating it. The memo pad is like the cache, and the memo entries are like the cached results. Cache expiry is like setting an expiration date on the memo entries to ensure they are still valid.
By mastering caching functions in Streamlit, you can significantly improve the performance of your applications by avoiding redundant computations and ensuring that your data remains up-to-date.