Monday 24 November 2014

Configure region programmatically


You can configure attribute for a region cache programmatically.

Step 1: Get cache instance.
Example
JCS empCache = JCS.getInstance("empCache");

Step 2: Get IElementAttributes instance for this cache.
Example
IElementAttributes attributes = empCache.getDefaultElementAttributes();

Step 3: Use reference variable  “attributes” to set the region properties.
Example
attributes.setIdleTime(1000);
attributes.setIsEternal(true);
attributes.setIsLateral(true);
attributes.setIsRemote(false);
attributes.setIsSpool(true);
attributes.setLastAccessTimeNow();
attributes.setMaxLifeSeconds(50000);
empCache.setDefaultElementAttributes(attributes);

cache.ccf
# 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
jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.default.cacheattributes.UseMemoryShrinker=true
jcs.default.cacheattributes.MaxMemoryIdleTimeSeconds=3600
jcs.default.cacheattributes.ShrinkerIntervalSeconds=60
jcs.default.elementattributes=org.apache.jcs.engine.ElementAttributes
jcs.default.elementattributes.IsEternal=false
jcs.default.elementattributes.MaxLifeSeconds=21600
jcs.default.elementattributes.IdleTime=1800
jcs.default.elementattributes.IsSpool=true
jcs.default.elementattributes.IsRemote=true
jcs.default.elementattributes.IsLateral=true

#Configurations for auxilary cache
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=C:\var
jcs.auxiliary.DC.attributes.MaxPurgatorySize=10000000
jcs.auxiliary.DC.attributes.MaxKeySize=1000000
jcs.auxiliary.DC.attributes.MaxRecycleBinSize=5000
jcs.auxiliary.DC.attributes.OptimizeAtRemoveCount=300000
jcs.auxiliary.DC.attributes.ShutdownSpoolTimeLimit=60

#Configuration for employee cache
jcs.region.empCache=DC
jcs.region.empCache.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.region.empCache.cacheattributes.MaxObjects=1000
jcs.region.empCache.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.region.empCache.cacheattributes.UseMemoryShrinker=true
jcs.region.empCache.cacheattributes.MaxMemoryIdleTimeSeconds=3600
jcs.region.empCache.cacheattributes.ShrinkerIntervalSeconds=60
jcs.region.empCache.cacheattributes.MaxSpoolPerRun=500
jcs.region.empCache.elementattributes=org.apache.jcs.engine.ElementAttributes
jcs.region.empCache.elementattributes.IsEternal=false


Employee.java
import java.io.Serializable;

public class Employee implements Serializable{
    private String firstName;
    private String lastName;
    private String id;

