Friday, April 01, 2005

AOP and caching

I was recently developing a new AJAX based system for picking a drug from a long list of potentials (this sounds a lot more fun/dodgy/illegal than it really was). Specifically, there are two drop down lists - picking an element from the first select list makes the page look up the entry from the server.

In itself, this is rather unexciting, but i got a chance to code in what is IMHO a rather elegant manner:

I used JSON for the javascript-server client communication, and spring+hibernate for all server side interaction. Now, at this point, we are still talking a decent implementation. The part that I really liked was using AOP for method level caching.

Since the response time of the system needs to be as close to seamless as possible, I needed to make sure that round-trips to the database happened as infrequently as possible. There are, of course, two ways this can be accomplished: the first would be to code a caching mechanism at the service layer, and the second was to use AOP to put it in declaritavly after the rest of the code worked.

I looked around for a few minutes on the spring web site and found a couple of EHCache based method caches, stuck them into my working application, and presto: the time delay that had been due to database look-up was entirely removed. It actually seemed faster than normal javascript.

AOP, when used correctly, is a truly beautiful technology. On a side note, a friend and I did some performance testing of reflection in JDK 1.4 and 1.5. End result: don't worry about it, unless you are in a endless loop. I will post more about that later.

No comments: