Using thread_safe Gem

I was monkey patching my way around issue #10791 when I noticed that the cache store had been converted over to a ThreadSafe::Cache. In fact, several classes now use ThreadSafe’s caches starting from 45448a5. The main problem I’m having at with it is that it’s not compatible with ActiveSupport::Cache (e.g. #fetch), so #10791 isn’t as easy as just dropping an ActiveSupport::Cache in. And since MemoryStore is thread-safe, I’m curious why we’re not using it instead.

For me to work on #10791, would my time be better spent on making an ActiveSupport::Cache compatible wrapper for ThreadSafe, or changing over the classes that use it to MemoryStore?

Hi,

ThreadSafe::Cache and ActiveSupport::Cache are classes with 2 completely different purposes. TS::Cache doesn’t support any of the AS::Cache’s features (TTL, max size, prune, etc.), but it is faster, more memory efficient and more scalable (under concurrent load) than AS::Cache, that is why it is being used for Rails internal caching. Notice how in the commit where it was introduced it always replaces Hash usage, but never AS::Cache usage.

As for the ThreadSafe::Cache#fetch method, originally it did have ActiveSupport::Cache#fetch semantics, but since TS::Cache was aimed at replacing naked/non-thread-safe concurrent Hash usage I chose it to be as Hash-like as possible.