sample example for JCS

10.7k views Asked by At

I am implementing JCS for the first time.

My requirement : I have one java class with a main method in which i am storing some data in the cache.

I have second java class with a main method from which i am retrieving from the disk cache which i had stored using the first java class.

Please note: 1. I want to use disk cache(Of JCS). 2. I want to retrieve the data from different JVM. 3. When i run the first Java class main method, i should store the data in the disk cache and when i run the second java class main method, i want to retrieve the data from the cache which is stored in the disk using the first java class main method.

class 1: main method..

public static void main(String[] args) {
//   Initialize the JCS object and get an instance of the default cache region
    try {
        JCS cache = JCS.getInstance("default");

    String key = "key0";
    String value = "value0";

    cache.put(key, value);
    cache.put("vasu","dev");


    } catch (CacheException e) {
        e.printStackTrace();
    }
}

class2: main method

public static void main (String asd[]){
    try {
        JCS cache = JCS.getInstance("default");


    String cachedData = (String)cache.get("vasu");


//   Check if the retrieval worked
    if (cachedData != null) {
      // The cachedData is valid and can be used
      System.out.println("Valid cached Data: " + cachedData);
    }
    else
         System.out.println("Invalid cached Data: ");

    } catch (CacheException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

cache.ccf:

jcs.default=DISK_REGION
jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.default.cacheattributes.MaxObjects=1000
jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.default.elementattributes.IsEternal=false
jcs.default.elementattributes.MaxLifeSeconds=3600
jcs.default.elementattributes.IdleTime=1800
jcs.default.elementattributes.IsSpool=true
jcs.default.elementattributes.IsRemote=true
jcs.default.elementattributes.IsLateral=true

jcs.region.OUR_REGION=DISK_REGION
jcs.region.OUR_REGION.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.region.OUR_REGION.cacheattributes.MaxObjects=1000
jcs.region.OUR_REGION.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.region.OUR_REGION.cacheattributes.UseMemoryShrinker=true
jcs.region.OUR_REGION.cacheattributes.MaxMemoryIdleTimeSeconds=3600
jcs.region.OUR_REGION.cacheattributes.ShrinkerIntervalSeconds=60
jcs.region.OUR_REGION.cacheattributes.MaxSpoolPerRun=500
jcs.region.OUR_REGION.elementattributes=org.apache.jcs.engine.ElementAttributes
jcs.region.OUR_REGION.elementattributes.IsEternal=false

jcs.auxiliary.DISK_REGION=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory
jcs.auxiliary.DISK_REGION.attributes=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes
jcs.auxiliary.DISK_REGION.attributes.DiskPath=c:/jcs/disk_region
jcs.auxiliary.DISK_REGION.attributes.MaxPurgatorySize=10000
jcs.auxiliary.DISK_REGION.attributes.MaxKeySize=10000
jcs.auxiliary.DISK_REGION.attributes.OptimizeAtRemoveCount=300000
jcs.auxiliary.DISK_REGION.attributes.MaxRecycleBinSize=7500
1

There are 1 answers

1
Tharanga Hewavithana On

I did two changes and the expected outcome of above sample code was achieved.

console -> "Valid cached Data: dev"

What I did

  1. Adding an additional line to cache.ccf under default cache region -

    jcs.default.cacheattributes.DiskUsagePatternName=UPDATE
    
  2. Adding a sleep at the end of class 1: main method

Explanation

  1. DiskUsagePattern defaults to SWAP which means the cache elements are written to the disk when MaxMemoryIdleTimeSeconds of the element has reached, default seems to be 60 * 120 seconds. When DiskUsagePattern is UPDATE, the elements are written to the disk when they are added to the cache. Well, elements are not written to the cache synchronously, instead added to a queue to be written immediately and return. So, if someone is looking for an immediate and reliable update on the disk, then DiskUsagePattern should be UPDATE not SWAP(default).
  2. The elements in above mentioned queue are immediately processed by a background thread which is marked as daemon. Real disk update happens in this background thread. So we need to give a little time for this daemon thread to be executed.