Creating ‘mini-caches’ in Java to speed up database calls.

Java has a cool construct called a Hashmap, this can be used to cache the result of a routine using a key.

For example.

Map cache=new HashMap();
public String getUnitsAtSite(String siteCode) {
     // is the sitecode already in the cache?
     if (cache.containsKey(siteCode)) return (String)cache.get(siteCode);
     String getSites=.... < get data from data base

     return getSites;


Now ofcourse there are downsides, if the data changes we won't see it - ofcourse we could do a cache.clear(); or cache.delete(key) on save of a change of record.

About Paul C

I am a Senior Java programmer/Web Technologies, Play in a band called Mandolin Monday, I have a cat and I live on a boat.

Posted on July 31, 2012, in glassfish, MYSQL. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: