Ehcache 1.6 2 Orders of Magnitude Faster

I have been waiting for enough people to move to Java 5 to mandate it as a minimum standard for ehcache. At JavaOne 2008 I found out that a lot of people were still to make the move. Now that we are in 2009 I have decided to move to Java 5. As part of this I have done a general cleanup of the core. I can now retire backport-concurrent which has served the project well (thanks guys) and other dependencies. Ehcache-1.6 core has no dependencies.


I decided that with the improvements in concurrency support that have come along, it was time to move beyond the use of synchronized. Years ago I adopted striped locking on BlockingCache which gave amazing results but I left the core pretty much as it was. The rework adopts some new goodness in Java 5 such as CopyOnWriteArray and ConcurrentHashMap. Having said that there is nothing in Java 5 for eviction, so the new work relies heavily on some excellent contributions to provide performance for caching application that is not available in Java 5.

On my own concurrency tests, which use 70 threads simulating a typical load against a single cache, I get the following improvements in ehcache-1.6 over ehcache-1.5. (Note 70 are just for that cache. Ehcache typically has many caches, so this translates to a production system with thousands of threads against all caches)

Operation Number of Times Faster
Than Ehcache-1.5.0
get 92.5 times faster
put 30 times faster
remove 48 times faster
removeAll 80 times faster
keySet 30 times faster


Manik Surtani maintains a cache performance benchmark tool. Using that I have added ehcache-1.6. It shows dramatically the performance increases in Ehcache-1.6.

For those with less than perfect eyesight, the second column, which is too short to even have its time printed, is the ehcache-1.6 performance.


What these charts are saying, is that an ehcache, with 25 concurrent threads, is now much faster than it was. The single threaded case it no faster. But caches are not about single threads.
Now, in case everyone gets preoccupied on the comparsions between Java caches, here is an old Ehcache versus Memcached chart.
If I redid this chart using ehcache, the barely visible columns for ehcache would completely disappear on this scale.
So what is this really saying? An in-process cache, which uses a few tens of CPU operations to access data already held in memory, is much, much, much faster than going out over the network for some data, regardless of how slick the server implementation at the other end is.
But I recognise that Memcached is about a different type of caching: massive partitioned caches. The Ehcache project has the Ehcache Server for that, with RESTful and SOAP APIs. The RESTful implementation uses a variety of tricks such as conditional get, the ability to have hardware and software load balancers (think ngnx) perform URI routing, head, HTTP1.1 compression and pipelining plus the goodness of modern NIO Java Web Containers to seriously give memcached a run for its money. I will be doing some performance comparisons between Memcached and Ehcache Server in the near future.
What else is next? The above numbers are for MemoryStore based caches. I am also going to give the DiskStore a work over, with lots of suggestions made to me in the last year. Stay tuned.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ehcache是一款用于Java应用程序的内存缓存解决方案,它可以帮助我们有效地管理应用程序中的对象和数据。如果你需要读取2G的文件并将其缓存到Ehcache中,可以按照以下步骤进行操作: 1. 将文件读取到内存中,并将其存储为一个字符串或字节数组。 2. 使用Ehcache创建一个缓存对象,并设置合适的缓存配置项,如最大缓存大小、缓存过期时间等。 3. 将读取的文件内容存储到缓存对象中,可以使用Ehcache提供的put()方法。 4. 当需要访问文件内容时,可以首先从缓存中查找,如果缓存中存在,则直接返回缓存中的内容;如果缓存中不存在,则从磁盘中读取文件并存储到缓存中,并返回读取的内容。 以下是一个简单的Java代码示例,用于读取文件并将其缓存到Ehcache中: ```java import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import org.apache.commons.io.IOUtils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class FileCache { private CacheManager cacheManager; private Cache cache; public FileCache() { cacheManager = CacheManager.create(); cache = new Cache("fileCache", 2000, false, false, 0, 0); cacheManager.addCache(cache); } public byte[] getFileContent(String filePath) throws IOException { Element element = cache.get(filePath); if(element != null) { return (byte[]) element.getObjectValue(); } byte[] fileContent = IOUtils.toByteArray(new FileInputStream(new File(filePath))); cache.put(new Element(filePath, fileContent)); return fileContent; } public void shutdown() { cacheManager.shutdown(); } } ``` 在上面的示例中,我们使用了Apache Commons IO库的IOUtils类来读取文件内容,并使用了Ehcache库来创建缓存对象和缓存配置项。getFileContent()方法用于从缓存中获取文件内容,如果缓存中不存在,则从磁盘中读取文件并存储到缓存中。最后,我们使用shutdown()方法来关闭缓存管理器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值