Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't use hash_funcs to compute an @st.cache function's
cache_key
(#…
…2331) Don't use hash_funcs for a function's `__qualname__` + `__module_` Fixes #2328 A longer explanation, from that bug: There are two parts to cache retrieval for @st.cache: 1) Retrieve the decorated function's `MemCache` instance. We use a `(func.__module__, func.__qualname__, func)` tuple to get the `cache_key` that uniquely identifies the function. No two functions (even if they have the same name and body) will share the same `MemCache`. 2) Retrieve the cached value from the function's `MemCache`. This is where we hash the function's arguments to produce the `value_key` for looking up the value _within_ the `MemCache`. We currently pass `hash_funcs` when computing both `cache_key` and `value_key`. This is generally innocuous, since `cache_key` uses two strings and a function's AST as hash values. However, if you supply a hash_func that operates on string values, you run the risk of having two different functions resolve the same `cache_key`, and end up unexpectedly sharing a `MemCache` instance. See [this forum issue](https://discuss.streamlit.io/t/caching-with-hash-funcs-fails-for-similar-methods/6941) for an example of this bug in action. In short, we should not use `hash_funcs` when hashing the string arguments of `cache_key`. We never want different functions to share the same `MemCache` instance.
- Loading branch information