用于 memory 函数执行的函数装饰器。
Memoized 数据以"pickled" 形式存储,这意味着 memoized 函数的返回值必须是 picklable 的。
memory 函数的每个调用者都会获得自己的缓存数据副本。
您可以使用 f.clear() 清除 memory 函数的缓存。
函数签名
st.experimental_memo(func=None, *, persist=None, show_spinner=True, suppress_st_warning=False, max_entries=None, ttl=None)
参数 | 说明 |
---|---|
func (callable) | 要 memory 的函数。 Streamlit 散列函数的源代码。 |
persist (str or None) | 将缓存数据持久保存到的可选位置。目前,唯一有效的值是"disk",它将持久化到本地磁盘。 |
show_spinner (boolean) | 启用微调器。默认为 True 以在缓存未命中时显示微调器。 |
suppress_st_warning (boolean) | 禁止从缓存函数中调用 Streamlit 函数的警告。 |
max_entries (int or None) | 保留在缓存中的最大条目数,或 None 用于无界缓存。 (将新条目添加到完整缓存时,将删除最旧的缓存条目。)默认值为无。 |
ttl (float or None) | 将条目保留在缓存中的最大秒数,如果缓存条目不应过期,则为 None。默认值为无。 |
示例
@st.experimental_memo
def fetch_and_clean_data(url):
# Fetch data from URL here, and then clean it up.
return data
d1 = fetch_and_clean_data(DATA_URL_1)
# Actually executes the function, since this is the first time it was
# encountered.
d2 = fetch_and_clean_data(DATA_URL_1)
# Does not execute the function. Instead, returns its previously computed
# value. This means that now the data in d1 is the same as in d2.
d3 = fetch_and_clean_data(DATA_URL_2)
# This is a different URL, so the function executes.
设置坚持参数,使用该命令如下:
@st.experimental_memo(persist="disk")
def fetch_and_clean_data(url):
# Fetch data from URL here, and then clean it up.
return data
默认情况下,memoized 函数的所有参数都必须是可散列的。任何名称以开头的参数_不会被散列。对于不可散列的参数,您可以将其用作"escape hatch":
@st.experimental_memo
def fetch_and_clean_data(_db_connection, num_rows):
# Fetch data from _db_connection here, and then clean it up.
return data
connection = make_database_connection()
d1 = fetch_and_clean_data(connection, num_rows=10)
# Actually executes the function, since this is the first time it was
# encountered.
another_connection = make_database_connection()
d2 = fetch_and_clean_data(another_connection, num_rows=10)
# Does not execute the function. Instead, returns its previously computed
# value - even though the _database_connection parameter was different
# in both calls.
memory 函数的缓存可以通过程序清除:
@st.experimental_memo
def fetch_and_clean_data(_db_connection, num_rows):
# Fetch data from _db_connection here, and then clean it up.
return data
fetch_and_clean_data.clear()
# Clear all cached entries for this function.
相关用法
- Python Streamlit st.experimental_memo.clear用法及代码示例
- Python Streamlit st.experimental_singleton.clear用法及代码示例
- Python Streamlit st.experimental_singleton用法及代码示例
- Python Streamlit st.experimental_get_query_params用法及代码示例
- Python Streamlit st.experimental_set_query_params用法及代码示例
- Python Streamlit st.experimental_rerun用法及代码示例
- Python Streamlit st.experimental_show用法及代码示例
- Python Streamlit st.expander用法及代码示例
- Python Streamlit st.exception用法及代码示例
- Python Streamlit st.empty用法及代码示例
- Python Streamlit st.error用法及代码示例
- Python Streamlit st.echo用法及代码示例
- Python Streamlit st.bokeh_chart用法及代码示例
- Python Streamlit st.caption用法及代码示例
- Python Streamlit st.text_input用法及代码示例
- Python Streamlit st.area_chart用法及代码示例
- Python Streamlit st.title用法及代码示例
- Python Streamlit st.cache用法及代码示例
- Python Streamlit st.video用法及代码示例
- Python Streamlit st.vega_lite_chart用法及代码示例
- Python Streamlit st.slider用法及代码示例
- Python Streamlit st.header用法及代码示例
- Python Streamlit st.container用法及代码示例
- Python Streamlit st.form_submit_button用法及代码示例
- Python Streamlit st.form用法及代码示例
注:本文由纯净天空筛选整理自streamlit.io大神的英文原创作品 st.experimental_memo。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。