Building "isolated" and "automatically updated" caches (java.util.List) in Java.

Posted by Aidos on Stack Overflow See other posts from Stack Overflow or by Aidos
Published on 2010-04-10T15:15:45Z Indexed on 2010/04/10 15:23 UTC
Read the original article Hit count: 252

Hi Guys,

I am trying to write a framework which contains a lot of short-lived caches created from a long-living cache. These short-lived caches need to be able to return their entier contents, which is a clone from the original long-living cache.

Effectively what I am trying to build is a level of transaction isolation for the short-lived caches. The user should be able to modify the contents of the short-lived cache, but changes to the long-living cache should not be propogated through (there is also a case where the changes should be pushed through, depending on the Cache type).

I will do my best to try and explain:

master-cache contains: [A,B,C,D,E,F] temporary-cache created with state [A,B,C,D,E,F]

1) temporary-cache adds item G: [A,B,C,D,E,F] 2) temporary-cache removes item B: [A,C,D,E,F]

master-cache contains: [A,B,C,D,E,F]

3) master-cache adds items [X,Y,Z]: [A,B,C,D,E,F,X,Y,Z]

temporary-cache contains: [A,C,D,E,F]

Things get even harder when the values in the items can change and shouldn't always be updated (so I can't even share the underlying object instances, I need to use clones).

I have implemented the simple approach of just creating a new instance of the List using the standard Collection constructor on ArrayList, however when you get out to about 200,000 items the system just runs out of memory. I know the value of 200,000 is excessive to iterate, but I am trying to stress my code a bit.

I had thought that it might be able to somehow "proxy" the list, so the temporary-cache uses the master-cache, and stores all of it's changes (effectively a Memento for the change), however that quickly becomes a nightmare when you want to iterate the temporary-cache, or retrieve an item at a specific index. Also given that I want some modifications to the contents of the list to come through (depending on the type of the temporary-cache, whether it is "auto-update" or not) and I get completly out of my depth.

Any pointers to techniques or data-structures or just general concepts to try and research will be greatly appreciated.

Cheers,

Aidos

© Stack Overflow or respective owner

Related posts about java

Related posts about caching