1
1
2
- *** Cache data objects***
2
+ ## Cache data objects***
3
3
4
4
** Define a cached function:**
5
5
@@ -30,7 +30,7 @@ clears values from all cached functions, either in-memory or on-disk.
30
30
31
31
These snippets demonstrate how Streamlit allows you to cache expensive computations or data retrievals to improve performance.
32
32
33
- *** Cache global resources***
33
+ ## Cache global resources
34
34
35
35
E.g. TensorFlow session, database connection, etc.
36
36
@@ -53,4 +53,26 @@ Create and return a non-data object
53
53
foo.clear()
54
54
#Clear all global resources from cache
55
55
56
- st.cache_resource.clear()
56
+ st.cache_resource.clear()
57
+
58
+ ## Deprecated caching
59
+
60
+ These Streamlit snippets showcase caching with the ` @st.cache ` decorator:
61
+
62
+ ** Define a cached function:**
63
+
64
+ `@st.cache
65
+ decorator marks the function ` foo() ` for caching.
66
+
67
+ Inside `foo()
68
+ perform some expensive computation and return data.
69
+
70
+ ** Execute the cached function:**
71
+
72
+ `d1 = foo(ref1)` executes `foo()` with the argument `ref1` and caches the result in `d1`.
73
+
74
+ `d2 = foo(ref1)` retrieves the cached result for the same argument, so `foo()` is not executed again, and `d1 == d2`.
75
+
76
+ `d3 = foo(ref2)` calls `foo()` with a different argument `ref2`, so the function executes again.
77
+
78
+ These snippets demonstrate how Streamlit allows you to cache function results to avoid redundant computation and improve performance.
0 commit comments