【问题标题】:How do I get the browser to use the cache instead of reloading from the server?如何让浏览器使用缓存而不是从服务器重新加载?
【发布时间】:2016-10-26 06:20:47
【问题描述】:

我有一个搜索结果页面,当用户单击结果,然后单击返回按钮时,我希望它从缓存中加载搜索结果,而不是转到服务器,有点像 google 的做法。我正在发送 HTTP 标头:

Date: Thu, 23 Jun 2016 18:53:05 GMT
Server: WSGIServer/0.1 Python/2.7.9
Vary: Cookie
Last-Modified: Wed, 22 Jun 2016 00:00:00 GMT
ETag: a
Cache-Control: public, max-age=300
X-Frame-Options: SAMEORIGIN
Content-Type: text/html; charset=utf-8

但是当我使用后退按钮导航回结果时,它会从服务器检索页面,在这种情况下,服务器是在我的笔记本电脑上运行的开发 Django 服务器。

【问题讨论】:

    标签: django http http-headers request-headers response-headers


    【解决方案1】:

    您尝试过 .htaccess 吗? 在 .htaccess 中设置 Expires 和 Cache-Control 可以解决这个问题。 过期:

    <ifModule mod_expires.c>
     ExpiresActive On
     ExpiresDefault "access plus 5 seconds"
     ExpiresByType image/x-icon "access plus 2592000 seconds"
     ExpiresByType image/jpeg "access plus 2592000 seconds"
     ExpiresByType image/png "access plus 2592000 seconds"
     ExpiresByType image/gif "access plus 2592000 seconds"
     ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
     ExpiresByType text/css "access plus 604800 seconds"
     ExpiresByType text/javascript "access plus 216000 seconds"
     ExpiresByType application/javascript "access plus 216000 seconds"
     ExpiresByType application/x-javascript "access plus 216000 seconds"
     ExpiresByType text/html "access plus 600 seconds"
     ExpiresByType application/xhtml+xml "access plus 600 seconds"
    </ifModule>
    

    和缓存控制:

    <ifModule mod_headers.c>
      <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
        Header set Cache-Control "public"
      </filesMatch>
      <filesMatch "\.(css)$">
        Header set Cache-Control "public"
      </filesMatch>
      <filesMatch "\.(js)$">
        Header set Cache-Control "private"
      </filesMatch>
      <filesMatch "\.(x?html?|php)$">
        Header set Cache-Control "private, must-revalidate"
      </filesMatch>
    </ifModule>
    

    【讨论】:

    • 我没有在 Apache 上运行它
    • 你设置了缓存后端吗? CACHE_BACKEND = 'memcached:
    • 视图没有缓存搜索结果,但这会影响客户端缓存吗?
    猜你喜欢
    • 2016-11-27
    • 2013-01-09
    • 2016-07-24
    • 2019-04-10
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多