? Design Pattern to cache service calls?

Discussion in 'Java' started by timasmith@hotmail.com, Mar 14, 2006.

  1. Guest

    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;
    }
    }
     
    , Mar 14, 2006
    #1
    1. Advertising

  2. Timo Stamm Guest

    schrieb:
    > 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


    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?
    >
    > 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;
    > }
    > }


    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
     
    Timo Stamm, Mar 14, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    594
  2. Leon Mergen

    Design pattern for file cache

    Leon Mergen, Mar 13, 2005, in forum: C++
    Replies:
    2
    Views:
    737
    Leon Mergen
    Mar 13, 2005
  3. Pallav singh
    Replies:
    0
    Views:
    364
    Pallav singh
    Jan 22, 2012
  4. Pallav singh
    Replies:
    0
    Views:
    405
    Pallav singh
    Jan 22, 2012
  5. Pallav singh
    Replies:
    1
    Views:
    453
    Peter Remmers
    Jan 22, 2012
Loading...

Share This Page