1
我在應用程序中使用EhCache,並且偶然發現了一個問題。 我需要緩存「原始」數據(地圖樹和一些列表)。從緩存中檢索後的緩存值意味着要進一步處理(某些元素被過濾掉,重新排序等)。將EhCache對象存儲爲值不參考?
我的問題是,我想保持原來的緩存值不變 - 因爲它意味着用於一些「後處理」。所以最終我想存儲對象「價值/深層克隆」而不是它的參考。
一個例子代碼:
//create a List of Maps
List list = new ArrayList();
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "v1");
map.put("key2", "v2");
list.add(map);
//add to cache
cache.put("cacheRegion", "list", list);
//now add a new element to list (2nd map)
list.add(new TreeMap());
//now remove 1 entry from the 1st Map
map = (Map<String, String>) list.get(0);
map.remove("key1");
list = (List) cache.get("cacheRegion", "list");
assertEquals("list should still have 1 element, despite adding new map after cache put", 1, list.size());
//check map
map = (Map<String, String>) list.get(0);
assertEquals("map should still contain 2 entries, as it was added to the cache", 2, map.size());
確實的Ehcache支持了嗎?
中號