Rails 3.2和开发中的页面缓存导致缓存未命中-还有其他人吗? [英] Cache miss with Rails 3.2 and page caching in development - anyone else?

查看:104
本文介绍了Rails 3.2和开发中的页面缓存导致缓存未命中-还有其他人吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发模式下使用Rails 3.2,我正在尝试测试一些简单的页面缓存.

Using Rails 3.2 in development mode, I'm trying to test out some simple page caching.

pages_controller.rb

class PagesController < ActionController::Base

  caches_page :index, :show

  def index
    @pages = Page.all
  end

  def show
    @page = Page.find(params[:id])
  end

end

development.rb

config.action_controller.perform_caching = true

application.rb

config.action_controller.page_cache_directory = File.join(Rails.root, 'public')

当我测试此设置时,似乎可以正常处理这些操作,并且页面缓存按预期方式写入.但是,后续请求报告了让我感到困惑的以下两件事:

When I test this setup out, it seems to process these actions like normal, and the page cache gets written as expected. However, subsequent requests report the following two things that have me confused:

  1. 似乎错过了缓存,但是...
  2. 此后的请求似乎没有加载控制器,运行任何查询等,这使我相信它是从缓存中加载的.

这是日志在第一次请求时输出的内容,然后在随后的五次重载中输出:

Here's what the log outputs on first request, and then five reloads afterwards:

Started GET "/pages" for 127.0.0.1 at 2012-02-12 21:01:24 -1000
Processing by PagesController#index as HTML
  Page Load (0.2ms)  SELECT `pages`.* FROM `pages` 
  Rendered pages/index.html.erb (0.8ms)
Write page /Users/ckihe/Sites/experiment/public/pages.html (0.3ms)
Completed 200 OK in 3ms (Views: 1.9ms | ActiveRecord: 0.2ms)
cache: [GET /pages] miss
cache: [GET /pages] miss
cache: [GET /pages] miss
cache: [GET /pages] miss
cache: [GET /pages] miss

任何人都知道为什么缓存说它丢失了吗?

Anyone have any ideas why the cache says it's missing?

推荐答案

可以通过多种方式进行缓存(是的,有一些冗余).

There are multiple ways in which caching can happen (and yes there is some redundancy).

从(我认为)导轨3.1开始,为您设置了Rack::Cache.这是一个http级缓存,它了解所有有关到期时间,etag等的信息,并且可以将数据存储在各种缓存存储区中.这就是报告缓存未命中的原因,可能是因为您没有发出允许其缓存页面的缓存控制标头(请参见expires_infresh_when帮助器).

Starting with (I think) rails 3.1, Rack::Cache is setup for you. This is an http level cache that understands all about expiry times, etags etc and can store the data in a variety of cache stores. This is what is reporting a cache miss, probably because you're not emitting the cache control headers that would allow it to cache the page (see the expires_in or fresh_when helpers).

您配置的那种页面缓存已经很老了,并且完全不同地运行.它将渲染的HTML转储到您选择的目录中,然后Rails将其用作静态资产(在生产中,您可以将其配置为直接从Web服务器提供,而无需触摸ruby级别代码).这种缓存不太聪明,对http缓存控制标头等一无所知(但另一方面却非常快).

Page caching of the sort you have configured is way older and operates entirely dIfferently. It dumps the rendered HTML into the directory of your choice and rails is then serving those as static assets (in production you would configure this to be served straight from the web server without touching ruby level code). This caching is less smart and knows nothing about http cache control headers and so on (but is on the other hand very fast).

因此,总的来说,您有两种不了解彼此的缓存方案,这就是为什么您从其中一个错过而又从另一个错过了的原因.

So in summary you've got two caching schemes in place that are unaware of each other which is why you get a miss from one of them and a hit from the other one.

这篇关于Rails 3.2和开发中的页面缓存导致缓存未命中-还有其他人吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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