Dynamic <img> tags unnecessarily reloading static images

社会主义新天地 提交于 2019-12-24 03:12:51

问题


I already asked this question at <img> tag not respecting max-age, and unnecessarily reloading images and thought the comments provided a satisfactory answer, but turns out I was mistaken. In the six months since, I think I've thought up a solution, but want to check it out here before implementing it. Will it work? Is it worth implementing? Any simpler solution? etc.

To briefly re-describe the problem, I have a cgi program that generates gif images for html pages with tags like the following

<img src="http://example.com/cgi-bin/gifprogram.cgi?query_string">

The ?query_string specifies the image to be generated, and once the program generates it, the image is cached in a cachedirectory/filename.gif with filename chosen as the md5-hash of the ?query_string. Then subsequent tags calling for the same image just get that same cached file.

So browsers can use their own cache, but treat the tag as dynamic, and re-download the same, often-large, images again-and-again, every time they see the tag, regardless of max-age or any other http-header info (that I could think to try). Okay, I understand why "dynamic" is necessary for actually-dynamic cgi applications. But there ought to be a way to tell the browser "static". There doesn't seem to be any standard way, so here's my kludge...

On the server side, I'm going to write a short cgi program called checkcache.cgi?expression that checks the cache directory for a filename that's md5hash(expression), i.e., it will check whether or not a desired image is already cached. And its output to stdout will be the usual http-headers, ending with text/text, followed by either the full path/filename.ext to the cached file corresponding to expression, or "notfound" if the desired image doesn't yet exist.

On the client side, rather than directly writing <img> tags, I'll write a php script that takes the desired expression and does the following. First, it uses curl to curl_exec() checkcache.cgi. Then, if the cached file already exists, it echo's a static <img> tag, with src="http://etc/filename.gif", using the filename returned by checkcache. But if checkcache returns "not found", then it echo's the dynamic tag to run the gifprogram.cgi dynamically.

Since checkcache only emits a few dozen bytes (rather than some of these gifs which can sometimes run half a MB or more), it should go real quick. So, my question again -- the above strategy will work, right? Or what's wrong with it? Or can you suggest a simpler idea? Thanks.

-----------------------------------------------------------------
Edit Update: Yeah, that works. Finally. Just an awfully elaborate way of accomplishing what I'd think ought to be a simple task. And I'd still be interested to learn some simpler procedure.

来源:https://stackoverflow.com/questions/44719626/dynamic-img-tags-unnecessarily-reloading-static-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!