首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使Rails.cache (内存缓存)与彪马一起工作?

如何使Rails.cache (内存缓存)与彪马一起工作?
EN

Stack Overflow用户
提问于 2018-06-02 06:13:50
回答 2查看 2.9K关注 0票数 11

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

代码语言:javascript
复制
  £ 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

这让我可以做像这样的事情

代码语言:javascript
复制
      Rails.cache.fetch(cache_key) do
        msg_data
      end

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

代码语言:javascript
复制
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

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

代码语言:javascript
复制
Rails.cache.fetch(cache_key)

始终不返回任何内容。我希望有一个多线程的puma环境(最终)来优雅地处理许多请求。然而,我也希望我的缓存能正常工作。我怎么才能让他们两个一起玩呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-08 00:24:03

你不能使用在集群模式下运行的puma (即多个工作进程)的memory_store。上面是这么说的,right here in the Rails guide。你不能在不同的进程之间共享内存,所以这显然是合理的。

如果不能将puma工作线程减少到1,那么可以考虑使用Redis或Memcached。在这方面,Rails指南中的文档非常完整--您需要在Gemfile中添加一两个gem,并更新config.cache_store。你需要在机器上安装相关的服务,或者有大量的托管服务提供商会为你管理它(Heroku Redis,Redis to Go,Memcachier等)

票数 6
EN

Stack Overflow用户

发布于 2018-06-02 07:20:15

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

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

代码语言:javascript
复制
config.cache_store = :mem_cache_store, "localhost" # assuming you run memcached on localhost

还有..。事情就是这样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50651571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档