问题描述
我正在考虑使用JBoss Cache或Ehcache来实现缓存。看完这两个API之后,我觉得JBoss的内存效率可能比Ehcache高一点,因为它可以将 raw 对象放入缓存中,而Ehcache需要将数据包装在<$ c $中c> Element 对象。
I'm considering to use to implement a cache either JBoss Cache or Ehcache. After looking at both APIs I has the intuition that JBoss is probably a little bit more memory efficient than Ehcache since it can put raw objects into the cache while Ehcache needs to wrap the data in a Element
object.
我建立了一个快速工作台,在缓存中重复插入键,值元组。键和值类非常简单:
I set up a quick bench inserting repeatedly key, value tuples in the cache. The key and values classes are very simple:
键:
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字节)。
When inserting 100000 objects the result on memory where quite what I expected, Ehcache used 13396 bytes to store the objects while JBoss used 5712 bytes for the same operation (which is good since the same test using a ConcurrentHashMap
used 5680 bytes).
但是,当我查看执行时间时,我感到非常惊讶:Ehcache花费了300毫秒来执行测试,而JBossCache花费了44秒钟来执行测试。我很确定我的JBoss配置中有烂东西解释了这种区别。
However when I looked at the execution times, I had a very bad surprise: it took Ehcache 300 milliseconds to perform my test while it took 44 seconds for JBossCache to do the same. I'm pretty sure there's something rotten in my JBoss configuration explaining this difference.
Ehcache的编程方式如下:
Ehcache is initialized programmatically like this:
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缓存是使用Spring和以下bean配置创建的:
JBoss cache is created using Spring with the following bean configuration:
<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 the sake of completeness the Ehcache test is:
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());
}
设置/基准测试中出了错吗?
Anything wrong in my setup/benchmark?
推荐答案
我切换到,然后我那么就不会有任何奇怪的性能问题。
I switched to infinispan and I don't have any strange performance issues then.
这篇关于JBoss Cache和Ehcache的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!