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

查看:53
本文介绍了如何防止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.

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

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

使用内存缓存的gem:

Using memcached gem:

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

使用达利宝石

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- asset-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天全站免登陆