apache - 如何理解 "semantically transparent"部分RFC2616的 "Cache-Control machanism"?

标签 apache http http-headers cache-control http-caching

1.RFC2616 section "1.3 Terminology"中“语义透明”的定义

语义透明

  A cache behaves in a "semantically transparent" manner, with
  respect to a particular response, when its use affects neither the
  requesting client nor the origin server, except to improve
  performance. When a cache is semantically transparent, the client
  receives exactly the same response (except for hop-by-hop headers)
  that it would have received had its request been handled directly
  by the origin server.

2.RFC2616“13.1.3 Cache-control Mechanisms”这句话看不懂

Cache-Control header 允许客户端或服务器传输一个 请求或响应中的各种指令。这些 指令通常会覆盖默认的缓存算法。 作为 一般规则,如果标题之间有任何明显的冲突 值,应用最严格的解释(即, 一种最有可能保持语义透明性的方法)。

我混淆了“Cache-Control” header 中的那些冲突值

3.我通过Apache web服务器测试了一些例子

3.1 网络拓扑

Telnet(Client) <->HTTP proxy(apache work in proxy mode,S1) <->Web Server(Apache,S2)

3.1.1 S1配置(缓存代理):

<Location />
    ProxyPass http://10.8.1.24:80/
</Location>

<IfModule mod_cache.c>

        <IfModule mod_mem_cache.c>
            CacheEnable mem /
            MCacheSize 4096
            MCacheMaxObjectCount 100
            MCacheMinObjectSize 1
            MCacheMaxObjectSize 2048
        </IfModule>

        CacheDefaultExpire 86400
</IfModule>

3.1.2 S2配置(作为真正的网络服务器工作):

<filesMatch "\.(html|png)">
    Header set Cache-Control "max-age=5, max-age=15"
</filesMatch>

3.2 测试用例

3.2.1 两个“max-age”值

GET /index.html HTTP/1.1
Host: haha
User-Agent: telnet


HTTP/1.1 200 OK
Date: Wed, 13 Mar 2013 03:40:25 GMT
Server: Apache/2.2.23 (Win32)
Last-Modified: Sat, 20 Nov 2004 20:16:24 GMT
ETag: "63e62-2c-3e9564c23b600"
Accept-Ranges: bytes
Content-Length: 44
Cache-Control: max-age=5, max-age=35, must-revalidate
Age: 3
Content-Type: text/html

<html><body><h1>It works!</h1></body></html>

应用值“max-age=5”。 这里我认为应用“max-age=35”,因为这个值可以在缓存和服务器中存储更长时间的内容,以应对后续从“语义透明”概念提高性能的请求。

3.2.2 max-age=35 和 must-revalidate

GET /index.html HTTP/1.1
Host: haha
User-Agent: telnet

HTTP/1.1 200 OK
Date: Wed, 13 Mar 2013 03:41:24 GMT
Server: Apache/2.2.23 (Win32)
Last-Modified: Sat, 20 Nov 2004 20:16:24 GMT
ETag: "63e62-2c-3e9564c23b600"
Accept-Ranges: bytes
Content-Length: 44
Cache-Control: max-age=35, must-revalidate
Age: 10
Content-Type: text/html

<html><body><h1>It works!</h1></body></html>

应用值 max-age=35。 在这里,我认为应该应用值“必须重新验证”。

3.2.3 max-age=35 和 no-store

GET /index.html HTTP/1.1
Host: haha
User-Agent: telnet

HTTP/1.1 200 OK
Date: Wed, 13 Mar 2013 03:45:04 GMT
Server: Apache/2.2.24 (Unix)
Last-Modified: Sat, 20 Nov 2004 20:16:24 GMT
ETag: "63e62-2c-3e9564c23b600"
Accept-Ranges: bytes
Content-Length: 44
Cache-Control: max-age=35, no-store
Content-Type: text/html

<html><body><h1>It works!</h1></body></html>

应用值“no-store”。

3.2.4 max-age=36 和无缓存

GET /index.html HTTP/1.1
Host: haha
User-Agent: telnet

HTTP/1.1 200 OK
Date: Wed, 13 Mar 2013 06:22:14 GMT
Server: Apache/2.2.24 (Unix)
Last-Modified: Sat, 20 Nov 2004 20:16:24 GMT
ETag: "63e62-2c-3e9564c23b600"
Accept-Ranges: bytes
Content-Length: 44
Cache-Control: max-age=35, no-cache
Content-Type: text/html

<html><body><h1>It works!</h1></body></html>

应用值“no-cache”。

引用资料: RFC2616 https://www.rfc-editor.org/rfc/rfc2616

最佳答案

我会解释你给出的例子如下:

  • max-age=5, max-age=15:max-age=5 获胜,因为它是更短的缓存时间,限制性更强

  • max-age=5, max-age=35, must-revalidate:must-revalidate 获胜,因为它要求客户端始终重新验证要求。和 Section 14.9.4说:

      The must-revalidate directive is necessary to support reliable
      operation for certain protocol features. In all circumstances an
      HTTP/1.1 cache MUST obey the must-revalidate directive;
    
  • max-age=35, no-store: no-store 获胜,因为它基本上意味着不应该执行缓存,这当然是最限制性的。

  • max-age=35, no-cache:no-cache 获胜,因为它类似于 no-store并且不指定任何字段名称,这意味着缓存不得为后续请求重用响应,这是两者中限制性更强的一个。

关于apache - 如何理解 "semantically transparent"部分RFC2616的 "Cache-Control machanism"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15378546/

相关文章:

apache - 从 xampp 移动到 apache 2.4.7 后,Laravel 路由返回 404。 mod_rewrite 和 htaccess 或缺少 apache 设置?

c - 如何使用 ap_parse_form_data 并保留请求体?

浏览器不会将 max-age=0 添加到浏览器刷新的某些请求中

c# - 使用 HttpWebRequest 添加自定义 header

http - 为什么 do_GET 比 do_POST 快得多

ios - 尝试使用 Alamofire 4 和 OAuth2 发出 POST 请求时出错 - Swift

perl - 删除了自定义 HTTP header 字段

apache - 如何保持 apache 作为前端和 tomcat 作为后端?

java - 我怎么知道服务器是否会接受特定类型的内容类型?

java - 收到 400 - 错误请求 Java 客户端