New experimental primitives for caching

Hi, I鈥檓 an author of streamlit-webrtc.

I took a glance at these new caching mechanisms though, I think they cannot meet my current needs.

I鈥檓 using st.cache to keep the object identity returned from a factory function for identical input arguments over reruns.

My actual code is here: streamlit-webrtc/factory.py at e351066c5887e7d7ebeadda06c7d12c95b4bac24 路 whitphx/streamlit-webrtc 路 GitHub

Example pseudo code is below:


obj_a = ... # Assume that the identity of this object is not changed over reruns until some event occurs. This is done by creating obj_a through a function wrapped by @st.cache or @st.singleton, or storing the object in the session_state.

obj_b = factory(obj_a) # !! How to memoize the factory function? !!

print(id(obj_b)) # I want to keep this identity of obj_b unchanged over reruns as long as id(obj_a) does not change.

st.memo cannot be used because it uses pickle() so seems not to preserve the returned object identity. In my case, additionally, obj_b is an instance of C-extension class, so cannot be pickled.

st.singleton cannot be used either because it cannot refresh the output identity when the input identity changes.

In other words, I want a Python version of ReactJS鈥檚 React.memo(), which accepts arbitrary dependency list and returns an identical object.
To do it, I鈥檓 using st.cache with ugly hack in streamlit-webrtc.

2 Likes