简体   繁体   English

使用mod_headers或mod_expires使图像不可缓存

[英]Make images non-cacheable using mod_headers or mod_expires

My question is related to one post discussed some time ago: how to clear or replace a cached image 我的问题与前段时间讨论的一篇帖子有关: 如何清除或替换缓存的图像

Let me introduce what I am doing... I use JavaScript to create an animation that will be updated every 5 minutes. 让我介绍一下我在做什么...我使用JavaScript创建一个动画,该动画每5分钟更新一次。 I use the trick of adding time stamps to the image names, so my images are like: image-1-no-cache.png?d=131415135135 我使用在图像名称上添加时间戳的技巧,所以我的图像就像:image-1-no-cache.png?d = 131415135135

I use this to ensure the update. 我用它来确保更新。 If I don't make the difference between names, the browser won't update anything because the images will be stored in the cache and the browser won't realize that they changed. 如果我不区分名称,浏览器将不会更新任何内容,因为图像将存储在缓存中,并且浏览器不会意识到它们已更改。 The problem I have is that the images are continuously stored. 我的问题是图像是连续存储的。 I lose the reference via JavaScript and I don't know how to delete them. 我通过JavaScript丢失了引用,但我不知道如何删除它们。 After some hours, the cache is full of images and the web site is taking to much RAM. 几个小时后,缓存中充满了图像,该网站占用了大量RAM。

What I am trying to do right now is to set an expiration date to the images. 我现在想做的是为图像设置有效期。 I enabled the modules mod_headers and mod_expires and I changed my .htaccess file trying different things: 我启用了模块mod_headers和mod_expires,并尝试不同的方式更改了.htaccess文件:

<Files ~ ".*no-cache\.png?=*">
   Header set Cache-control "no-cache"
</Files>

or 要么

<FilesMatch ".*no-cache.png.*">
     ExpiresActive On      
   ExpiresDefault A300
</FilesMatch>

Nothing works. 什么都没有。 The idea is to make the files -no-cache.png?=. 这个想法是使文件-no-cache.png?=。 non-cacheable. 不可缓存。 Why I am not getting good results ? 为什么我没有取得好成绩? What am I missing ? 我想念什么?

This is the first time trying to do something similar and I am quite confused. 这是第一次尝试做类似的事情,我很困惑。 Any help will be appreciated. 任何帮助将不胜感激。 Thank you ! 谢谢 ! Yun

Adding ?something to the image does not make it non-cacheable. 添加?something到图像让它不可缓存。 It creates new URL which can be cacheable , and it does not affect other URLs, so all other ?versions remain in the cache as well. 它创建了可以缓存的 URL,并且不会影响其他URL,因此所有其他?versions保留在缓存中。

<Files ~ "no-cache\.png">
   Header set Cache-control "max-age=10, must-revalidate"
</Files>

This will tell Apache to add header to all files with no-cache.png anywhere in the filename ( .* is not needed). 这将告诉Apache将标头添加到文件名中任何位置具有no-cache.png所有文件中( no-cache.png .* )。 AFAIK this matches filesystem name, not the URL, so query string will never be there). AFAIK会匹配文件系统名称,而不是URL,因此查询字符串将永远不会存在)。

The header says to cache for maximum 10 seconds and check freshness with the server before every use. 标头表示最多可缓存10秒,并在每次使用前与服务器检查新鲜度。

This does not work ! 这行不通!

Using you configuration, the browser should revalidate (=update) the image after 10 seconds and this is not happening. 使用您的配置,浏览器应在10秒后重新验证(=更新)映像,并且不会发生这种情况。

I think that the best option is to set "Header set Cache-Control "no-store"". 我认为最好的选择是设置“ Header set Cache-Control“ no-store”“。 If I have all the images referenced in a JavaScript Image array I don't need to use the cache. 如果我在JavaScript Image数组中引用了所有图像,则不需要使用缓存。 For now, this is the best option I found. 目前,这是我找到的最佳选择。

Thank you anyway, Yun 谢谢你允

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM