Java缓存系统(JCS) - 以编程方式创建区域

时间:2012-10-06 16:58:14

标签: caching jcs

我们计划在我们的应用程序中使用一些缓存机制,并在执行许多其他缓存解决方案之间的比较研究后选择Java缓存系统(JCS)。当我使用外部配置(cache.ccf)来定义缓存区域及其属性(如maxlife,ideltime等)时,一切都很好。

但是需求被更改为具有动态缓存区域,即我们需要在运行时定义缓存区域及其属性。我无法找到有关此操作的更多详细信息或示例。 我在运行时成功创建了缓存区域(使用下面的方法签名)。

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

但问题是,IElmentAttributes没有设置缓存。我研究了JCS的来源,发现attr从未设置过。这是非使用的论点!!有点奇怪

经过一些谷歌搜索,我发现下面的选项手动设置属性,但仍然无法正常工作

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

我想要的只是为创建的区域设置maxLifeSeconds。任何帮助都会非常感激。

2 个答案:

答案 0 :(得分:2)

我找到了解决问题的方法,我们需要在将数据放入缓存时设置属性。请参阅感兴趣的人的实施,

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

答案 1 :(得分:2)

示例代码

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");

...
相关问题