如何防止 Rails 3.1 将静态资产缓存到 Rails.cache? [英] How do I prevent Rails 3.1 from caching static assets to Rails.cache?

查看:37
本文介绍了如何防止 Rails 3.1 将静态资产缓存到 Rails.cache?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rails 3.1 应用程序上使用 CloudFlare CDN.Cloudflare 是一个在 DNS 级别工作的 CDN.在第一次点击静态资产时,CloudFlare 从您的应用程序加载它,然后将其缓存在他们的 CDN 中.未来对该资产的请求从 CDN 而不是您的应用加载.

I'm using CloudFlare CDN on my Rails 3.1 application. Cloudflare is a CDN that works at the DNS level. On the first hit to a static asset, CloudFlare loads it from your app then caches it in their CDN. Future requests for that asset load from the CDN instead of your app.

我遇到的问题是,如果您将控制器缓存设置为 true:

The problem I'm having is that if you set controller caching to true:

config.action_controller.perform_caching = true

它启用了 Rack::Cache 中间件.由于 Rails 为静态资产设置了默认缓存控制设置,因此这些资产将写入 Rails.cache 存储.结果我的缓存存储(在我的例子中是 redis)被静态资产填满,url 作为哈希键.

it enables the Rack::Cache middleware. Since Rails sets a default cache control setting for static assets, those assets get written to the Rails.cache store. As a result my cache store (in my case redis) is being filled up with static assets with the url as the hash key.

不幸的是,我无法在不影响 Cloudflare 和我的用户浏览器缓存资产的方式的情况下关闭静态资产缓存控制标头.我无法关闭控制器缓存或丢失页面/操作/片段缓存.如果我删除 Rack::Cache 中间件,结果相同.

Unfortunately, I can't turn off the static asset cache control headers without affecting how Cloudflare and my users' browsers cache the assets. I can't turn off controller caching or I lose page/action/fragment caching. Same result if I delete the Rack::Cache middleware.

有人有其他想法吗?

更新:我在 GitHub 这里上开了一张票.

Update: I've opened a ticket on GitHub here.

推荐答案

原发帖者想要防止静态资源进入通用 Rails 缓存,这导致他们想要禁用 Rack::Cache.与其这样做,更好的解决方案是将 Rack::Cache 配置为使用单独的缓存而不是一般的 Rails 缓存.

The original poster wanted to prevent static assets from getting into the general Rails cache, which led them to want to disable the Rack::Cache. Rather than doing this, the better solution is to configure Rack::Cache to use a separate cache than the general Rails cache.

Rack::Cache 应该针对实体存储和元存储进行不同的配置.Rack::Cache 有两个不同的存储区域:元存储和实体存储.元存储保存有关每个缓存条目的高级信息,包括 HTTP 请求和响应标头.该区域存储被高频访问的小块数据.实体存储缓存响应正文内容,尽管它的访问频率低于元存储,但它可能是相对大量的数据.

Rack::Cache should be configured differently for entity storage vs meta storage. Rack::Cache has two different storage areas: meta and entity stores. The metastore keeps high level information about each cache entry including HTTP request and response headers. This area stores small chunks of data that is accessed at a high frequency. The entitystore caches the response body content which can be a relatively large amount of data though it is accessed less frequently than the metastore.

以下配置将 Metastore 信息缓存在 memcached 中,但将资产的实际主体缓存到文件系统中.

The below configuration caches the metastore info in memcached but the actual body of the assets to the file system.

使用 memcached gem:

Using memcached gem:

config.action_dispatch.rack_cache = {
  :metastore    => 'memcached://localhost:11211/meta',
  :entitystore  => 'file:tmp/cache/rack/body',
  :allow_reload => false
}

使用 dalli gem

Using dalli gem

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

顺便说一下,这个配置是对 Heroku 的推荐:https://devcenter.heroku.com/articles/rack-cache-memcached-static-资产-rails31

By the way this configuration is the recommendation for Heroku: https://devcenter.heroku.com/articles/rack-cache-memcached-static-assets-rails31

这篇关于如何防止 Rails 3.1 将静态资产缓存到 Rails.cache?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