我已在JBoss Application Server 7.1.1中为我的Web应用程序设置了缓存过滤器
使用响应的setHeader和setDateHeader函数。
此后,过期日期始终设置为1个月 服务器已重新启动 - 月份也取决于当前月份: 因此,如果服务器已重新启动,例如:
2011年3月21日星期三格林尼治标准时间10:34 我将过期日期设置为:
周三,21 4月 2018 10:34 GMT或视当前日期而定:
但在Google-Chrome-Tools中,我总会看到错误的Expires-Date:
e.g。 theme.css:
而不是使用我发送的Expires-Date:
我不知道问题出在哪里,我目前在我的过滤器中使用此功能:
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
// Get request datas:
HttpServletResponse httpResponse = (HttpServletResponse)response;
HttpServletRequest reqt = (HttpServletRequest) request;
// Ignore ajax-request's:
String reqURI = reqt.getRequestURI().toLowerCase().trim();
String reqURIContext = reqt.getContextPath().toLowerCase()+ '/';
String reqURIPart = reqURI.substring(reqURIContext.trim().length()).toLowerCase().trim();
String CACHE_MEDIA = ";.js;.css;.svg;.png;.jpg;.gif;.pdf;.ico;.woff2;.woff;.ttf;.otf;.eot;"
+ResourceHandler.RESOURCE_IDENTIFIER+";";
Boolean useCache = false;
// Set Browser cache for all resources including all media files :
String[] medias2cache = CACHE_MEDIA.split(";");
for (int idx=1;idx<medias2cache.length;idx++)
{
if (reqURI.contains(medias2cache[idx]))
{
useCache = true;
break;
}
}
// Set browser-cache:
if (useCache)
{
httpResponse.setHeader("Cache-Control", "public, must-revalidate");
httpResponse.setDateHeader("Expires", InitBean.getStartupDateTime4CacheAsTimeStamp());
logger.info("*** Setting cache for static resource '"+reqURIPart+ '\'');
}
else
{
httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0.
httpResponse.setDateHeader("Expires", 0); // Proxies.
logger.info("*** No cache for '"+reqURIPart+ '\'');
}
// Continue with next filter:
chain.doFilter(request, response);
}
和每次启动服务器时调用的InitBean-Part:
@Singleton
@Startup
public class InitBean
{
// Time on which server has been restarted:
private static Date startUpDateTime = new Date();
@PostConstruct
private void StartUp()
{
// Set startup date/time
InitBean.startUpDateTime = new Date(System.currentTimeMillis());
}
public static Long getStartupDateTime4CacheAsTimeStamp()
{
// Calculate date difference in month since server has been started up:
Calendar startCalendar = new GregorianCalendar();
startCalendar.setTime(startUpDateTime);
Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(new Date());
//
int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
// And set expiration date to 1 month in the future from server startup :
diffMonth++;
// Get date in future:
startCalendar.add(Calendar.MONTH, diffMonth);
return startCalendar.getTimeInMillis();
}
}