【问题标题】:Exclude images from caching with Memcached / Dalli使用 Memcached / Dalli 从缓存中排除图像
【发布时间】:2012-04-26 15:59:16
【问题描述】:

我最近使用 Dalli gem 为我的 Rails 应用程序使用 memcached heroku 插件实现了缓存。但我发现,当部署到 Heroku 时,它还会缓存我的所有静态资产,包括图像,这很快就会炸毁我的 memcached 大小。 Heroku 日志的示例如下所示

cache: [GET /assets/application.css] fresh
app[web.1]: cache: [GET /assets/sign-in-twitter.gif] fresh
app[web.1]: cache: [GET /assets/ajax-loader.gif] fresh
app[web.1]: cache: [GET /assets/sign-in-facebook.gif] fresh

特别是对于索引页,每个不同请求的缓存大小都会增加大约 5MB。这种行为是可配置的吗?我可以将 memcached 配置为仅缓存我的片段缓存而不是主动缓存每个页面中的每个图像吗?

【问题讨论】:

    标签: ruby-on-rails-3 caching memcached dalli


    【解决方案1】:

    使用 dalli gem,在config/environments/production.rb:

    config.action_dispatch.rack_cache = { :metastore => Dalli::Client.new, :entitystore => '文件:tmp/cache/rack/body', :allow_reload => 假 }

    上述配置将元存储信息缓存在 memcached 中,但将资产的实际主体缓存到文件系统中。

    config/application.rb:

    如果!Rails.env.development? && !Rails.env.test? config.middleware.insert_before Rack::Cache, Rack::Static, urls: [config.assets.prefix], root: 'public' 结尾

    机架::静态使用:
    Rack::Static 中间件将带有匹配前缀的 url 提供给根目录。在这里,我将 config.assets.prefix 作为我的 url 前缀,默认为 '/assets'。这应该直接从 public/assets 目录中提供任何资产,而不是点击 Rails::Cache。这仅在您在生产中运行 'rake assets:precompile' 时才有效,否则 'public/assets' 中将没有预编译的资产。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 2013-12-30
      相关资源
      最近更新 更多