2012-10-06 34 views
2

我們計劃在我們的應用程序中使用一些高速緩存機制,並在執行許多其他高速緩存解決方案之間的比較研究之後選擇Java高速緩存系統(JCS)。當我使用外部配置(cache.ccf)來定義緩存區域及其屬性(如maxlife,ideltime等)時,每件事情都很好。Java高速緩存系統(JCS) - 以編程方式創建地區

但是需求被改變爲具有動態緩存區域,也就是說我們需要在運行時定義緩存區域及其屬性。我無法找到關於此操作的更多細節或樣本。 我在運行時成功創建了緩存區域(使用下面的方法簽名)。

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

但問題是,IElmentAttributes沒有設置爲緩存。我研究了JCS的來源,發現attr從未設置。這是不使用的論點!有點奇怪

一些google搜索後,我發現下面的選項手動設置屬性,但仍然沒有工作

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

所有我想要的是設置爲maxLifeSeconds創建區域。任何幫助將非常感激。

回答

2

我找到了解決問題的方法,我們需要在將數據放入緩存時設置屬性。見有人誰感興趣的實施,

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

示例代碼這裏

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

...