Sunday, October 4, 2009

Patterns - 033: Structural patterns ( Object Cache )

The concept of keeping a copy of an object in the memory, in some form, with the goal of providing a faster response time to a client request is called caching. This is often done when the construction of a new object is expensive in terms of the processing involved. The object in the memory is not kept in the memory forever. Maintaining a large number of objects for a long time could have a negative effect on the application’s performance. A strategy must be developed to decide on the optimal number of objects to be cached and how long these objects are to be kept in the memory. Such decisions constitute the cache management strategy. The following example shows how caching can be applied in an application scenario to improve the response time.

Example

(object_cache)


The ItemManager offers an activate method that can be used by other application objects to activate an item. The ItemManager maintains a cache of some of the most recently activated item bar codes in the form of an instance variable of the ItemCache type. Whenever a client needs to activate an item, it:


1. Creates an ItemManager instance.

2. Invokes the activate method on the ItemManager object passing the item bar code as an argument.

The ItemManager checks to see if the item already exists in the cache.

  • If it exists then, it means that the item has been activated recently. The ItemManager simply returns with an appropriate message.
  • If the item does not exist, then the ItemManager accesses the database using a helper class DBManager to check if the item is already in the active state.
– If the item is already activated, the ItemManager returns with an appropriate message.
– If not, the ItemManager updates the item as active with the current date as the date-of-activation and adds it to the item cache.

This approach eliminates the need for redundant database updates.


The existence of the item cache remains transparent to the client object. The client can invoke methods on the ItemManager without having to know how the ItemManager uses the ItemCache internally.


Source for the ItemCache

public class ItemCache {
  private final static int Max_cache_size = 5;
  Vector cache;

  public ItemCache() {
      cache = new Vector();
  }

  public String getItem(String code) {
      String barCode = null;
      int pos = cache.indexOf(code);
      if (pos != -1)
          barCode = (String) cache.get(pos);
      return barCode;
  }

  public void addItem(String code) {
      // if the max limit is reached
      // remove the LRU item
      if (cache.size() == Max_cache_size) {
          cache.remove(0);
      }
      cache.add(code);
  }
}


Source for the ItemManager



public class ItemManager {
  ItemCache cache;
  DBManager manager;

  public ItemManager() {
      cache = new ItemCache();
      manager = new DBManager();
  }

  public void activate(String code) {
      if (cache.getItem(code) != null) {
          System.out.println("five.zero.three.five.txt.Item Already Activated - cache");

      } else {
          if (manager.isActiveItem(code)) {
              System.out.println(
                      "five.zero.three.five.txt.Item Already Activated - DB Access");
          } else {
              manager.activateItem(code);
              System.out.println(
                      "five.zero.three.five.txt.Item Activated successfully");
              //add to the cache
              cache.addItem(code);
          }
      }
  }
}

No comments:

Post a Comment