Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » J2EE Interest »

Cache loose coupling best practice?

Erik Beijnoff

2003-09-30

Replies:

I'm building a cache and are facing some problems on how to create cache
interrelations and need advice on best practices on the subject.

Say that I have one separate cache that holds people and one that holds
documents. The objects in the people cache can hold a list of references
to the objects in the document cache. From time to time, some of the
objects in both caches are cleared out to release memory.

My problem is this: Even if I remove some documents from the cache they
may be referenced from some people objects, preventing the document
object from being garbage collected, despite that it's been cleared from
the cache.

This MAY be okay, since eventually the people also will be cleared
thereby releasing all references. But two problems occur:
1. The document object may be reread into the cache, thereby creating
two in-memory representations of the document, leaving some people
objects with references to document objects representations that are
invalid.
2. And the more serious problem: If I use an object pool (which I do) to
recycle the document objects to prevent unneccessary creation and
destruction of objects, objects that are returned to the pool and then
recycled may have invalid references to them, thereby creating a corrupt
system state.

The one solution that I've managed to come up with is having loose
bindings between the caches. Instead of referencing the documents
directly, the people objects holds an array of unique identifier
references to the document cache. So that whenever acccess is needed to
a document, the document gets fetched from the cache using the unique
identifier. This identifier will propably be the integer key that the
documents are stored under in the cache.

But I do not feel too comfortable with the approach since I have a
feeling that fetching, say a 1000 documents by reference will be
magnitudes faster than fetching each document with an identifier from
the cache. If some better standard solution exists, I'd be very
interested to hear about it.

Erik Beijnoff
Addsystems

erik@(protected)
erik@(protected)

Åsögatan 198
116 32 Stockholm
Sweden

===========================================================================
To unsubscribe, send email to listserv@(protected)
of the message "signoff J2EE-INTEREST". For general help, send email to
listserv@(protected)".


©2008 junlu.com - Jax Systems, LLC, U.S.A.