我正在考虑使用JBoss Cache或Ehcache来实现缓存。在查看这两个API之后,我直觉认为JBoss可能比Ehcache更有效,因为它可以将原始对象放入缓存中,而Ehcache需要将数据包装在{{1}中对象。
我设置了一个快速的工作台,在缓存中重复插入密钥,值元组。键和值类非常简单:
键:
Element
值:
public class Key implements Serializable {
private static final long serialVersionUID = -2124973847139523943L;
private final int key;
public Key(int pValue) {
this.key = pValue;
}
public int getValue() {
return this.key;
}
@Override
public String toString() {
return "Key [key=" + this.key + "]";
}
}
当在内存中插入100000个对象时,结果非常符合我的预期,Ehcache使用13396个字节来存储对象,而JBoss使用5712个字节进行相同的操作(这很好,因为使用了public class Value implements Serializable{
/**
* serialVersionUID
*/
private static final long serialVersionUID = -499278480347842883L;
}
的相同测试5680字节)。
然而,当我查看执行时间时,我有一个非常糟糕的惊喜:Ehcache需要300毫秒来执行测试,而JBossCache需要44秒才能执行相同操作。我很确定我的JBoss配置中有些东西可以解释这种差异。
Ehcache以编程方式初始化为:
ConcurrentHashMap
JBoss缓存是使用Spring创建的,具有以下bean配置:
CacheConfiguration cacheConfiguration = new CacheConfiguration("MyCache", 0).diskPersistent(false).eternal(true)
.diskExpiryThreadIntervalSeconds(100000).transactionalMode(TransactionalMode.OFF);
final Configuration config = new Configuration();
config.setDefaultCacheConfiguration(cacheConfiguration);
this.cacheManager = new CacheManager(config);
cacheConfiguration.name("primaryCache");
this.cache = new net.sf.ehcache.Cache(cacheConfiguration);
this.cacheManager.addCache(this.cache);
以及以下<bean id="cache" class="org.jboss.cache.Cache" factory-bean="cacheFactory" factory-method="createCache">
<constructor-arg>
<value type="java.io.InputStream">/META-INF/jbossCacheSimpleConf.xml</value>
</constructor-arg>
</bean>
文件:
jbossCacheConf.xml
为了完整起见,Ehcache测试是:
<?xml version="1.0" encoding="UTF-8"?>
<jbosscache xmlns="urn:jboss:jbosscache-core:config:3.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:jboss:jbosscache-core:config:3.2 http://www.jboss.org/schema/jbosscache/jbosscache-config-3.2.xsd">
</jbosscache>
虽然JBoss是:
for (int i = 0; i < ITEM_COUNT; i++) {
this.cache.put(new Element(new Key(i), new Value()));
}
我的设置/基准测试有什么问题吗?
答案 0 :(得分:4)
我切换到infinispan,然后我没有任何奇怪的性能问题。
答案 1 :(得分:0)
注意默认的JBossCache配置。 默认情况下,JBossCache可能会尝试在从属节点上查找和复制数据。