New experimental primitives for caching

Hi, I’m an author of streamlit-webrtc.

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

I’m 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’s React.memo(), which accepts arbitrary dependency list and returns an identical object.
To do it, I’m using st.cache with ugly hack in streamlit-webrtc.

2 Likes