Hi again Daniel!

Daniel Hoppe wrote:
> Actually I would generally like to have some kind of mixed strategy between
> longest interim and fewest number of total hits which should to some degree
> be configurable. I need this for a content management system where the page
> generation is computation intensive, and I am not too sure yet which caching
> strategy would be the best fit, so if I can configure it I can still play
> around with the settings during load testing.

Again, very interesting. May I suggest to include a "expiration date"
within each object, maybe obtained via a method in the interface they
will implement, after which the object is no longer valid.

Quite often, you want to keep a cache of results from a DB, but only
valid for a specific period of time; you might consider that, after a
minute, some administrator may have changed it; or that the DB is being
updated with live data every 2 seconds. I hope you get the point in
spite of my horrible explanation.

> My problem is that my
> implementation for the longest interim is quite inefficient (I keep the keys
> in a LinkedList where they are  (re)inserted at the first position on
> access, the times I profiled for this  (esp. linkedList.remove()) makes me
> doubt that my approach is smashing so far...). Dropping the oldest value
> object is straightforward and fast as I do never need to reorder the keys.
> Better approaches on this are more than welcome!

Are there more accesses than "garbage collections" (dropping unused
objects)? Assuming it is so, wouldn't it be faster to switch the load to
the dropping process?

It might be done with something like a TreeSet: you add() the objects
you create, and remove() the ones you must drop -- according to the
javadoc, they are guaranteed to be log(n), i.e. fast.

The delay must then occur when you ask for an iterator(): the TreeSet is
sorted, and the resulting Iterator will traverse them in ascending
order. (You sort them according to the time of the last use.) You just
drop and remove() the first few, and leave the rest alone. After that,
you just update the time of the last use when appropriate.

What do you think? Me, I like TreeSets :)

Un saludo,

Alex.

> Depending on the configuration of the cache I either directly keep the value
> object or put it into a Reference (see
> http://www.javasoft.com/j2se/1.3/docs/api/java/lang/ref/SoftReference.html).

Thanks a lot, didn't even knew those classes existed! A whole world
opens before me, of messy objects that may no longer be there! Now that
I thought I had finally got rid of C++! :)

Un saludo,

Alex.

Reply via email to