在GWT中永久缓存图像 [英] Permanently-Caching images in GWT
问题描述
缓存与放大器有什么区别?永久缓存在浏览器中。
在GWT框架中,图像文件重命名为 .cache。模式,并且我在谷歌网站的某处读取以永久性地缓存GWT中的图像,将App-Server配置为永久缓存它们。
但我不知道如何完全做到这一点。
我的网站图片永远不会改变,我想永远缓存它们,而不需要任何版本检查(为了获得最佳性能)
真诚地
这就是说,GWT引入了一种他们称之为完美缓存的概念。它通过将应用程序拆分为几个名为类似.cache.html的文件来工作,并且在您的应用程序代码或资源发生更改时,md5部分始终会更改。然后是引导脚本,其中包含查找正确的< md5> .cache.html
文件并加载它的逻辑。应用程序永远不应该被缓存。
在您的应用服务器中,您需要对其进行配置(在这种情况下为Apache)
< Files * .nocache。*>
ExpiresDefault访问
< / Files>
< Files * .cache。*>
ExpiresDefaultnow plus 1 year
< / Files>
在这种情况下,它被设置为缓存一年。据我所知,没有永久缓存的设置,只意味着非常高的到期时间。
Tomcat缓存
在Tomcat的情况下,就我所知,没有缓存控制,因此必须通过设置正确的HTTP标头手动完成。这可以通过使用过滤器来实现自动化。
/ *请不要在生产中使用它!* /
public class CacheFilter implements Filter {
$ b $ public void doFilter(ServletRequest req,ServletResponse res,FilterChain chain)
throws IOException,ServletException {
HttpServletRequest request =(HttpServletRequest)req;
HttpServletResponse响应=(HttpServletResponse)res;
//将所有内容缓存一年
response.addHeader(Cache-Control,max-age = 31556926);
chain.doFilter(request,response);
}
public void init(FilterConfig filterConfig){
this.fc = filterConfig;
}
public void destroy(){
this.fc = null;
$然后将过滤器映射到tomcat或衍生物(例如在web.xml中: < filter>
< filter-name> cachingFilter< / filter-name>
< filter-class> CacheFilter< / filter-class>
< / filter>
< filter-mapping>
< filter-name> cachingFilter< / filter-name>
< url-pattern> *。cache。*< / url-pattern>
< / filter-mapping>
What's difference between cache & permanently cache in browser.
In GWT framework image files rename to .cache. pattern and I read somewhere in google website to cache permanently images in GWT configure the App-Server for caching them permanently.
But I don't know how to do this exactly.
My website's images will never changed and I want to cache them forever without any version checking (for best performance)
Yours sincerely
解决方案 ClientBundle
introduced in GWT 2.0 allows you to bundle images and other resources in one file, this file is cached forever resulting in fewer server request.
That being said, GWT introduces a concept they call perfect caching. It works by splitting your application in several files named something like .cache.html and the md5 part always change when your application code or resources change. Then there's the bootstrap script, which contains the logic to look for the correct <md5>.cache.html
file and load it. The bootstrap should never be cached.
In your app server, you need to configure it something like this (Apache in this case)
<Files *.nocache.*>
ExpiresDefault "access"
</Files>
<Files *.cache.*>
ExpiresDefault "now plus 1 year"
</Files>
In this case it's set to cache for one year. As far as I know there's no setting to cache forever, that only means a very high expiration time.
Tomcat caching
In the case of Tomcat, as far as I know there's no cache control so it has to be done manually by setting the proper HTTP headers. This can be automated by the use of filters.
/*Please don't use this in production!*/
public class CacheFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
//cache everything for one year
response.addHeader("Cache-Control", "max-age=31556926");
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) {
this.fc = filterConfig;
}
public void destroy() {
this.fc = null;
}
}
Then map the filter in tomcat or derivatives (such as glassfish), in web.xml:
<filter>
<filter-name>cachingFilter</filter-name>
<filter-class>CacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cachingFilter</filter-name>
<url-pattern>*.cache.*</url-pattern>
</filter-mapping>
这篇关于在GWT中永久缓存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!