Search code examples
springdistributed-cachinghazelcast

Return a cloned object from Map in Hazelcast


I'm using Spring @Cacheable annotation with Hazelcast 2.1 and Spring 3.1.

@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.

Is it possible to return a clone/copy of the cached value from map instead of reference from Hazelcast.getMap() ?

Namely I want a copyOnRead functionality like in EhCache. See EhCache Documentation


Solution

  • If you don't use near cache and disable the cache-value. Ex:

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

    then Hazelcast will always return you the copy of the actual value no matter what.

    If you keep cache-value = true then Hazelcast will cache the object version of the value and will return you the same copy on local reads. By local read I mean the member that read is initiated and the owner of the key is same.