WhirlyCache下载地址:http://grepcode.com/snapshot/repo1.maven.org/maven2/com.whirlycott/whirlycache/1.0.1
原文1地址:http://blog.itpub.net/post/38949/480925
原文2地址:http://hi.baidu.com/zhangrg_123/item/47e6a0ab86c68c15a9cfb745
优点:
1.以较少代码实现了最常用的几种CACHE策略:FIFO,LRU,LFU
2.最大缓存对象数量可控
3.效率高(自己做了些测试,不过主要依据还是whirlyCache官方网站上的数据)
不足:
没有提供update方法,在需要更新cache中的某个对象时需要先del,再add,这样会丢失策略信息
在使用之前需要在classpath下配置whirlycache.xml文件,文件配置可参考whirlycache.jar中的whirlycache-default.xml的配置.
backend指定了cache的实现方式
tuner-sleeptime指定了隔多长时间执行优化和清理等
policy指定了清理策略实现类
maxsize指定了cache中可以存储的最大数量
使用后请调用CacheManager实例的shudown方法。
注意CacheManager使用的是单例模式,它会在初始化时执行配置操作。
如果在web程序中使用whirlyCache操作。建议做一个ContextListener来初始化CacheManager。whirlycache自带的有一个CacheManagerListener,可以作为参考。
WhirlyCache简单测试例子
concurrent-1.3.4.jar
commons-collections.jar
junit-4.4.jar
whirlycache-1.0.1.jar
代码如下:
package cn.cboard.test;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.cboard.util.DateUtil;
import com.whirlycott.cache.Cache;
import com.whirlycott.cache.CacheDecorator;
import com.whirlycott.cache.CacheException;
import com.whirlycott.cache.CacheManager;
public class TestWhirlyCache extends TestCase{
private Cache cache = null;
public static final long TIME_OUT = DateUtil.HOUR;
private static Log log = LogFactory.getLog(TestWhirlyCache.class);
public TestWhirlyCache(){
log.info("<<start...");
try{
cache = CacheManager.getInstance().getCache("member");
}
catch(CacheException ex){
ex.printStackTrace();
}
}
public void tearDown(){
try{
CacheManager.getInstance().shutdown();
}
catch(CacheException ex){
ex.printStackTrace();
}
}
public static Test suite()
{
return new TestSuite(TestWhirlyCache.class);
}
public String getEfficiencyReport(){
String result = "No report";
if(cache == null){
result = "cache is disabled";
}
else{
result = "cache cannot be inited";
}
if(cache instanceof CacheDecorator){
result = ((CacheDecorator) cache).getEfficiencyReport();
}
return result;
}
public void clear(){
if(cache!= null)
cache.clear();
}
public void testCacheDone(){
//Use the cache manager to create the default cache
//Cache c = CacheManager.getInstance().getCache();
//Put an object into the cache
//cache.store("yourKeyName", "korpton");
//String str = (String)cache.retrieve("yourKeyName");
//assertEquals("korpton",str);
//Shut down the cache manager
//try {
// CacheManager.getInstance().shutdown();
//} catch (CacheException e) {
// e.printStackTrace();
//}
try{
cache = CacheManager.getInstance().getCache();
cache.store("key", new String("korpton"));
cache.store("yahoo", "yahoo search");
String s = (String) cache.retrieve("key");
assertEquals("korpton",s);
assertEquals("yahoo search",cache.retrieve("yahoo"));
}
catch(CacheException e){
e.printStackTrace();
}
}
public void testBeanCache() throws CacheException{
for(int i=0;i<10;i++){
mBean mb = new mBean("korpton" + i);
cache.store("m"+i, mb);
}
mBean mb = (mBean)cache.retrieve("m3");
assertEquals("korpton3",mb.getm());
}
public void testReport(){
System.out.println(getEfficiencyReport());
}
public static void main(String[] args[]){
//junit.textui.TestRunner.run(TestWhirlyCache.class);
}
class mBean{
String name;
public mBean(String name){
this.name = name;
}
public void setm(String name){
this.name = name;
}
public String getm(){
return name;
}
}
}