如何在只使用tomcat的情况下,自动缓存js和css或者image等文件
该方法分为以下3个步骤
第一步:写一个filter,可以根据路径的正则来判断该路径的请求是否需要设置max-age:
Java代码
/**
*
* @author ahuaxuan
* @date 2008-12-4
* @version $id$
*/
public class CacheFilter implements Filter{
private static transient Log logger = LogFactory.getLog(CacheFilter.class);
private Integer cacheTime = 3600 * 24;
private List<Pattern> patternList = new ArrayList<Pattern>();
private static ResourceBundle rb = ResourceBundle.getBundle("cache-pattern");
public void destroy() {
}
public void doFilter(ServletRequest rq, ServletResponse rqs,
FilterChain fc) throws IOException, ServletException {
fc.doFilter(rq, rqs);
if (rq instanceof HttpServletRequest && rqs instanceof HttpServletResponse) {
HttpServletRequest request = (HttpServletRequest) rq;
HttpServletResponse response = (HttpServletResponse) rqs;
if (matchPattern(request.getRequestURI())) {
response.setHeader("Cache-Control", "max-age=" + cacheTime);
if (logger.isDebugEnabled()) {
StringBuilder sb = new StringBuilder();
sb.append(" set cache control for uri = ").append
(request.getRequestURI());
sb.append(" and the cache time is ").append(cacheTime).append("
second");
logger.debug(sb.toString());
}
}
} else {
if (logger.isWarnEnabled()) {
logger.warn("---- the request instance is not instanceof
HttpServletRequest ---");
logger.warn("---- the response instance is not instanceof
HttpServletResponse ---");
}
}
}
public void init(FilterConfig arg0) throws ServletException {
Enumeration<String> keys = rb.getKeys();
while (keys.hasMoreElements()) {
String p = keys.nextElement();
String value = rb.getString(p);
patternList.add(Pattern.compile(value, Pattern.CASE_INSENSITIVE));
if (logger.isInfoEnabled()) {
logger.info(">>>>>>>>>>> init the cache pattern " + value);
}
}
if (arg0 != null) {
String ct = arg0.getInitParameter("cache-time");
if (!"".equals(ct) && null != ct) {
cacheTime = new Integer(ct);
if (logger.isInfoEnabled()) {
logger.info(">>>>>>>>>> the cache time is " + cacheTime);
}
}
}
}
private boolean matchPattern(String url) {
for (Pattern pattern : patternList) {
if (pattern.matcher(url).matches()) {
return true;
}
}
return false;
}
public static void main(String [] args) throws ServletException {
CacheFilter cf = new CacheFilter();
cf.init(null);
System.out.println(cf.matchPattern("/css/prototype.CSS"));
}
}
/**
*
* @author ahuaxuan
* @date 2008-12-4
* @version $id$
*/
public class CacheFilter implements Filter{
private static transient Log logger = LogFactory.getLog(CacheFilter.class);
private Integer cacheTime = 3600 * 24;
private List<Pattern> patternList = new ArrayList<Pattern>();
private static ResourceBundle rb = ResourceBundle.getBundle("cache-pattern");
public void destroy() {
}
public void doFilter(ServletRequest rq, ServletResponse rqs,
FilterChain fc) throws IOException, ServletException {
fc.doFilter(rq, rqs);
if (rq instanceof HttpServletRequest && rqs instanceof HttpServletResponse) {
HttpServletRequest request = (HttpServletRequest) rq;
HttpServletResponse response = (HttpServletResponse) rqs;
if (matchPattern(request.getRequestURI())) {
response.setHeader("Cache-Control", "max-age=" + cacheTime);
if (logger.isDebugEnabled()) {
StringBuilder sb = new StringBuilder();
sb.append(" set cache control for uri = ").append(request.getRequestURI());
sb.append(" and the cache time is ").append(cacheTime).append(" second");
logger.debug(sb.toString());
}
}
} else {
if (logger.isWarnEnabled()) {
logger.warn("---- the request instance is not instanceof HttpServletRequest ---");
logger.warn("---- the response instance is not instanceof HttpServletResponse ---");
}
}
}
public void init(FilterConfig arg0) throws ServletException {
Enumeration<String> keys = rb.getKeys();
while (keys.hasMoreElements()) {
String p = keys.nextElement();
String value = rb.getString(p);
patternList.add(Pattern.compile(value, Pattern.CASE_INSENSITIVE));
if (logger.isInfoEnabled()) {
logger.info(">>>>>>>>>>> init the cache pattern " + value);
}
}
if (arg0 != null) {
String ct = arg0.getInitParameter("cache-time");
if (!"".equals(ct) && null != ct) {
cacheTime = new Integer(ct);
if (logger.isInfoEnabled()) {
logger.info(">>>>>>>>>> the cache time is " + cacheTime);
}
}
}
}
private boolean matchPattern(String url) {
for (Pattern pattern : patternList) {
if (pattern.matcher(url).matches()) {
return true;
}
}
return false;
}
public static void main(String [] args) throws ServletException {
CacheFilter cf = new CacheFilter();
cf.init(null);
System.out.println(cf.matchPattern("/css/prototype.CSS"));
}
}
第二步:在classpath路径下创建一个cache-pattern.properties文件,内容如下:
Java代码
1 = .*ext-all.js
2 = .*prototype.js
3 = .*/css/.*\\.css
1 = .*ext-all.js
2 = .*prototype.js
3 = .*/css/.*\\.css
在这个配置文件中,您可以根据js和css的路径来配置哪些目录,或者哪些文件需要设置max-age.
第三步:
在web.xml添加如下内容:
Java代码
<filter>
<filter-name>cache-filter</filter-name>
<filter-class>com.tudou.tudouadsales.component.web.filter.CacheFilter</filter-
class>
<init-param>
<param-name>cache-time</param-name>
<param-value>86000</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>cache-filter</filter-name>
<url-pattern>*.js</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>cache-filter</filter-name>
<url-pattern>*.css</url-pattern>
</filter-mapping>
注:本文转自http://d-tune.iteye.com/blog/638715