    Employee(String firstName, String lastName, String id){
        this.firstName = firstName;
        this.lastName = lastName;
        this.id  = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getId() {
        return id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
 
}


EmployeeCache.java
import org.apache.jcs.JCS; 
import org.apache.jcs.access.exception.CacheException;
import org.apache.jcs.engine.behavior.IElementAttributes;

public class EmployeeCache {
    static JCS empCache;

    static{
        try{
            empCache = JCS.getInstance("empCache");
            IElementAttributes attributes = empCache.getDefaultElementAttributes();
            attributes.setIdleTime(1000);
            attributes.setIsEternal(true);
            attributes.setIsLateral(true);
            attributes.setIsRemote(false);
            attributes.setIsSpool(true);
            attributes.setLastAccessTimeNow();
            attributes.setMaxLifeSeconds(50000);
            empCache.setDefaultElementAttributes(attributes);
        }
        catch(CacheException e){
            
        }
    }

    static JCS getCache(){
        return empCache;
    }

    static void putCache(Employee emp){
        String id = emp.getId();
        if(empCache.get(id) == null){
            try {
                empCache.put(id, emp);
            }
            catch (CacheException ex) {

            }
        }
    }

    static Employee getEmployeeByID(String id){
        return (Employee)empCache.get(id);
    }
    
}


TestApplication.java
import org.apache.jcs.JCS;
import org.apache.jcs.access.exception.CacheException;
import org.apache.jcs.engine.behavior.IElementAttributes;

public class TestApplication {
    public static void main(String args[]) throws CacheException{
        JCS cache = EmployeeCache.getCache();
        Employee emp1 = new Employee("Joel", "Chinna", "E341267");
        Employee emp2 = new Employee("Hari", "Krishna", "E529532");
        Employee emp3 = new Employee("Rithwik", "Bhushan", "E123445");

        cache.put(emp1.getId(), emp1);
        cache.put(emp2.getId(), emp2);
        cache.put(emp3.getId(), emp3);

        Employee cachedEmp = EmployeeCache.getEmployeeByID("E341267");
        System.out.println(cachedEmp.getId() + " " +cachedEmp.getFirstName() +" " + cachedEmp.getLastName());

        cachedEmp = EmployeeCache.getEmployeeByID("E529532");
        System.out.println(cachedEmp.getId() + " " +cachedEmp.getFirstName() +" " + cachedEmp.getLastName());

        cachedEmp = EmployeeCache.getEmployeeByID("E123445");
        System.out.println(cachedEmp.getId() + " " +cachedEmp.getFirstName() +" " + cachedEmp.getLastName());

        IElementAttributes attributes = cache.getDefaultElementAttributes();
        System.out.println("Create Time : " +attributes.getCreateTime());
        System.out.println("Idle Time : " +attributes.getIdleTime());
        System.out.println("Is etrnal : " +attributes.getIsEternal());
        System.out.println("Is lateral : " +attributes.getIsLateral());
        System.out.println("Is remote : " +attributes.getIsRemote());
        System.out.println("Is spool : " +attributes.getIsSpool());
        System.out.println("Last Access time : " +attributes.getLastAccessTime());
        System.out.println("Maximum life seconds : " +attributes.getMaxLifeSeconds());
    }
}


Output
Nov 24, 2014 10:39:46 AM org.apache.jcs.engine.control.CompositeCacheManager configure
INFO: Creating cache manager from config file: /cache.ccf
Nov 24, 2014 10:39:46 AM org.apache.jcs.utils.threadpool.ThreadPoolManager loadConfig
INFO: thread_pool.default PoolConfiguration = useBoundary = [true] boundarySize = [2000] maximumPoolSize = [150] minimumPoolSize = [4] keepAliveTime = [300000] whenBlockedPolicy = [RUN] startUpSize = [4]
Nov 24, 2014 10:39:46 AM org.apache.jcs.engine.control.CompositeCacheConfigurator setDefaultAuxValues
INFO: Setting default auxiliaries to DC
Nov 24, 2014 10:39:46 AM org.apache.jcs.engine.control.CompositeCacheConfigurator setDefaultCompositeCacheAttributes
INFO: setting defaultCompositeCacheAttributes to [ useLateral = true, useRemote = true, useDisk = true, maxObjs = 1000, maxSpoolPerRun = -1, diskUsagePattern = 0 ]
Nov 24, 2014 10:39:46 AM org.apache.jcs.engine.control.CompositeCacheConfigurator setDefaultElementAttributes
INFO: setting defaultElementAttributes to [ IS_LATERAL = true, IS_SPOOL = true, IS_REMOTE = true, IS_ETERNAL = false, MaxLifeSeconds = 21600, IdleTime = 1800, CreateTime = 1416805786552, LastAccessTime = 1416805786552, getTimeToLiveSeconds() = 21599, createTime = 1416805786552 ]
Nov 24, 2014 10:39:46 AM org.apache.jcs.engine.memory.lru.LRUMemoryCache initialize
INFO: initialized LRUMemoryCache for empCache
Nov 24, 2014 10:39:46 AM org.apache.jcs.engine.control.CompositeCache <init>
INFO: Constructed cache with name [empCache] and cache attributes [ useLateral = true, useRemote = true, useDisk = true, maxObjs = 1000, maxSpoolPerRun = 500, diskUsagePattern = 0 ]
Nov 24, 2014 10:39:46 AM org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCache <init>
INFO: Region [empCache] Cache file root directory: C:var
Nov 24, 2014 10:39:46 AM org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCache initKeyMap
INFO: Region [empCache] Set maxKeySize to: '1000000'
Nov 24, 2014 10:39:46 AM org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCache <init>
INFO: Region [empCache] Indexed Disk Cache is alive.
Nov 24, 2014 10:39:46 AM org.apache.jcs.engine.control.CompositeCacheConfigurator parseRegions
INFO: Parsed regions [empCache]
Nov 24, 2014 10:39:46 AM org.apache.jcs.engine.control.CompositeCacheConfigurator doConfigure
E341267 Joel Chinna
E529532 Hari Krishna
E123445 Rithwik Bhushan
INFO: Finished configuration in 159 ms.
Create Time : 1416805786706
Idle Time : 1000
Is etrnal : true
Is lateral : true
Is remote : false
Is spool : true
Last Access time : 1416805786706
Maximum life seconds : 50000


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment