【问题标题】:How do I make Rails.cache (in-memory cache) work with Puma?如何使 Rails.cache(内存缓存)与 Puma 一起使用?
【发布时间】:2018-11-12 01:50:20
【问题描述】:

我使用的是 Rails 5.1。我有使用 Rails 发生的应用程序范围的 memory_store 缓存。这是在我的config/environments/development.rb 文件中设置的

  £ Enable/disable caching. By default caching is disabled.
  if Rails.root.join('tmp/caching-dev.txt').exist?
    config.action_controller.perform_caching = true

    config.cache_store = :memory_store
    config.public_file_server.headers = {
      'Cache-Control' => 'public, max-age=172800'
    }
  else
    config.action_controller.perform_caching = true
    config.cache_store = :memory_store
  end

这让我可以做类似的事情

      Rails.cache.fetch(cache_key) do
        msg_data
      end

在我的应用程序的一部分(Web 套接字)中,并在我的应用程序的另一部分(控制器)中访问该数据。但是,我注意到的是,如果我在运行 puma 的情况下启动 Rails 服务器(例如,在 config/puma.rb 中包含以下文件)...

threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count, threads_count

£ Specifies the `port` that Puma will listen on to receive requests, default is 3000.
£
port        ENV.fetch("PORT") { 3000 }

£ Specifies the number of `workers` to boot in clustered mode.
£ Workers are forked webserver processes. If using threads and workers together
£ the concurrency of the application would be max `threads` * `workers`.
£ Workers do not work on JRuby or Windows (both of which do not support
£ processes).
£
workers ENV.fetch("WEB_CONCURRENCY") { 4 }

app_dir = File.expand_path("../..", __FILE__)
shared_dir = "£{app_dir}/shared"

£ Default to production
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env

£ Set up socket location
bind "unix://£{shared_dir}/sockets/puma.sock"

£ Logging
stdout_redirect "£{shared_dir}/log/puma.stdout.log", "£{shared_dir}/log/puma.stderr.log", true

£ Set master PID and state locations
pidfile "£{shared_dir}/pids/puma.pid"
state_path "£{shared_dir}/pids/puma.state"
activate_control_app





£ Use the `preload_app!` method when specifying a `workers` number.
£ This directive tells Puma to first boot the application and load code
£ before forking the application. This takes advantage of Copy On Write
£ process behavior so workers use less memory. If you use this option
£ you need to make sure to reconnect any threads in the `on_worker_boot`
£ block.
£
£ preload_app!

£ The code in the `on_worker_boot` will be called if you are using
£ clustered mode by specifying a number of `workers`. After each worker
£ process is booted this block will be run, if you are using `preload_app!`
£ option you will want to use this block to reconnect to any threads
£ or connections that may have been created at application boot, Ruby
£ cannot share connections between processes.
£
on_worker_boot do
  require "active_record"
  ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
  ActiveRecord::Base.establish_connection(YAML.load_file("£{app_dir}/config/database.yml")[rails_env])
end

£ Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

内存缓存不再起作用。换句话说

Rails.cache.fetch(cache_key)

总是什么都不返回。我想要一个多线程 puma 环境(最终)来优雅地处理许多请求。但是,我也希望我的缓存能够正常工作。我怎样才能让他们一起玩?

【问题讨论】:

    标签: ruby-on-rails caching ruby-on-rails-5 puma in-memory


    【解决方案1】:

    您不能将memory_store 与在集群模式下运行的 puma 一起使用(即,与多个工人一起使用)。上面写着right here in the Rails guide。你不能在不同的进程之间共享内存,所以这显然是有道理的。

    如果将 puma worker 减少到 1 个不是一个选项,那么请考虑使用 Redis 或 Memcached。 Rails 指南中的文档在这方面非常完整 - 您需要在 Gemfile 中添加一两个 gem,并更新 config.cache_store。您需要在盒子上安装相关服务,或者有很多托管服务提供商会为您管理它(Heroku Redis、Redis To Go、Memcachier 等)

    【讨论】:

    • 我正在使用 Redis。你是说我可以用 Redis 实现内存共享?
    • @Dave:是的。 Redis 非常适合这种情况。在这种情况下,您可能已经拥有所需的所有 gem,唯一需要考虑的是您是否要引入命名空间以保持各个部分分开(不确定您在应用程序中的其他地方使用它。会话可能?)
    • 哦,这是个好消息。我正在使用 Redis 来帮助管理 Web 套接字、将消息发布到通道等等。关于发布的问题,我需要更改“config.cache_store = :memory_store”以利用 Redis?
    • @Dave:在 Rails 指南页面上向下滚动一点 :-) guides.rubyonrails.org/…。你会想要:redis_cache_store
    • 谢谢兄弟!我要试一试。我可能需要几天时间才能回到这里接受,但我会及时通知您我的进度。
    【解决方案2】:

    我不知道你是否可以 - 但无论如何都不要这样做。使用真正的缓存服务。例如,memcached。

    http://guides.rubyonrails.org/caching_with_rails.html

    config.cache_store = :mem_cache_store, "localhost" # assuming you run memcached on localhost
    

    还有……就是这样。

    【讨论】:

    • 我愿意接受您的建议,但我需要一些代码示例以及根据我发布的代码我需要做些什么来设置它。
    • 我只在单个服务器上运行我的应用程序,因此安装一个 memcached 应用程序来处理 Rails 缓存数据似乎有点过头了。如果绝对无法通过传统的 Rails 完成这项工作,我会回来接受。
    • 您认为 memcached 是开销。它是 - 但它是微不足道的。请记住,我们谈论的是具有大量 CPU/RAM 的大型机器。运行 memcached 非常简单且高性能。它是每个人用于缓存(或 redis 或其他东西)的东西,因为它就是这样做的,而且做得很好。不要去解决已经解决的问题。你有足够的自己的:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多