【问题标题】:How to get asset caching working如何让资产缓存工作
【发布时间】:2012-04-01 06:15:39
【问题描述】:

我正在尝试升级到雪松/资产管道,但遇到了一些问题。

一切都很好,但我的应用程序似乎明显变慢了。翻阅我的日志,我看到了大量这样的项目:

2012-03-15T17:03:02+00:00 应用程序[web.1]:缓存:[GET /assets/application.js] 错过 2012-03-15T17:03:02+00:00 app[web.1]: 缓存:[GET /assets/down_arrow.gif] 错过 2012-03-15T17:03:02+00:00 app[web.1]:缓存:[GET /assets/application.css] 未命中

我希望这些会很受欢迎——对吧?

我的生产.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

我的应用程序.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

谢谢!如果您需要更多信息,请告诉我

【问题讨论】:

  • 部署时是否在 Heroku 日志中看到资产编译?
  • 应该有类似-----> Preparing app for Rails asset pipeline Running: rake assets:precompile
  • 是的——我会用该信息更新问题

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


【解决方案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

【讨论】:

    【解决方案2】:

    我向 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 的方法

    【讨论】:

    猜你喜欢
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 2019-01-11
    • 2016-02-03
    • 2012-03-02
    相关资源
    最近更新 更多