views:

264

answers:

3

In order to minimize the number of database queries I need some sort of cache to store pairs of data. My approach now is a hashtable (with Strings as keys, Integers as value). But I want to be able to detect updates in the database and replace the values in my "cache". What I'm looking for is something that makes my stored pairs invalid after a preset timespan, perhaps 10-15 minutes. How would I implement that? Is there something in the standard Java package I can use?

+4  A: 

I would use some existing solution(there are many cache frameworks). ehcache is great, it can reset the values on given timespan and i bet it can do much more(i only used that)

01
A: 

You can either use existing solutions (see previous reply)

Or if you want a challenge, make your own easy cache class (not recommended for production project, but it's a great learning experience.

You will need at least 3 members

  1. A cache data stored as hashtable object,

  2. Next cache expiration date

  3. Cache expiration interval set via constructor.

Then simply have public data getter methods, which verify cache expiration status:

  • if not expired, call hastable's accessors;

  • if expired, first call "data load" method that is also called in the constructor to pre-populate and then call hashtable accessors.

For an even cooler cache class (I have implemented it in Perl at my job), you can have additional functionality you can implement:

  • Individual per-key cache expiration (coupled with overall total cache expiration)

  • Auto, semi-auto, and single-shot data reload (e.g., reload entire cache at once; reload a batch of data defined either by some predefined query, or reload individual data elements piecemail). The latter approach is very useful when your cache has many hits on the same exact keys - that way you don't need to reload universe every time 3 kets that are always accessed expire.

DVK
For now it will be a learning experience, but I'll have Ehcahce and the others in mind when I need it for a real project. This time the specification does not call for this particular functionality - it's only something I want to try and do anyway (but never release to the public).
matpe
A: 

You could use a caching framework like OSCache, EHCache, JBoss Cache, JCS... If you're looking for something that follows a "standard", choose a framework that supports the JCache standard interface (javax.cache) aka JSR-107.

For simple needs like what you are describing, I'd look at EHCache or OSCache (I'm not saying they are basic, but they are simple to start with), they both support expiration based on time.

If I had to choose one solution, I'd recommend Ehcache which has my preference, especially now that it has joined Terracotta. And just for the record, Ehcache provides a preview implementation of JSR107 via the net.sf.cache.jcache package.

Pascal Thivent
The JSR-107 specification was a great read. It really deals with what my question is about.
matpe