HTMLUNIT Cache Implementation

HTMLUNIT Cache Implementation

I got the latest codes from this URL:
https://htmlunit.svn.sourceforge.net/svnroot/htmlunit/trunk/htmlunit

Use maven to manage the project, these commands
>mvn compile
>mvn clean compile

Build the dependency for IDE eclipse
>mvn eclipse:eclipse

Running the test with this command
>mvn test
>mvn test -Dtest=CacheTest

Packaging
>mvn -DskipTests=true package
>mvn -DskipTests=true source:jar
>mvn -DskipTests=true source:test-jar

Check the simple Cache implementation com.gargoylesoftware.htmlunit.Cache

The URL.toString will be the keys, and the contents will be the values. All these things are stored in HashMap.

Add some log message in class:
private static final Log LOG = LogFactory.getLog(Cache.class);

public Object getCachedObject(final WebRequest request) {
if (HttpMethod.GET != request.getHttpMethod()) {
return null;
}
final Entry cachedEntry = entries_.get(request.getUrl().toString());
if (cachedEntry == null) {
return null;
}
synchronized (entries_) {
cachedEntry.touch();
}
if (LOG.isDebugEnabled()) {
LOG.debug("Cache is hit==========================size:" + this.getSize());
LOG.debug("key URL: " + request.getUrl().toString());
LOG.debug("Cache is hit==========================");
}
return cachedEntry.value_;
}

The test class is com.gargoylesoftware.htmlunit.CacheTest.

The class is as follow:
Cache htmlCache = new Cache();
webClient.setCache(htmlCache);

references:
http://htmlunit.sourceforge.net/gettingLatestCode.html
https://htmlunit.svn.sourceforge.net/svnroot/htmlunit/trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/Cache.java
https://htmlunit.svn.sourceforge.net/svnroot/htmlunit/trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/CacheTest.java

你可能感兴趣的:(htmlunit)