To
use JCS in your application you must include below jar files in class path.
jcs-1.3.jar
commons-logging.jar
commons-lang-2.4.jar
concurrent.jar
Step 1: First to store any
object into JCS cache, that class must implement Serializable interface. Let’s
say I want to cache Employee objects into cache.
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; } }
Step 2: We need to
configure cache to store employees in cache.ccf file. Make sure cache.ccf file
is in class path.
cache.ccf
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 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
cache.ccf
holds all the configuration required for cache. Above cache.ccf file configures
three caches, one default, one auxiliary cache(DC), one memory cache(empCache).
We are going to use empCache to hold employee objects.
Brief summary of ccf
file configuration
jcs.region.empCache=DC
inherit
configurations from cache DC
jcs.region.empCache.cacheattributes.MaxObjects=1000
Specifies
maximum number of objects allowed in memory
jcs.region.empCache.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
Used
to specify memory manager for this cache. By default it is LRU
jcs.region.empCache.cacheattributes.UseMemoryShrinker=true
Specifies
JCS to iterate periodically over the cache, looking for objects that can be
removed. Object can be removed if they exceed maximum idle time. By default
this value is false.
jcs.region.empCache.cacheattributes.MaxMemoryIdleTimeSeconds=3600
If
memory shrinker is enabled, this property specifies how longer this object is
in memory.
jcs.region.empCache.cacheattributes.ShrinkerIntervalSeconds=60
If
the memory shrinker is enabled, this property tells JCS how often to run the
shrinker. Default value is 60 seconds.
We
discuss about all the properties in subsequent posts.
Step 3: Get the cache
reference to access employee cache.
JCS
empCache = JCS.getInstance("empCache");
Above
statement gets reference for employee cache.
EmployeeCache.java
import org.apache.jcs.JCS; import org.apache.jcs.access.exception.CacheException; public class EmployeeCache { static JCS empCache; static{ try{ empCache = JCS.getInstance("empCache"); } 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); } }
Step 4: Test
Application
import org.apache.jcs.JCS; import org.apache.jcs.access.exception.CacheException; 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", "E5291532"); 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("E5291532"); System.out.println(cachedEmp.getId() + " " +cachedEmp.getFirstName() +" " + cachedEmp.getLastName()); cachedEmp = EmployeeCache.getEmployeeByID("E123445"); System.out.println(cachedEmp.getId() + " " +cachedEmp.getFirstName() +" " + cachedEmp.getLastName()); } }
Sample Output
Nov 23, 2014 9:28:05 PM org.apache.jcs.engine.control.CompositeCacheManager configure INFO: Creating cache manager from config file: /cache.ccf Nov 23, 2014 9:28:05 PM 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 23, 2014 9:28:05 PM org.apache.jcs.engine.control.CompositeCacheConfigurator setDefaultAuxValues INFO: Setting default auxiliaries to DC Nov 23, 2014 9:28:05 PM org.apache.jcs.engine.control.CompositeCacheConfigurator setDefaultCompositeCacheAttributes INFO: setting defaultCompositeCacheAttributes to [ useLateral = true, useRemote = true, useDisk = true, maxObjs = 1000, maxSpoolPerRun = -1, diskUsagePattern = 0 ] Nov 23, 2014 9:28:05 PM 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 = 1416758285494, LastAccessTime = 1416758285494, getTimeToLiveSeconds() = 21599, createTime = 1416758285494 ] Nov 23, 2014 9:28:05 PM org.apache.jcs.engine.memory.lru.LRUMemoryCache initialize INFO: initialized LRUMemoryCache for empCache Nov 23, 2014 9:28:05 PM 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 23, 2014 9:28:05 PM org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCache <init> INFO: Region [empCache] Cache file root directory: C:var Nov 23, 2014 9:28:05 PM org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCache initKeyMap INFO: Region [empCache] Set maxKeySize to: '1000000' Nov 23, 2014 9:28:05 PM org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCache <init> INFO: Region [empCache] Indexed Disk Cache is alive. Nov 23, 2014 9:28:05 PM org.apache.jcs.engine.control.CompositeCacheConfigurator parseRegions INFO: Parsed regions [empCache] E341267 Joel Chinna E5291532 Hari Krishna E123445 Rithwik Bhushan Nov 23, 2014 9:28:05 PM org.apache.jcs.engine.control.CompositeCacheConfigurator doConfigure INFO: Finished configuration in 83 ms.
No comments:
Post a Comment