缓存动态生成的图像
我正在使用 PHP 脚本生成一些图像。
我想缓存图像,这样浏览器就不必每次都加载它们。 我添加了这些标头:
'max-age' => 3600
'Etag' => md5($image->getSlug())
'last-modified' => $image->getUpdatedAt()
'Vary' => 'Accept-Encoding'
'content-length' => (size of the image)
'Content-type' => 'image/jpeg'
但是图像没有缓存,并且每次都会由浏览器加载。
响应标头如下所示(使用 Firebug):
Date Sun, 04 Sep 2011 00:25:45 GMT
Server Apache/2.2.16 (Debian)
X-Powered-By PHP/5.3.3-7+squeeze1
Cache-Control max-age=3600, private
Etag "9280c6c672c6535c13b7481972f9ac39"
Last-Modified Sat, 27 Aug 2011 01:36:24 GMT
Vary Accept-Encoding
Content-Length 26231
Connection close
Content-Type image/jpeg
有人知道这里出了什么问题吗?
I am generating some images using a PHP script.
I want to cache the images, so the browser doesn't have to load them each time.
I have added these headers:
'max-age' => 3600
'Etag' => md5($image->getSlug())
'last-modified' => $image->getUpdatedAt()
'Vary' => 'Accept-Encoding'
'content-length' => (size of the image)
'Content-type' => 'image/jpeg'
but the image is not cached, and it is loaded every time by the browser.
The response headers look like these (using Firebug):
Date Sun, 04 Sep 2011 00:25:45 GMT
Server Apache/2.2.16 (Debian)
X-Powered-By PHP/5.3.3-7+squeeze1
Cache-Control max-age=3600, private
Etag "9280c6c672c6535c13b7481972f9ac39"
Last-Modified Sat, 27 Aug 2011 01:36:24 GMT
Vary Accept-Encoding
Content-Length 26231
Connection close
Content-Type image/jpeg
Does anyone have any idea what is wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
强制缓存图像的一种方法是将
Last-Modified
标记设置为您要使用的图像的创建日期和时间。例如:不要忘记创建一个数据库(在任何数据库程序中),其中包含一个名为
images
的表,其中sessionid
应为主键,并且日期。两者都应该是字符串。
One way you can force an image to be cached is to set the
Last-Modified
tag to the date and time that the image you want to use was created. For example:Don't forget to create a database (in any database program) that contains a table called
images
withsessionid
, which should be the primary key, anddate
. Both should be strings.您需要计算并添加 Expires: 标头。例如:
You need to calculate and add an Expires: header. For example: