Monday, November 30, 2009

Working with JCS


Step 1: Core Concepts
Before using JCS, you must know some basic terms used in JCS like elements, region, auxiliaries.
Elements are the objects you put in cache. Objects are retrieved using a key, much like hashtable.
You can think of JCS as a collection of hashtables that you reference by a name. Each of these hashtables is called a Region. Each of the region can be configured independently from each other.
Auxiliaries are optional plugins that a region can use. The core auxiliaries are the Indexed Disk Cache, the TCP Lateral Cache, and the Remote Cache Server. The Disk Cache, for example, allows you to swap items onto disk when a memory threshold is reached.
Step 2: Downloading JCS and required dependencies
As of version 1.2.7.0, the core of JCS requires only two other jars.
commons-logging
Versions 1.2.6.9 and below also require the following two additional jars:
commons-collections
commons-lang
Step 3: Configuring JCS
Following is the configuration for a sample program.
# CACHE REGIONS AVAILABLE
# DEFAULT CACHE REGION

# sets the default aux value for any non configured caches
jcs.default=DC
jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.default.cacheattributes.MaxObjects=1000

# Regions preconfigured for caching
jcs.region.bookCache=DC
jcs.region.bookCache.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.region.bookCache.cacheattributes.MaxObjects=1200
jcs.region.bookCache.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.region.bookCache.elementattributes.IsEternal=false
jcs.region.bookCache.elementattributes.MaxLifeSeconds=7200
jcs.region.bookCache.elementattributes.IdleTime=1800
jcs.region.bookCache.elementattributes.IsSpool=true
jcs.region.bookCache.elementattributes.IsRemote=false
jcs.region.bookCache.elementattributes.IsLateral=true

# Primary Disk Cache -- faster than the rest because of memory key storage
jcs.auxiliary.DC=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory
jcs.auxiliary.DC.attributes=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes
jcs.auxiliary.DC.attributes.DiskPath=/usr/opt/bookstore/raf
jcs.auxiliary.DC.attributes.MaxPurgatorySize=10000
jcs.auxiliary.DC.attributes.MaxKeySize=10000
jcs.auxiliary.DC.attributes.OptimizeAtRemoveCount=300000
jcs.auxiliary.DC.attributes.MaxRecycleBinSize=7500

Provide at least one region and one auxiliary configuration.
Step 4: Examples
We have a Book java POJO class. This is the object we are interested to put in cache. The Book class must implement Serializable interface.  This is required as JCS uses Disk Swap once max. object limit is reached.
class Book implements Serializable {
      private String bookName;
      private String author;
      private String isbn;
     
      public String getBookName() {
            return bookName;
      }

      public void setBookName(String bookName) {
            this.bookName = bookName;
      }

      public String getAuthor() {
            return author;
      }

      public void setAuthor(String author) {
            this.author = author;
      }

      public String getIsbn() {
            return isbn;
      }

      public void setIsbn(String isbn) {
            this.isbn = isbn;
      }

      public Book(String bookName, String author, String isbn){
            this.bookName = bookName;
            this.author = author;
            this.isbn = isbn;
      }
     
      public Book(){
           
      }
}
Now we have to create a cache instance and put the Book objects in cache.  This is pretty easy, similar to putting and retrieving objects from Hashtable.
public class JCSExample {

      private static JCS bookCache;
      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            Book javaBook = new Book("Complete Reference Java", "Herbert Schildt", "1234");
            Book oracleBook = new Book("Oracle 10g", "Oracle Press", "1235");
            Book unixBook = new Book("Advanced Unix", "Linus", "1236");
           
            try {
                  bookCache = JCS.getInstance("bookCache1");
            } catch (CacheException e) {
                  // TODO Auto-generated catch block
                  System.out.println("Cannot create cache.");
                  e.printStackTrace();
            }
           
            try {
                  System.out.println("Inserting JavaBook in cache");
                  bookCache.put("JavaBook", javaBook);
                  System.out.println("Inserting OracleBook in cache");
                  bookCache.put("OracleBook",oracleBook);
                  System.out.println("Inserting UnixBook in cache");
                  bookCache.put("UnixBook", unixBook);
            } catch (CacheException e1) {
                  // TODO Auto-generated catch block
                  System.out.println("Could not insert into cache.");
                  e1.printStackTrace();
                  System.out.println(e1.getMessage());
                  System.exit(0);
            }
           
            Book book = new Book();
            book = (Book) bookCache.get("JavaBook");
            System.out.println("The Book name is - "+book.getBookName());
            book = (Book) bookCache.get("OracleBook");
            System.out.println("The Book name is - "+book.getBookName());
            book = (Book) bookCache.get("UnixBook");
            System.out.println("The Book name is - "+book.getBookName());
            System.out.println(bookCache.getStats());
            try {
                  bookCache.clear();
                  bookCache.dispose();
            } catch (CacheException e) {
                  // TODO Auto-generated catch block
                  System.out.println("Could not clear cache");
                  e.printStackTrace();
            }
      }
}

The program above is pretty straightforward. The best part of using JCS is, it’s simple to use.
Next post I will discuss about the Ehcache.

No comments:

Post a Comment