Pregunta

We are planning to use some caching mechanism in our application, and chosen Java Caching System(JCS) after performing comparison study among many other caching solutions. Every thing is fine when I use external configuration (cache.ccf) to define cache regions and its properties( like maxlife, ideltime, etc).

But the requirement is changed to have dynamic cache regions, that is we would need to define cache regions and its properties at run time. I am not able to find more details or samples regarding this operation. I successfully created cache regions at run time ( using below method signature).

ICompositeCacheAttributes  cattr=..
IElementAttributes attr = new ElementAttributes();
attr.setIsEternal(false);
attr.setMaxLifeSeconds( maxLife );    
defineRegion(name,  cattr,attr);

But the problem is, IElmentAttributes does not sets to the cache. I did research on source of JCS and found attr is never set. It is non used argument!! bit strange

After some more googling, I found below options to set the attributes manually, but still did not work

 IElementAttributes attr = new ElementAttributes();
 attr.setIsEternal(false);
 attr.setMaxLifeSeconds( maxLife );
 jcs.setDefaultElementAttributes(attr);

All I want is to set maxLifeSeconds for created regions.

¿Fue útil?

Solución

I found way through for my problem, we need to set attributes when you put the data in to cache. See the implementation for somebody who is interested,

JCS jcs = JCS.getInstance("REGION");
IElementAttributes attr = new ElementAttributes();
attr.setIsEternal(false);
attr.setMaxLifeSeconds( maxLife );
jcs.put("Key",data, attr);

Otros consejos

Sample code here

Properties props = new Properties();
//
// Default cache configs
//
props.put("jcs.default", "");
props.put("jcs.default.cacheattributes","org.apache.jcs.engine.CompositeCacheAttributes");
props.put("jcs.default.cacheattributes.MaxObjects","1000");
props.put("jcs.default.cacheattributes.MemoryCacheName", "org.apache.jcs.engine.memory.lru.LRUMemoryCache");
props.put("jcs.default.cacheattributes.UseMemoryShrinker", "true");
props.put("jcs.default.cacheattributes.MaxMemoryIdleTimeSeconds", "3600");
props.put("jcs.default.cacheattributes.ShrinkerIntervalSeconds", "60");
props.put("jcs.default.cacheattributes.MaxSpoolPerRun", "500");

//
// Region cache
//
props.put("jcs.region.myregionCache", "");
props.put("jcs.region.myregionCache.cacheattributes", "org.apache.jcs.engine.CompositeCacheAttributes");
props.put("jcs.region.myregionCache.cacheattributes.MaxObjects", "1000");
props.put("jcs.region.myregionCache.cacheattributes.MemoryCacheName", "org.apache.jcs.engine.memory.lru.LRUMemoryCache");
props.put("jcs.region.myregionCache.cacheattributes.UseMemoryShrinker", "true");
props.put("jcs.region.myregionCache.cacheattributes.MaxMemoryIdleTimeSeconds", "3600");
props.put("jcs.region.myregionCache.cacheattributes.ShrinkerIntervalSeconds", "60");
props.put("jcs.region.myregionCache.cacheattributes.MaxSpoolPerRun", "500");
props.put("jcs.region.myregionCache.cacheattributes.DiskUsagePatternName", "UPDATE");
props.put("jcs.region.myregionCache.elementattributes", "org.apache.jcs.engine.ElementAttributes");
props.put("jcs.region.myregionCache.elementattributes.IsEternal", "false");

...


// Configure
CompositeCacheManager ccm = CompositeCacheManager.getUnconfiguredInstance();
ccm.configure(props);

// Access region
CompositeCache myregionCache =  ccm.getCache("myregionCache");

...
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top