j2ee页面静态化方案encache web cache框架源码分析2(六)

2014-11-24 08:12:19 · 作者: · 浏览: 15
a]
* Gets the CacheManager for this CachingFilter. It is therefore up to
* subclasses what CacheManager to use.
*


* This method was introduced in ehcache 1.2.1. Older versions used a
* singleton CacheManager instance created with the default factory method.
*
* @return the CacheManager to be used
* @since 1.2.1
*/
protected abstract CacheManager getCacheManager();

/**
* CachingFilter works off a key.
*


* The key should be unique. Factors to consider in generating a key are:
*


    *
  • The various hostnames that a request could come through
    *
  • Whether additional parameters used for referral tracking e.g. google
    * should be excluded to maximise cache hits
    *
  • Additional parameters can be added to any page. The page will still
    * work but will miss the cache. Consider coding defensively around this
    * issue.
    *

*


* Implementers should differentiate between GET and HEAD requests otherwise
* blank pages can result. See SimplePageCachingFilter for an example
* implementation.
*
* @param httpRequest
* @return the key, generally the URL plus request parameters
*/
protected abstract String calculateKey(final HttpServletRequest httpRequest);

3.SimplePageCachingFilter的处理
[java]
public class SimplePageCachingFilter extends CachingFilter {

public static final String DEFAULT_CACHE_NAME = "SimplePageCachingFilter";

private static final Logger LOG = LoggerFactory.getLogger(SimplePageCachingFilter.class);

protected String getCacheName() {
if (cacheName != null && cacheName.length() > 0) {
LOG.debug("Using configured cacheName of {}.", cacheName);
return cacheName;

} else {
LOG.debug("No cacheName configured. Using default of {}.", DEFAULT_CACHE_NAME);
return DEFAULT_CACHE_NAME;
}
}

protected CacheManager getCacheManager() {
return CacheManager.getInstance();
}


protected String calculateKey(HttpServletRequest httpRequest) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(httpRequest.getMethod()).append(httpRequest.getRequestURI()).append(httpRequest.getQueryString());
String key = stringBuffer.toString();
return key;
}

}

而SimpleCachingHeadersPageCachingFilter的处理
[java]
public class SimpleCachingHeadersPageCachingFilter extends SimplePageCachingFilter{
@Override
protected PageInfo buildPage(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws AlreadyGzippedException, Exception {
PageInfo pageInfo = super.buildPage(request, response, chain);

final List> headers = pageInfo.getHeaders();

long ttlMilliseconds = calculateTimeToLiveMilliseconds();

//Remove any conflicting headers
for (final Iterator> headerItr = headers.iterator(); headerItr.hasNext();) {
final Header< extends Serializable> header = headerItr.next();

final String name = header.getName();
if ("Last-Modified".equalsIgnoreCase(name) ||
"Expires".equalsIgnoreCase(name) ||
"Cache-Control".equalsIgnoreCase(name) ||
"ETag".equalsIgnoreCase(name)) {
headerItr.remove();
}
}

//add expires and last-modi