com.gargoylesoftware.htmlunit
Class Cache

java.lang.Object
  extended by com.gargoylesoftware.htmlunit.Cache
All Implemented Interfaces:
Serializable

public class Cache
extends Object
implements Serializable

Simple cache implementation.

The current implementation's main purpose is to provide the ability to cache .js files.

Version:
$Revision: 2132 $
Author:
Marc Guillemot, Daniel Gredler
See Also:
Serialized Form

Constructor Summary
Cache()
           
 
Method Summary
 void cacheIfNeeded(WebRequestSettings request, WebResponse response)
          Cache the response if needed.
protected  void deleteOverflow()
          Truncates the cache to the maximal number of entries.
 WebResponse getCachedContent(WebRequestSettings request)
          Returns the cached content corresponding to the specified request.
 int getMaxSize()
          Returns the cache's maximum size.
 int getSize()
          Returns the number of entries in the cache.
protected  boolean isCacheable(WebRequestSettings request, WebResponse response)
          Determines if the response should be cached.
protected  boolean isDynamicContent(WebResponse response)
          Tries to guess if the content is dynamic or not.
protected  boolean isJavaScript(WebResponse webResponse)
          Indicates if the provided response is JavaScript content.
protected  Date parseDateHeader(WebResponse response, String headerName)
          Parses and returns the specified date header of the specified response.
 void setMaxSize(int maxSize)
          Sets the cache's maximum size.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Cache

public Cache()
Method Detail

cacheIfNeeded

public void cacheIfNeeded(WebRequestSettings request,
                          WebResponse response)
Cache the response if needed. The current implementation only caches JavaScript files.

Parameters:
request - the request
response - the response

deleteOverflow

protected void deleteOverflow()
Truncates the cache to the maximal number of entries.


isCacheable

protected boolean isCacheable(WebRequestSettings request,
                              WebResponse response)
Determines if the response should be cached.

Parameters:
request - the performed request
response - the received response
Returns:
true if the response should be cached

isDynamicContent

protected boolean isDynamicContent(WebResponse response)

Tries to guess if the content is dynamic or not.

"Since origin servers do not always provide explicit expiration times, HTTP caches typically assign heuristic expiration times, employing algorithms that use other header values (such as the Last-Modified time) to estimate a plausible expiration time".

The current implementation considers as dynamic content everything except responses with a Last-Modified header with a date older than 10 minutes or with an Expires header specifying expiration in more than 10 minutes.

Parameters:
response - the response to examine
Returns:
true if the response should be considered as dynamic and therefore uncacheable
See Also:

parseDateHeader

protected Date parseDateHeader(WebResponse response,
                               String headerName)
Parses and returns the specified date header of the specified response. This method returns null if the specified header cannot be found or cannot be parsed as a date.

Parameters:
response - the response
headerName - the header name
Returns:
the specified date header of the specified response

isJavaScript

protected boolean isJavaScript(WebResponse webResponse)
Indicates if the provided response is JavaScript content.

Parameters:
webResponse - the response to analyze
Returns:
true if it can be considered as JavaScript

getCachedContent

public WebResponse getCachedContent(WebRequestSettings request)
Returns the cached content corresponding to the specified request. If there is no corresponding cached content, this method returns null.

Parameters:
request - the request whose cached content is sought
Returns:
the cached content corresponding to the specified request

getMaxSize

public int getMaxSize()
Returns the cache's maximum size. This is the maximum number of files that will be cached. The default is 20.

Returns:
the cache's maximum size

setMaxSize

public void setMaxSize(int maxSize)
Sets the cache's maximum size. This is the maximum number of files that will be cached. The default is 20.

Parameters:
maxSize - the cache's maximum size (must be >= 0)

getSize

public int getSize()
Returns the number of entries in the cache.

Returns:
the number of entries in the cache


Copyright © 2002-2010 Gargoyle Software Inc.. All Rights Reserved.