Reputation: 1462
I am trying to use JCS 1.3 to create a eternal disk cache. I want the cache to be able to shut down, and then reload itself from the disk the next time I use it. I have a lot of data that I am going to be populating from a DB that takes a long time to run and it is going to be much faster if the cache just reloads off of the disk. I have tried reading through the JCS documentation and using their configuration examples to get going, but no success so far. Here is the configuration I am using
##############################################################
##### Default Region Configuration
jcs.default=DC
jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.default.cacheattributes.MaxObjects=100
jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.default.cacheattributes.DiskUsagePatternName=UPDATE
##############################################################
##### CACHE REGIONS
jcs.region.myRegion1=DC
jcs.region.myRegion1.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.region.myRegion1.cacheattributes.MaxObjects=1000
jcs.region.myRegion1.cacheattributes.DiskUsagePatternName=UPDATE
jcs.region.myRegion1.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.region.myRegion1.elementattributes.IsEternal=true
##############################################################
##### AUXILIARY CACHES
# Indexed Disk 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=f:/eh cache
jcs.auxiliary.DC.attributes.MaxPurgatorySize=10000
jcs.auxiliary.DC.attributes.MaxKeySize=10000
jcs.auxiliary.DC.attributes.OptimizeAtRemoveCount=300000
jcs.auxiliary.DC.attributes.OptimizeOnShutdown=true
jcs.auxiliary.DC.attributes.MaxRecycleBinSize=7500
Here is a very simple test I am running to make sure my configuration is correct.
JCS cache = JCS.getInstance("myRegion1"); cache.put("key", "my value");
System.out.println("Value In Cache After Put [" + cache.get("key") + "]");
Thread.sleep(5000);
CompositeCacheManager.getInstance().shutDown();
cache = JCS.getInstance("myRegion1");
System.out.println("Value In Cache After Reload [" + cache.get("key") + "]");
Here is what I get in my output:
Value In Cache After Put [my value]
Value In Cache After Reload [null]
Would someone be able to point me in the right direction on this one? Not sure if it is an issue in my configuration, or something that needs to be done in the code to tell it to refresh from disk.
Upvotes: 3
Views: 1640
Reputation: 51
I know the question is old and the original poster have already found an alternative, but anyway these are my findings after going through the same problem.
CompositeCacheManager.getInstance()
The CompositeCacheManager is kept as a singleton. This instance is not prepared to "rearm" after a shutdown because there are some objects that are loaded only in its constructor. And the singleton instance is never cleared or reassigned.
After the shutdown the CompositeCacheManager
instance is still able to create cache objects, but they are not fully operational: they kind of work, using basic memory-based cache, but all the secondary resources (such as FileSystem stores) are never created again.
To me it looks like a broken implementation, but I am not sure wheter this behavior is by design or the result of a bug.
Workaround
If you are in a scenario where it is necessary to undeploy/deploy the caches, you will have to skip the getInstance()
methods and manage your own CompositeCacheManager instance.
It is not possible to instantiate it directly because of protected constructor + protected initialize()
method. Create a subclass in order to expose those elements, and create a factory method that mimics the CompositeCacheManager.getInstance()
behavior but without the part that keeps an eternal single reference.
Upvotes: 0
Reputation: 524
The shutdown call will dispose of the cache. I don't know about your architecture, but I've not called shutdown on JCS because I ensure that there are no calls made to JCS when I'm shutting it down. This makes it write to disk. By default it'll load from disk too so you don't have to do anything there.
Upvotes: -1