views:

202

answers:

1

Hi,

I am planning to implement a cache solution into an existing web app. Nothing complicated: basically a concurrent map that supports overflowing to disk and automatic eviction. Clustering the cache could be requirement in the future, but not now.

I like ehcache's copyOnRead and copyOnWrite features, because it means that I don't have to manually clone things before modifying something I take out of the cache. Now I have started to look at Infinispan, but I have not found anything equivalent there. Does it exist?

I.e., the following unit tests should pass:

@Test
public void testCopyOnWrite() {
    Date date = new Date(0);
    cache.put(0, date);
    date.setTime(1000);
    date = cache.get(0);
    assertEquals(0, date.getTime());
}

@Test
public void testCopyOnRead() {
    Date date = new Date(0);
    cache.put(0, date);
    assertNotSame(cache.get(0), cache.get(0));
}
+1  A: 

According to a JBoss developer, Infinispan does not yet support such feature. You should log a request for enhancement in the Infinispan issue tracker, so that others may vote on it (I will).

That being said, if you need this feature now, a workaround would be to extend AbstractDelegatingCache, and override the get and put methods to add this functionality. You could use your own copy strategy or look at how EHCache did it for inspiration.

Also, you may consider the Infinispan forum if you have further questions, since you will have more views from the Infinispan community.

eneveu
Thank you. I believe that copying an object graph correctly is a difficult problem (in the general case), which is why I didn't want to do it myself. I will take a look at how ehcache does it, like you suggest, but I think I will end up just manually copying what I take out of the cache before modifying it.
waxwing
I think the default strategy used by EHCache to copy objects is to use Java's serialization mechanism... (serialize the object, then deserialize it: you get a new instance). Of course, it's slow, and it only works if the objects are serializable (but it's often the case with cached objects, since you often want to write them on the disk...). But you are right, copying object graph is a difficult problem, I've used Dozer in the past, you may also look at: http://stackoverflow.com/questions/1432764/any-tool-for-java-object-to-object-mapping
eneveu
Found it: http://svn.terracotta.org/svn/ehcache/trunk/core/src/main/java/net/sf/ehcache/store/compound/SerializationCopyStrategy.java
eneveu
Awesome, this pretty much takes away one of the advantages of ehcache as I see it.
waxwing