简体   繁体   中英

Ehcache eternal attribute usage with READ_WRITE objects

I am setting up Ehcache as a 2nd level cache for Hibernate on my project. All of the objects are going to be updated through Hibernate and have a cache strategy of CacheConcurrencyStrategy.READ_WRITE. In my mind, there should never be stale data in the cache because if an update occurs, the object will be removed from the cache. No updates to the database will occur outside of the application.

My question is if when defining the caches, setting the eternal attribute to true makes sense. I can't think of any reason that objects should expire from the cache and thus am inclined to set eternal to true. Am I misunderstanding how Ehcache will behave?

I think this depends on several things.

How "long-lived" are your entities? If you deal with entities that are never deleted, they will be living in your cache forever, occupying memory. If they are only handled once - in contrast to being accessed frequently, this makes no sense.

How big are your caches? Do they overflow to disk? Which memory eviction policy do you use?
I assume you restrict cache sizes, so putting more & more elements into the cache will cause eviction to happen, so you need to think about a proper eviction policy; as poor choices here may cause cache misses that can be avoided otherwise. It may also result in disk I/O (depending on your configuration) that you mostly want to avoid.

As positive indicators, you could go with eternal caching

  • if the number of objects that are to be cached is relatively small and won't exceed the cache size
  • you restart your application (and clear the cache) regularly at small intervals (like daily), such that the cache size won't be hit anyways
  • if the same objects are accessed frequently and thus you can use the LRU or LFU eviction policy meaningfully

Note that these are more or less rules of thumb; specific situations may require specific evaluation ;)

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