ruby-on-rails - 如何使 Rails.cache(内存缓存)与 Puma 一起工作?

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

我正在使用 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

在我的应用程序的一部分(网络套接字)中,并在我的应用程序的另一部分( Controller )中访问该数据。但是,我注意到的是,如果我在运行 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 环境(最终)来优雅地处理许多请求。但是我也希望我的缓存工作。我怎样才能让他们一起玩?

最佳答案

您不能使用 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 等)

关于ruby-on-rails - 如何使 Rails.cache(内存缓存)与 Puma 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50651571/

相关文章:

ruby-on-rails - ruby rails : Foursquare remove list item gives 400 value is invalid for item id

ruby-on-rails - Cloudfront 正在缓存来自 nginx 的 404 错误,以获取源服务器上确实存在的 Assets

ruby-on-rails - Rails通过关联导出到csv

ruby-on-rails - gem-idea : Automatic spam protection with captcha in before_filter when HTTP-method is post, 放置或删除

ruby-on-rails - 如何在 heroku 中为单个应用程序添加多个子域?

iphone - Iphone应用程序有缓存吗?

java - 两个应用程序实例可以与 Hibernate L1 缓存一起使用而不会出现任何数据库同步问题吗?

ruby-on-rails - 使用 Active Storage Image 获取 MD5 哈希

ruby-on-rails - 使用 devise_token_auth +omniauth 请求规范

ruby-on-rails - Rails has_one 嵌套资源 form_for