启用指定修改日期的图像缓存
我有一个加载许多图像的jsp 页面。我想缓存图像以加快加载速度。
我来阐述一下我的想法,如有错误请指正。我为每个图像调用图片加载 servlet,并以 BLOB 形式返回。我的想法是为图像添加修改日期和其他值,例如上次修改时间、过期时间、缓存控制和最大年龄。从而使浏览器了解图像是否发生变化。
但是如何将修改日期附加到 BLOB 中呢?或者有一些更好的想法可以使它们可缓存吗?
谢谢...
I've a jsp page which loads many images. I'd like to cache the images for faster loading.
I'll explain my idea, please correct it if it's wrong. I'm calling the picture loading servlet for each image and return as a BLOB. My idea is to add a modified date with the image and the other values like Last-Modified, expires, Cache-control and max age. And thereby make the browser understand if the image changes.
But how can i append a modified date to a BLOB? Or is there some better ideas to make them cachable?
Thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一件好事TM。
为此,您实际上需要
ETag
,Last-Modified
以及可选的过期
标头。通过 ETag 标头,服务器和客户端都可以识别唯一的文件。如果需要,您可以在每个数据库密钥下使用此操作。通过Last-Modified
header,服务器和客户端都知道它们是否具有相同版本的文件。使用Expires
标头,您可以指示客户端下次何时重新请求文件(因此,当Expires
中指定的日期已过期时)。Cache-Control
标头在这里并不那么相关,因为您只想允许缓存,而普通客户端默认情况下已经这样做了。有关更多信息和 servlet 示例,您可以找到这篇文章< /a> 有用,也许这篇文章对于如果您有兴趣调整 JSP/Servlet Web 应用程序的性能。
只需向相关数据库表中再添加一列即可表示插入日期。在大多数数据库中,您只需使用 now() 函数即可,甚至将其创建为自动触发器,以便在每次插入/更新时自动设置。
This is a Good ThingTM.
For that you actually need the
ETag
,Last-Modified
and optionally alsoExpires
header. With theETag
header both the server and client can identify the unique file. You can if necessary use under each the database key for this. With theLast-Modified
header header both the server and client knows if they both have the same version of the file. With theExpires
header you can instruct the client when to re-request the file the firstnext time (thus, when the date as specified inExpires
has been expired).The
Cache-Control
header is not so relevant here as you just want to allow caching and the average client already does that by default.For more information and a servlet example, you may find this article useful and maybe also this article for the case you'd be interested in tuning performance of a JSP/Servlet webapplication.
Just add one more column to the database table in question which represents the insertion date. In most DB's you can just use
now()
function for this or even create it as an auto-trigger so that it get set automatically on every insert/update.我相信您应该为受控缓存设置适当的标头。
您始终可以使用 http 协议规范作为发送和设置适当标头(响应标头)的参考。
您可以查看以下链接:
Caching in HTTP
http://www.w3.org/Protocols/rfc2616/rfc2616-sec13。 html
Http协议头域定义
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14。 html
问候,
I believe you should set the appropriate headers for controlled caching.
You can always use the http protocol specs as a reference for sending and setting the appropriate headers (response headers).
You can have a look at the following links:
Caching in HTTP
http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
Http Protocol Header Field Definitions
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
regards,
您不想修改 HTTP 标头,而不是实际的图像/BLOB 本身吗?
请参阅此页面有关在 servlet 响应中设置标头的信息。我怀疑您会查看标准标头,例如
Expires
。Don't you want to amend the HTTP headers, rather than the actual image/BLOB itself ?
See this page for info on setting headers in the servlet response. I suspect you'll be looking at standard headers such as
Expires
.(从重复的问题中移出)
添加一个过滤器(
javax.servlet.Filter
),只要响应包含图像,该过滤器就会添加缓存标头。类似于:(其中
DateUtil
是org.apache.commons.httpclient.util.DateUtil
)当然,上述过滤器假设您已经设置了正确的
Content - 输入您的图像
。否则我认为它们可能无法在浏览器中正确显示,无论有没有缓存。回答你的问题:
那是一个不同的场景。您可以将图像存储在某些缓存实现中(例如ehcache),但这是服务器缓存,而不是客户端缓存。
(moved from the duplicate question)
Add a filter (
javax.servlet.Filter
) that adds the cache headers whenever the response contains an image. Something like:(where
DateUtil
isorg.apache.commons.httpclient.util.DateUtil
)The above filter, of course, assumes you have set the right
Content-Type
for your images. Otherwise I think they might not be displayed properly in the browser, with or without cache.To answer your question:
That's a different scenario. You can store your images in some cache implementation (ehcache for example), but this is a server cache, not client cache.