比较Java中几种数据cache方式

现在很多网站一说到cache就想到memcached,memcached确实是非常优秀的系统,但是在某些场合,特别在不是分布式应用的场合,或者某些数据不需要分布式的访问,那它就未必是最佳的选择。

下面比较3种cache方式,用测试结果说话

Map,严格的说不能算cache,它适合本机访问,没有过期时间,不适合大容量,不能预计长度的数据,可能会使内存耗尽。 ehcache,可设过期时间,当超过指定内存数量,可设置淘汰算法,可输出到磁盘,可适合本机访问,也适合用作分布式缓存,分布式缓存配置和原理稍复杂,没有memcached直观,本测试未使用ehHcache分布式支持。 Memcached, 适合分布式缓存,可设过期时间

1. 环境
OS: Linux, Ubuntu 7.04 64-bit
Memory: 4G
CPU: Intel(R) Pentium(R) D CPU 2.66GHz
SCSI DISK, ext3 file system

libevent 1.3e
Memcached 1.2.4
Java 1.6.0

2. 测试方法
Key: 数字,1~100万
数据:100字节字符串

// put 100 char
Element e = new Element(String.valueOf(n), “blah…..blah… 100 chars…”);
cache.put(e);

Memcache的设置方法参看memcachedb的性能测试

3. ehcache 设置 <cache name=”cache1″
maxElementsInMemory=”1000000″
eternal=”true”
overflowToDisk=”false”
timeToIdleSeconds=”36000″
timeToLiveSeconds=”36000″
diskPersistent=”false”
diskExpiryThreadIntervalSeconds=”120″
memoryStoreEvictionPolicy=”LRU”
/>

4. 测试结果

类型 测试数据 平均速度(次/秒) 最大速度(次/秒) 占用内存 Thread(s) HashMap.put* 1千万 146,268 254,262 1344.18MB 1* Hashtable.put 1千万 128,752 253,911 1572.52MB 4 EHCache.put 1千万 118,399 381,601 1245.08MB 4 Memcached.put 1百万 16,515 19,942 118.20MB 4

* HashMap 不是线程安全,多线程测试无意义。

* 未比较GET测试结果,由于GET测试先要模拟一定数据,用空表去测试GET结果可能无意义。(但是GET比较可能更重要,有时间补上)

本文地址为:http://hi.baidu.com/jabber

参考资源:NP博士的文章PHP cache的比较 《大型》系列(三)——Cache & Buffer

补充:

写完几天之后无意在网上看到这两篇文章:一正一反,

正方:Comparing Memcached and Ehcache Performance说ehcache要快50~100倍

” border=”0″ small=”0″ />反方:Unfair Benchmarks of Ehcache vs Memcached,貌似国内访问不到,里面意思是说要用memcached的getmulti方式测试比较才公平。另外担心ehcache中LRU算法GC不能回收内存。

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

No related posts.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Contact us

Admin: Bryan Wu