为什么 Rails.cache 不是线程安全的?

发布于 2024-09-05 19:39:53 字数 270 浏览 2 评论 0 原文

我知道 Rails.cache 是 ActiveSupport::Cache::MemoryStore ,并且它不是线程安全的。

我不明白,为什么 Rails 使用线程不安全的缓存作为默认值?为什么不使用 ActiveSupport::Cache::SynchronizedMemoryStore?在我看来,在一个网站中,如果缓存不是线程安全的,它几乎毫无用处,因为请求不是在一个线程中处理的。

您在 Web 应用程序中使用 Rails.cache 吗?你如何使用它?

I know Rails.cache is ActiveSupport::Cache::MemoryStore, and it is not thread safe.

I don't understand, why rails use a thread-unsafe cache as its default? Why not use ActiveSupport::Cache::SynchronizedMemoryStore? In my opinion, in a web site, if a cache is not thread-safe, it almost useless, because the requests are not handled in ONE thread.

Do you use Rails.cache in you webapp? And how do you use it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悍妇囚夫 2024-09-12 19:39:53

Rails 中的默认缓存存储是 ActiveSupport::Cache::FileStore,而不是 MemoryStore

内存存储在实践中用途有限,因为它仅限于单个进程,这使得它对于使用 Passenger 或 Mongrel 集群部署的 Rails 应用程序毫无用处,其中请求在单独的进程中处理,而不是在单独的线程中处理。

对于中小型应用程序,您可能可以使用默认文件存储。如果您需要扩大规模,您应该看看 ActiveSupport::Cache::MemCacheStore

The default cache store in Rails is ActiveSupport::Cache::FileStore, not MemoryStore.

The memory store is of limited use in practice, since it is restricted to a single process, which makes it useless for Rails apps that are deployed using Passenger or a Mongrel cluster where requests are handled in separate processes, not in separate threads.

For small to medium-sized applications you'll probably do fine with the default file store. If you need to scale beyond that, you should have a look at ActiveSupport::Cache::MemCacheStore.

江城子 2024-09-12 19:39:53

从 Rails 版本 3.1 开始,默认的 Rails 缓存 (ActiveSupport::Cache MemoryStore) 是线程安全的: http://api.rubyonrails.org/v3.1.0/files/activesupport/CHANGELOG.html
正如变更日志所指出的:“确保线程安全,以便 Rails 使用的默认缓存实现
是线程安全的。”

The default Rails cache (ActiveSupport::Cache MemoryStore) is thread-safe as of Rails version 3.1: http://api.rubyonrails.org/v3.1.0/files/activesupport/CHANGELOG.html
As the CHANGELOG notes: "Make thread safe so that the default cache implementation used by Rails
is thread safe."

幼儿园老大 2024-09-12 19:39:53

大多数 Rails 部署场景实际上都是单线程的。并发是通过自动或预先生成多个进程来实现的。对于很多人来说,线程安全并不那么重要。

多线程选项确实存在(尤其是 JRuby),因此您的问题仍然有效。这就是为什么在 Rails 3 中,旧的 ActiveSupport::Cache::MemoryStore 已被删除并替换为 ActiveSupport::Cache::SynchronizedMemoryStore,从而使其成为线程安全的默认。

如果您需要 Rails 2 应用程序中的线程安全性,请将以下内容放在您的环境中的某个位置。

ActionController::Base.cache_store = :synchronized_memory_store

Most deployment scenario's for Rails are actually single-threaded. Concurrency is achieved by spawning multiple processes, either automatically or beforehand. For many people, thread-safety won't matter that much.

Multi-threaded options do exist (especially with JRuby), so your question is still valid. Which is why in Rails 3, the old ActiveSupport::Cache::MemoryStore has been removed and replaced with ActiveSupport::Cache::SynchronizedMemoryStore, making it thread-safe by default.

If you need the thread-safety in a Rails 2 app, put the following somewhere in your environment.

ActionController::Base.cache_store = :synchronized_memory_store
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文