tags:

views:

306

answers:

2

My Java web application (tomcat) gets all of its data from an SQL database. However, large parts of this database are only updated once a day via a batchjob. Since queries on these tables tend do be rather slow, I want to cache the results.

Before rolling my own solution, I wanted to check out existing cache solutions for java. Obviously, I searched stackoverflow and found references and recommendations for ehcache.

But looking through the documentation it seems it only allows for setting the lifetime of cached objects as a duration (e.g. expire 1 hour after added), while I want an expiry based on a fixed time (e.g. expire at 0h30 am).

Does anyone know a cache library that allows such expiry behaviour? Or how to do this with ehcache if that's possible?

+1  A: 

Do you need a full blown cache solution? You use standard Maps and then have a job scheduled to clear them at the required time.

Mark
That's the rolling my own solution option :), certainly viable, but if a good third party solution exists I'd obviously prefer that.
Pieter
+3  A: 

EhCache allows you programmatically set the expiry duration on an individual cache element when you create it. The values configured in ehcache.xml are just defaults.

If you know the specific absolute time that the element should expire, then you can calculate the difference in seconds between then and "now" (i.e. the time you add to the cache), and set that as the time-to-live duration, using Element.setTimeToLive()

skaffman