如何让资产缓存工作

时间:2012-03-15 17:16:13

标签: ruby-on-rails caching heroku asset-pipeline

我正在尝试升级到雪松/资产管道并且正在努力解决问题。

一切都很好,但我的应用似乎明显变慢了。看着我的日志,我看到了很多像这样的东西:

  

2012-03-15T17:03:02 + 00:00 app [web.1]:cache:[GET   /assets/application.js] miss 2012-03-15T17:03:02 + 00:00 app [web.1]:   缓存:[GET /assets/down_arrow.gif] miss 2012-03-15T17:03:02 + 00:00   app [web.1]:cache:[GET /assets/application.css] miss

我希望这些会成功 - 对吧?

我的production.rb

  config.cache_classes = true

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # For nginx:
  config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'

  # I also tried these
  # config.action_dispatch.x_sendfile_header = "X-Sendfile"
  # config.action_dispatch.x_sendfile_header = nil

  config.cache_store = :dalli_store

我的application.rb

...

    config.assets.enabled = true

    # Version of your assets, change this if you want to expire all your assets
    config.assets.version = '1.0'

    config.assets.initialize_on_precompile = false

    config.active_support.deprecation = :log

注意:assets:precompile在部署时可以正常工作:

-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
-----> Rails plugin injection
       Injecting rails_log_stdout
       Injecting rails3_serve_static_assets

谢谢!如果您需要其他信息,请与我们联系

2 个答案:

答案 0 :(得分:-1)

您可能错过了许多资产管道配置中的一个。看看我的production.rb文件:

HamburgOnRuby::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # Code is not reloaded between requests
  config.cache_classes = true

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = false

  # Compress JavaScripts and CSS
  config.assets.compress = true

  # Don't fallback to assets pipeline if a precompiled asset is missed
  # config.assets.compile = false

  # Generate digests for assets URLs
  config.assets.digest = true

  # Compress both stylesheets and JavaScripts
  config.assets.js_compressor  = :uglifier
  config.assets.css_compressor = :scss

  # Defaults to Rails.root.join("public/assets")
  # config.assets.manifest = YOUR_PATH

  # Specifies the header that your server uses for sending files
  # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
  config.action_dispatch.x_sendfile_header = nil # http://devcenter.heroku.com/articles/rails31_heroku_cedar#the_asset_pipeline

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  # config.force_ssl = true

  # See everything in the log (default is :info)
  # config.log_level = :debug

  # Use a different logger for distributed setups
  # config.logger = SyslogLogger.new

  # Use a different cache store in production
  # config.cache_store = :mem_cache_store

  # Enable serving of images, stylesheets, and JavaScripts from an asset server
  # config.action_controller.asset_host = "http://assets.example.com"

  # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
  config.assets.precompile += %w( active_admin.js active_admin.css )

  # Disable delivery errors, bad email addresses will be ignored
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.smtp_settings = {
    :address        => "smtp.sendgrid.net",
    :port           => "25",
    :authentication => :plain,
    :user_name      => ENV['SENDGRID_USERNAME'],
    :password       => ENV['SENDGRID_PASSWORD'],
    :domain         => ENV['SENDGRID_DOMAIN']
  }

  # Enable threaded mode
  # config.threadsafe!

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation can not be found)
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners
  config.active_support.deprecation = :notify

  # Set the default host for production
  config.default_host = 'onruby.de'
end

答案 1 :(得分:-1)

我用heroku提出了一张支持票,这里有回复:

  

我会试一试:   http://jackchu.com/blog/2011/09/20/rails-asset-pipeline-content-delivery-networks-and-rack-cache/

     

最近调查Rack :: Cache和memcached / dalli似乎是   在Rails 3.1 +中打破。

这似乎可以解决问题。我期待Rack :: Cache和dalli之间的互动得到修复,但同时我将遵循@jackchu方法