2012-05-09 31 views
0

我使用Spring @Cacheable注解Hazelcast 2.1和 Spring 3.1。从Hazelcast中的Map中返回一个克隆的对象

@Cacheable("testCache") 
public MyObject testMethod(int testParam); 

//After method call 
MyObject test = Hazelcast.getMap("testCache").get("key") 
test.setSomeProp() //This line causes an update to the cache since it is reference. 

是否有可能从地图,而不是参考从Hazelcast.getMap()返回缓存值的克隆/复制?

即我想要一个像EhCache中的copyOnRead功能。请参阅EhCache Documentation

回答

2

如果您不使用靠近缓存并禁用缓存值。例如:

<hz:map name="map" 
      backup-count="1" 
      max-size="0" 
      read-backup-data="true" 
      cache-value="false"/> 

然后Hazelcast将永远返回您实际值的副本,无论如何。

如果您保持cache-value = true,则Hazelcast将缓存该值的对象版本,并将在本地读取中返回相同的副本。通过本地读取,我的意思是读取的成员被启动并且密钥的所有者是相同的。

+0

Thanks @Fuad。这就是我确实需要的。我注意到,在你的答案后文档中有一部分关于“缓存值”,但它在另一章(Common Gotchas)中。在相关的章节中看到这种配置的东西真是太好了,以至于它们可以更容易找到 – emrahkocaman