简体   繁体   中英

EHCache in Java

We are using EHCache for caching in our application. We are storing a large XML String as value in the Cache. I would like some pointers on what would be more efficient way of storing the XML in cache, XML as a StringBuffer Object or a plain String value.

The point I am considering here is that if I save it as a plain String, it might be stored in Stack, while if I define it as an StringBuffer object, it will be stored in the Heap.

Could you please give some pointers on what would be the more efficient way.

Thanks, Mark

Which memory value are you considering here ?

  • String xml; ?
  • The cache memory used to store the xml ?

The first case is obvious it has to have memory to store the xml. From your question storing the XML in cache indicated you are looking for the cache memory for storing the xml.

Well that's in the hands of the Ehcache configuration. Cache uses different ways to store the values.You can look here how the attributes differ the configuration of where to store. 图片来源

There is one another advantage in using the size configuration as defined here

    You can use a percentage of the total JVM heap for the CacheManager 
maxBytesLocalHeap. The CacheManager percentage, then, is a portion of the total 
JVM heap, and in turn, the Cache percentage is the portion of the CacheManager 
pool for that tier.

Source

I would advise strongly against storing a StringBuffer / StringBuilder as they represent mutable objects.

As for performance, you may want to have a look into Ehcache 3 which is now feature complete and has support for custom serializers which enables you to store arbitrary type in an efficient way. And for example the default String serializer is much more performant than basic java serialization which is what Ehcache 2.x uses. And you could also go for offheap storage if you have a lot of these objects, as offheap is part of the open source offering of Ehcache 3.

Note: I work on Ehcache

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM