Add thread safety and TTL to pn.state.as_cached #3198
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Now that we have the ability to set
--num-threads
I expect many more users will leverage threads to speed up their application. However not all functionality in Panel was written with thread-safety in mind. This PR ensures thatpn.state.as_cached
maintains thread locks for each cache key ensuring that if multiple threads are trying to access the same cache key at the same time only one of the executes the function that is being cached on.Additionally this PR also adds a TTL (time-to-live) for cache items allowing users to declare when a cached value expires and has to be refreshed.