JCS(Java Caching System) – Persisting cache to a database

You can read in details about JCS here : JCS -How to use it

JCS primarily stores the cache in memory. But we can configure auxiliary caches like disk, database, remote etc so that cache can be persisted there once it reaches the in-memory limit. But I had a requirement where I wanted all caches to be persisted to database. So here is the cache.ccf file with such configuration

##############################################################
################## DEFAULT CACHE REGION #####################
# sets the default aux value for any non configured caches
jcs.default=MYSQL
jcs.default.cacheattributes=org.apache.commons.jcs.engine.CompositeCacheAttributes
jcs.default.cacheattributes.MaxObjects=5000
jcs.default.cacheattributes.MemoryCacheName=org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache
jcs.default.cacheattributes.UseMemoryShrinker=true
jcs.default.cacheattributes.MaxMemoryIdleTimeSeconds=7200
jcs.default.cacheattributes.ShrinkerIntervalSeconds=60
jcs.default.elementattributes=org.apache.commons.jcs.engine.ElementAttributes
jcs.default.elementattributes.IsEternal=false
jcs.default.elementattributes.MaxLife=14400
jcs.default.elementattributes.IdleTime=14400
jcs.default.elementattributes.IsSpool=true
jcs.default.elementattributes.IsRemote=true
jcs.default.elementattributes.IsLateral=true

##############################################################
################## CACHE REGIONS AVAILABLE ###################

##############################################################
################## AUXILIARY CACHES AVAILABLE ################
# MYSQL disk cache used for flight options
jcs.auxiliary.MYSQL=org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCacheFactory
jcs.auxiliary.MYSQL.attributes=org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCacheAttributes
jcs.auxiliary.MYSQL.attributes.userName=root
jcs.auxiliary.MYSQL.attributes.password=PASSWORD
jcs.auxiliary.MYSQL.attributes.url=jdbc:mysql://localhost:3306/facebook?autoReconnect=true
jcs.auxiliary.MYSQL.attributes.driverClassName=com.mysql.jdbc.Driver
jcs.auxiliary.MYSQL.attributes.tableName=JCS_STORE
jcs.auxiliary.MYSQL.attributes.testBeforeInsert=false
jcs.auxiliary.MYSQL.attributes.maxActive=100
jcs.auxiliary.MYSQL.attributes.MaxPurgatorySize=10000000
jcs.auxiliary.MYSQL.attributes.UseDiskShrinker=true
jcs.auxiliary.MYSQL.attributes.ShrinkerInterval=1800
jcs.auxiliary.MYSQL.attributes.allowRemoveAll=false
jcs.auxiliary.MYSQL.attributes.EventQueueType=POOLED
jcs.auxiliary.MYSQL.attributes.EventQueuePoolName=disk_cache_event_queue

So here I have defined a MYSQL region and pointing the default region to it. In it, we have defined JDBC URL of database, username & password of the database

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *