Cache Object Pattern

The concept of keeping a copy of an object in some form of memory with the goal of providing a faster response time to a client request is called caching. A cache management 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.
The existence of the cache must remains transparent to the client.

public class Cache {
    //is works just based on a number. It can work based on time, etc.
    private final static int CACHE_SIZE = 100;
      List cache;
      public ItemCache() {
            cache = new ArrayList();
      public String getFromCache(...) {
        //check the cache and return the item
    public void addToCache(...) {
        //add to cache based on CACHE_SIZE

The service code that client will use should look like this:

public Item getItem(...) {
    cache = new Cache();
    if (cache.getFromCache(...) != null) {
        //is in the cache so return it
      } else {
        //get it from db or ... and add it to cache
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License