Python's id function | Example
Speed: Fast
Use case: If you're hashing a singleton object, like an open database connection or a TensorFlow session. These are objects that will only be instantiated once, no matter how many times your script reruns.
lambda _: None | Example
Speed: Fast
Use case: If you want to turn off hashing of this type. This is useful if you know the object is not going to change.
Python's hash() function | Example
Speed: Can be slow based the size of the object being cached
Use case: If Python already knows how to hash this type correctly.
Custom hash function | Example
Speed: N/a
Use case: If you'd like to override how Streamlit hashes a particular type.
Example 1: Pass a database connection around
Suppose we want to open a database connection that can be reused across multiple runs of a Streamlit app. For this you can make use of the fact that cached objects are stored by reference to automatically initialize and reuse the connection:
