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

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何让Rails.cache(内存缓存)与Puma一起使用?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Rails 5.1.我在Rails中发生了应用程序范围的memory_store缓存.这是在我的config / environments / development.rb文件中设置的
  1. £ Enable/disable caching. By default caching is disabled.
  2. if Rails.root.join('tmp/caching-dev.txt').exist?
  3. config.action_controller.perform_caching = true
  4.  
  5. config.cache_store = :memory_store
  6. config.public_file_server.headers = {
  7. 'Cache-Control' => 'public,max-age=172800'
  8. }
  9. else
  10. config.action_controller.perform_caching = true
  11. config.cache_store = :memory_store
  12. end

这让我可以做的事情

  1. Rails.cache.fetch(cache_key) do
  2. msg_data
  3. end

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

  1. threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
  2. threads threads_count,threads_count
  3.  
  4. £ Specifies the `port` that Puma will listen on to receive requests,default is 3000.
  5. £
  6. port ENV.fetch("PORT") { 3000 }
  7.  
  8. £ Specifies the number of `workers` to boot in clustered mode.
  9. £ Workers are forked webserver processes. If using threads and workers together
  10. £ the concurrency of the application would be max `threads` * `workers`.
  11. £ Workers do not work on JRuby or Windows (both of which do not support
  12. £ processes).
  13. £
  14. workers ENV.fetch("WEB_CONCURRENCY") { 4 }
  15.  
  16. app_dir = File.expand_path("../..",__FILE__)
  17. shared_dir = "£{app_dir}/shared"
  18.  
  19. £ Default to production
  20. rails_env = ENV['RAILS_ENV'] || "production"
  21. environment rails_env
  22.  
  23. £ Set up socket location
  24. bind "unix://£{shared_dir}/sockets/puma.sock"
  25.  
  26. £ Logging
  27. stdout_redirect "£{shared_dir}/log/puma.stdout.log","£{shared_dir}/log/puma.stderr.log",true
  28.  
  29. £ Set master PID and state locations
  30. pidfile "£{shared_dir}/pids/puma.pid"
  31. state_path "£{shared_dir}/pids/puma.state"
  32. activate_control_app
  33.  
  34.  
  35.  
  36.  
  37.  
  38. £ Use the `preload_app!` method when specifying a `workers` number.
  39. £ This directive tells Puma to first boot the application and load code
  40. £ before forking the application. This takes advantage of Copy On Write
  41. £ process behavior so workers use less memory. If you use this option
  42. £ you need to make sure to reconnect any threads in the `on_worker_boot`
  43. £ block.
  44. £
  45. £ preload_app!
  46.  
  47. £ The code in the `on_worker_boot` will be called if you are using
  48. £ clustered mode by specifying a number of `workers`. After each worker
  49. £ process is booted this block will be run,if you are using `preload_app!`
  50. £ option you will want to use this block to reconnect to any threads
  51. £ or connections that may have been created at application boot,Ruby
  52. £ cannot share connections between processes.
  53. £
  54. on_worker_boot do
  55. require "active_record"
  56. ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
  57. ActiveRecord::Base.establish_connection(YAML.load_file("£{app_dir}/config/database.yml")[rails_env])
  58. end
  59.  
  60. £ Allow puma to be restarted by `rails restart` command.
  61. plugin :tmp_restart

内存缓存不再有效.换一种说法

  1. Rails.cache.fetch(cache_key)

永远不会回报.我想有一个多线程的puma环境(最终)优雅地处理许多请求.但是我也喜欢我的缓存工作.我怎样才能让他们一起玩?

解决方法

@H_502_21@ 你不能使用memma在集群模式下运行的memory_store(即有多个worker).它说的是 right here in the Rails guide.你不能在不同的进程之间共享内存,所以这显然是有道理的.

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

猜你在找的Ruby相关文章