21

我无法将图像文件缓存起来。我已经尝试了在此站点和其他站点上找到的所有内容,但仍然无法将它们缓存。

我尝试过的 Web 配置设置

    <staticContent>
    <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
    </staticContent>
    <httpProtocol allowKeepAlive="true" />


    <caching enabled="true" enableKernelCache="true">
    <profiles>
    <add extension=".png" policy="CacheUntilChange" />
    <add extension=".jpg" policy="CacheForTimePeriod" duration="12:00:00" />
    </profiles>
    </caching>  

这是其中 1 张图片的响应标头

    Key Value
    Response    HTTP/1.1 200 OK
    Cache-Control   no-cache
    Content-Type    image/png
    Last-Modified   Thu, 16 Dec 2004 18:33:28 GMT
    Accept-Ranges   bytes
    ETag    "a1ca4bc9de3c41:0"
    Server  Microsoft-IIS/7.5
    X-Powered-By    ASP.NET
    Date    Fri, 18 May 2012 13:21:21 GMT
    Content-Length  775
4

2 回答 2

35

以下应该会导致浏览器缓存您的图像:

<staticContent>
    <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
</staticContent>
<httpProtocol>
    <customHeaders>
        <add name="Cache-Control" value="public" />
    </customHeaders>
</httpProtocol>

<caching>...</caching>块用于服务器端缓存,而不是客户端缓存。

于 2012-05-21T14:49:49.493 回答
1

如果有人需要将您的站点配置为 Chrome Audits 或 GTMetrix 要求,我已经使用以下内容配置了我的环境(感谢 Marco 的回答):

<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />

使用365天数,这两个工具都将该值视为缓存时间可接受的值。

于 2018-05-29T21:54:25.843 回答