I have implemented it successfully. It works fine. Below code needs to implement.
Added filter in web.xml
<filter>
<filter-name>gwtCacheControlFilter</filter-name>
<filter-class>com.server.GWTCacheControlFilter</filter-class>
<init-param>
<param-name>private</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>expirationTime</param-name>
<param-value>2592000</param-value>
</init-param>
</filter>
GWTCacheControlFilter.java
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.samaxes.filter.util.CacheConfigParameter;
import com.samaxes.filter.util.Cacheability;
import com.samaxes.filter.util.HTTPCacheHeader;
/**
* Code imported from cachefilter.jar
* <br/><br/>
*/
public class GWTCacheControlFilter implements Filter {
private final static String[] STATIC_CONTENTS = { ".jpeg", ".jpg", ".gif",
".png", ".css", ".js", ".html" };
private Cacheability cacheability;
private boolean isStatic;
private long seconds;
private static final Logger LOGGER = Logger.getLogger(GWTCacheControlFilter.class);
public static final int YEAR_IN_MINUTES = 365 * 24 * 60 * 60;
public void destroy() {
}
public void init(FilterConfig filterConfig) throws ServletException {
this.cacheability = (Boolean.valueOf(filterConfig.getInitParameter(CacheConfigParameter.PRIVATE.getName())).booleanValue() ? Cacheability.PRIVATE : Cacheability.PUBLIC);
this.isStatic = Boolean.valueOf(filterConfig.getInitParameter(CacheConfigParameter.STATIC.getName())).booleanValue();
try{
this.seconds = Long.valueOf(filterConfig.getInitParameter(CacheConfigParameter.EXPIRATION_TIME.getName())).longValue();
} catch (NumberFormatException e) {
throw new ServletException("The initialization parameter " +
CacheConfigParameter.EXPIRATION_TIME.getName() + " is missing for filter " +
filterConfig.getFilterName() + ".");
}
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String requestURI = httpRequest.getRequestURI();
HttpServletResponse httpResponse = (HttpServletResponse) response;
if (requestURI.contains(".nocache.")) {
Date now = new Date();
httpResponse.setDateHeader("Date", now.getTime());
// one day old
httpResponse.setDateHeader("Expires", now.getTime() - 86400000L);
httpResponse.setHeader("Pragma", "no-cache");
httpResponse.setHeader("Cache-control",
"no-cache, no-store, max-age=0, must-revalidate");
}else if ( requestURI.contains( ".cache." ) ){
// set expiry to back in the past (makes us a bad candidate for caching)
final Calendar calendar = Calendar.getInstance();
calendar.setTime( new Date() );
calendar.add( Calendar.YEAR, 1 );
httpResponse.setDateHeader( "Expires", calendar.getTime().getTime() );
httpResponse.setHeader( "Cache-control", "max-age=" + YEAR_IN_MINUTES + ", public" );
httpResponse.setHeader( "Pragma", "" );
} else if (isStaticContentUri(requestURI)) {
StringBuilder cacheControl = new StringBuilder(this.cacheability.getValue()).append(", max-age=").append(this.seconds);
if (!this.isStatic) {
cacheControl.append(", must-revalidate");
}
httpResponse.setHeader(HTTPCacheHeader.CACHE_CONTROL.getName(), cacheControl.toString());
httpResponse.setDateHeader(HTTPCacheHeader.EXPIRES.getName(), System.currentTimeMillis() + this.seconds * 1000L);
if (httpResponse.containsHeader("Pragma")) {
LOGGER.debug("found pragma header");
httpResponse.setHeader(HTTPCacheHeader.PRAGMA.getName(), "");
} else {
LOGGER.debug("did not find pragma header");
}
}
filterChain.doFilter(request, response);
}
private boolean isStaticContentUri(String uri) {
uri = uri.toLowerCase();
for (String contentType : STATIC_CONTENTS) {
if (uri.endsWith(contentType.toLowerCase()))
return true;
}
return false;
}
}
I have a query related to localstorage, like it's cache or cookie..It is part of cookie.
Storage.getLocalStorageIfSupported() is part of cookie not cache. when I have clear cookie than it is clear.
On Friday, 20 March 2015 19:36:01 UTC+5:30, Bhumika Thaker wrote:
Hi Jens,I have try this -https://github.com/realityforge/gwt-cache-filter with sample gwt example.I have cross check with firebug. I found that each request is all time request new file instead of load from cache.Have you add any extra setting for same?Thanks,Bhumika
On Friday, 20 March 2015 16:28:13 UTC+5:30, Bhumika Thaker wrote:Hi,Thanks for reply.Let me implement it.I have used local storage "Storage.getLocalStorageIfSupported()" to store certificate private key. is value of local storage clear when cache clear?Thanks,Bhumika
On Friday, 20 March 2015 15:34:31 UTC+5:30, Jens wrote:If you apply the correct caching headers there is only minimal overhead on each request because the browser first checks if a new version on the server is available and only if it is available it will download the new version. Otherwise it will continue to use the already cached version.A lot of people use https://github.com/realityforge/gwt-cache-filter to apply caching headers to their gwt apps.-- J.
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment