From 97daa656335351e123996006c850276c32779ad4 Mon Sep 17 00:00:00 2001 From: Arnaud Date: Mon, 11 Oct 2021 10:38:04 +0200 Subject: [PATCH 1/3] Add support for experimental caching objects --- app.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index b6b80c6..411656e 100644 --- a/app.py +++ b/app.py @@ -274,18 +274,31 @@ def cs_body(): col3.subheader('Optimize performance') col3.code(''' -@st.cache ->>> @st.cache +@st.experimental_memo +# Function decorator to memoize function executions +>>> @st.experimental_memo ... def fetch_and_clean_data(url): -... # Mutate data at url +... # Fetch data from URL, clean it up. ... return data >>> # Executes d1 as first time ->>> d1 = fetch_and_clean_data(ref1) +>>> d1 = fetch_and_clean_data(DATA_URL_1) >>> # Does not execute d1; returns cached value, d1==d2 ->>> d2 = fetch_and_clean_data(ref1) +>>> d2 = fetch_and_clean_data(DATA_URL_1) >>> # Different arg, so function d1 executes ->>> d3 = fetch_and_clean_data(ref2) - +>>> d3 = fetch_and_clean_data(DATA_URL_2) +@st.experimental_singleton +# Each singleton is shared across all users connected +>>> @st.experimental_singleton +... def get_database_session(url): +... # Create a database session object +... return session +>>> # Executes s1 as first time +>>> s1 = get_database_session(DATA_URL_1) +>>> # Does not execute; returns cached value, s1==s2 +>>> s2 = get_database_session(DATA_URL_1) +>>> # Different arg, so function d1 executes +>>> s3 = get_database_session(DATA_URL_2) +>>> # This is a different URL, so the function executes. ''') col3.subheader('Other key parts of the API') From 776a4391968a1fe05ba9c535dbc882a269fc19ce Mon Sep 17 00:00:00 2001 From: Arnaud Date: Mon, 11 Oct 2021 10:40:51 +0200 Subject: [PATCH 2/3] Update app.py --- app.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 411656e..2f849d3 100644 --- a/app.py +++ b/app.py @@ -293,7 +293,8 @@ def cs_body(): ... # Create a database session object ... return session >>> # Executes s1 as first time ->>> s1 = get_database_session(DATA_URL_1) +>>> s1 = get_database_session(SESSION_URL_1) + >>> # Does not execute; returns cached value, s1==s2 >>> s2 = get_database_session(DATA_URL_1) >>> # Different arg, so function d1 executes From dea0e8169e4d02184b7a6097341f14ac4f7c5b05 Mon Sep 17 00:00:00 2001 From: Arnaud Date: Mon, 11 Oct 2021 10:41:25 +0200 Subject: [PATCH 3/3] Update app.py --- app.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 2f849d3..9261aa2 100644 --- a/app.py +++ b/app.py @@ -294,11 +294,10 @@ def cs_body(): ... return session >>> # Executes s1 as first time >>> s1 = get_database_session(SESSION_URL_1) - >>> # Does not execute; returns cached value, s1==s2 ->>> s2 = get_database_session(DATA_URL_1) +>>> s2 = get_database_session(SESSION_URL_1) >>> # Different arg, so function d1 executes ->>> s3 = get_database_session(DATA_URL_2) +>>> s3 = get_database_session(SESSION_URL_2) >>> # This is a different URL, so the function executes. ''')