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
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
本文详细介绍了如何使用Maven构建HTMLUNIT缓存实现,包括依赖管理、测试运行等关键步骤。通过MVN命令,实现项目的构建、测试与打包,深入理解缓存机制的应用。
2883

被折叠的 条评论
为什么被折叠?



