如何测试缓存和缓存破坏?

时间:2022-02-05 20:29:01

In PHP, I'm trying to steal a page from the Rails playbook (see 'Using Asset Timestamps' here):

在PHP中,我试图从Rails playbook中偷取一个页面(参见这里的“使用资产时间戳”):

By default, Rails appends assets' timestamps to all asset paths. This allows you to set a cache-expiration date for the asset far into the future, but still be able to instantly invalidate it by simply updating the file (and hence updating the timestamp, which then updates the URL as the timestamp is part of that, which in turn busts the cache).

默认情况下,Rails将资产的时间戳附加到所有资产路径。这允许您设置一个缓存过期日期的资产到未来,但仍然能够立即使它通过简单更新文件(因此更新时间戳,然后更新URL作为时间戳的一部分,进而萧条缓存)。

It‘s the responsibility of the web server you use to set the far-future expiration date on cache assets that you need to take advantage of this feature. Here‘s an example for Apache:

您所使用的web服务器的职责是在缓存资产上设置未来的过期日期,您需要利用这个特性。这里有一个Apache的例子:

  # Asset Expiration
  ExpiresActive On
  <FilesMatch "\.(ico|gif|jpe?g|png|js|css)$">
    ExpiresDefault "access plus 1 year"
  </FilesMatch>

If you look at a the source for a Rails page, you'll see what they mean: the path to a stylesheet might be "/stylesheets/scaffold.css?1268228124", where the numbers at the end are the timestamp when the file was last updated.

如果您查看一个Rails页面的源代码,您将看到它们的含义:样式表的路径可能是“/样式表/脚手架。css?”1268228124",其中末尾的数字是最后一次更新文件时的时间戳。

So it should work like this:

它应该是这样工作的:

  1. The browser says 'give me this page'
  2. 浏览器会说"给我这个页面"
  3. The server says 'here, and by the way, this stylesheet called scaffold.css?1268228124 can be cached for a year - it's not gonna change.'
  4. 服务器在这里说,顺便说一下,这个样式表叫做scaffolding .css?1268228124可以缓存一年,不会改变。
  5. On reloads, the browser says 'I'm not asking for that css file, because my local copy is still good.'
  6. 在重载时,浏览器会说:“我不是要那个css文件,因为我的本地拷贝还是不错的。”
  7. A month later, you edit and save the file, which changes the timestamp, which means that the file is no longer called scaffold.css?1268228124 because the numbers change.
  8. 一个月后,您编辑并保存文件,这将更改时间戳,这意味着该文件不再被称为scaffold.css吗?1268228124因为数字变了。
  9. When the browser sees that, it says 'I've never seen that file! Give me a copy, please.' The cache is 'busted.'
  10. 当浏览器看到这个文件时,它会说“我从来没见过那个文件!”请给我一份。“高速缓存被破坏了。”

I think that's brilliant. So I wrote a function that spits out stylesheet and javascript tags with timestamps appended to the file names, and I configured Apache with the statement above.

我认为这是聪明的。因此,我编写了一个函数,它输出样式表和带有附加在文件名后面的时间戳的javascript标记,并使用上面的语句配置Apache。

Now: how do I tell if the caching and cache busting are working?

现在:如何判断缓存和缓存破坏是否有效?

I'm checking my pages with two plugins for Firebug: Yslow and Google Page Speed. Both seem to say that my files are caching: "Add expires headers" in Yslow and "leverage browser caching" in Page Speed are both checked.

我正在用两个Firebug插件检查我的页面:Yslow和谷歌页面速度。两者似乎都说我的文件是缓存的:在Yslow中“添加过期头”和在页面速度中“利用浏览器缓存”都被检查。

But when I look at the Page Speed Activity, I see a lot of requests and waiting and no 'cache hits'.

但是当我查看页面速度活动时,我看到很多请求和等待,没有“缓存命中”。

If I change my stylesheet and reload, I do see the change immediately. But I don't know if that's because the browser never cached in the first place or because the cache is busted.

如果我更改样式表并重新加载,我将立即看到更改。但是我不知道这是因为浏览器从来没有缓存过还是因为缓存被破坏了。

How can I tell?

我怎么能告诉?

Update: It's working!

If anyone's interested, I just wrote a blog post explaining the details.

如果有人感兴趣的话,我只是写了一篇博客来解释细节。

1 个解决方案

#1


3  

In firebug you will see 304 Not Modified for cached pages.

在firebug中,您将看到304未被修改为缓存页面。

Check you have implemented these measures in Apache - I've found you need a mixture of settings to accommodate all browsers.

检查您是否在Apache中实现了这些措施——我发现您需要混合设置以适应所有浏览器。

#1


3  

In firebug you will see 304 Not Modified for cached pages.

在firebug中,您将看到304未被修改为缓存页面。

Check you have implemented these measures in Apache - I've found you need a mixture of settings to accommodate all browsers.

检查您是否在Apache中实现了这些措施——我发现您需要混合设置以适应所有浏览器。