How to Store a List in a Cache

Posted: September 3rd, 2011 | Author: | Filed under: General

Storing a list in a cache and getting it from the cache is easy. Here is how.

The simplest way is just to put the list to the cache. Note that if you want to use a strongly typed cache, you need to provide a concrete list type:


import java.util.ArrayList;
import org.cacheonix.Cacheonix;
import org.cacheonix.cache.Cache;

...

   /**
    * Puts a list to the cache.
    *
    * @param key  a key identifying the list.
    * @param list the list to cache.
    */
   public void putListToCache(String key, ArrayList<String> list) {

      final Cache<String, ArrayList<String>> listCache = Cacheonix.getInstance().getCache("listCache");
      listCache.put(key, list);
   }


   /**
    * Gets a list from the cache.
    *
    * @param key a key identifying the list.
    * @return the cached list or null if the list wasn't found in the cache or if the list is null.
    */
   public ArrayList<String> getListFromCache(String key) {

      final Cache<String, ArrayList<String>> listCache = Cacheonix.getInstance().getCache("listCache");
      final ArrayList<String> list = listCache.get(key);
      return list;
   }

While the above works, there is a better way, which is creating a first-class object for the cached list.

Usually lists don’t come alone and are accompanied by some metadata. For example, if you are caching a database result set, in future you might want to cache time it took to retrieve the result set in addition to the result set itself. Wrapping the list into a class provides an ability to maintain a stable signature of the cached object. If you need to add more fields in the future, you don’t have to change all places where a cached list is put to and retrieved from the cache.

A cached result set object that contains a list would then look like the following. Note that it declares a signature interface Immutable that signals the cache that this object can be passed by reference:


import java.io.Externalizable
import org.cacheonix.cache.Immutable;

...

  public class CachedResultSet implements Externalizable, Immutable {

    private List<String> list = null;


    public CachedResultSet(List<String> list) {
      this.list = new ArrayList<String>(list);
    }


    /**
     * Required by Externalizable.
     */
    public CachedResultSet() {
    }

    public List getList() {
      return new ArrayList(list);
    }

    public void writeExternal(ObjectOutput objectOutput) throws IOException {

      if (list == null) {

        objectOutput.writeBoolean(true);
      } else {

        objectOutput.writeInt(list.size());
        for (int i = 0; i < list.size(); i++) {

          objectOutput.writeUTF(list.get(i));
        }
      }
    }


    public void readExternal(ObjectInput objectInput) throws IOException, ClassNotFoundException {

      if (!objectInput.readBoolean()) {

        int size = objectInput.readInt();
        list = new ArrayList<String>(size);
        for (int i = 0; i < size; i++) {

          list.add(objectInput.readUTF());
        }
      }
    }
  }

Getting the cached result set would look like this:

   public CachedResultSet getListFromCache(String key) {

      final Cache<String, CachedResultSet> listCache = Cacheonix.getInstance().getCache("listCache");
      final CachedResultSet list = listCache.get(key);
      return list;
   }

Regards,

Slava Imeshev

(No) Comments: Post a response

You must be logged in to post a comment.