我正在考虑使用 JBoss Cache 或 Ehcache 来实现缓存。在查看了这两个 API 之后,我的直觉是 JBoss 可能比 Ehcache 的内存效率更高一些,因为它可以将 raw 对象放入缓存中,而 Ehcache 需要将数据包装在 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 + "]";
}
}
值:
public class Value implements Serializable{
/**
* serialVersionUID
*/
private static final long serialVersionUID = -499278480347842883L;
}
当插入 100000 个对象时,内存中的结果完全符合我的预期,Ehcache 使用 13396 字节来存储对象,而 JBoss 使用 5712 字节来进行相同的操作(这很好,因为使用 ConcurrentHashMap
的相同测试使用了 5680 字节).
然而,当我查看执行时间时,我有一个非常糟糕的惊喜:Ehcache 花费了 300 毫秒来执行我的测试,而 JBossCache 花费了 44 秒来执行相同的操作。我很确定我的 JBoss 配置中有一些问题解释了这种差异。
Ehcache 像这样以编程方式初始化:
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);
JBoss 缓存是使用具有以下 bean 配置的 Spring 创建的:
<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
文件:
<?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>
为了完整起见,Ehcache 测试是:
for (int i = 0; i < ITEM_COUNT; i++) {
this.cache.put(new Element(new Key(i), new Value()));
}
而 JBoss 是:
for (int i = 0; i < ITEM_COUNT; i++) {
this.processNode.put(new Key(i), new Value());
}
我的设置/基准测试有什么问题吗?
最佳答案
我切换到infinispan然后我没有任何奇怪的性能问题。
关于java - JBoss Cache 和 Ehcache 的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6346357/