Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

? Design Pattern to cache service calls?

Thread view: 
timasmith@hotmail.com - 14 Mar 2006 04:35 GMT
I have a fat client application which looks up fairly static reference
data (database table)

Example - columns to display for a search result list

I have the following code below as a snippet to cache the results while
the application is running - restarting the app clears the cache.

However it looks fragile and relies on the developer not making a
mistake when assigning the key for the cached service call

Is there a design pattern best suited for this common issue?

private static Hashtable cache = new Hashtable();

public static AppColumnList getSearchColumns(int applicationId) {
   String key = applicationId + ":ServiceClass:getSearchColumns";
   if (cache.containsKey(key)) {
       AppColumnList value = (AppColumnList) cache.get(key);
       return value;
   } else {
       AppColumnList value = getService().getAppColumns(applicationId,
userid);
       cache.put(key,value);
       return value;
   }
}
Timo Stamm - 14 Mar 2006 06:02 GMT
timasmith@hotmail.com schrieb:
> I have a fat client application which looks up fairly static reference
> data (database table)
[quoted text clipped - 6 lines]
> However it looks fragile and relies on the developer not making a
> mistake when assigning the key for the cached service call

I don't know what you mean. You always need a compact key for cache lookup.

There are several ways to implement memory caches in java. A more
advanced on might use weak references to use as much as possible (but
not more).

> Is there a design pattern best suited for this common issue?
>
[quoted text clipped - 12 lines]
>     }
> }

You never remove data from the cache?

The LinkedHashMap has a nice feature: It can remove its eldest entry:

class MemCache extends LinkedHashMap<String, byte[]> {

   private int e;

   public MemCache(int entries) {
      super(entries, 0.75f, true);
      e = entries;
   }
   @Override
   protected boolean removeEldestEntry(Entry<Object, byte[]> arg0) {
      return size() > e;
   }
}

Timo


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.