Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ehcache Vs Static map cache implementation

I have few tables with very few entries in them and they will never change dynamically . so i want to cache the whole table in memory to reduce load on DB. I can easily achieve that by a static Map and populating the map in a static block.

i was wondering whether the same is possible in a more effective way by Ehcache + hibernate?

like image 527
dpsdce Avatar asked May 25 '11 07:05

dpsdce


2 Answers

Ehcache has a lot more features than a Map:

  • limit the maximum number of elements in memory
  • overflow to disk (if the above number is exceeded)
  • set a time-to-live and time-to-idle for elements
  • allows replication within a cluster

If you don't need any of those, you can safely use a Map - it will be easier to configure.

like image 131
Bozho Avatar answered Sep 29 '22 14:09

Bozho


The advantage of a real second-level cache over a static map is that you get the advantage of in-memory access by still keeping the same way of defining, accessing and traversing your entities: by using the Hibernate session (or entity manager).

You may keep relationships with other entities (even not cached); you may use a query cache and still perform queries over these entities (and results of these queries will be cached as well).

In short, it's transparent, offers more options as Bozho said, and is much easier to use because cached entties are used the same way as other entities.

like image 36
JB Nizet Avatar answered Sep 29 '22 14:09

JB Nizet