Shahzad Bhatti Welcome to my ramblings and rants!

August 7, 2007

Annotation based Caching in Java

Filed under: Computing — admin @ 3:33 pm

I probably had to write cache to store results of expensive operations or queries a dozens of times over last ten years of Java. And this week, I had to redo again. Though, there are a lot of libraries such as JCS , Terracota or Tangosol, but I just decided to write a simple implementation myself.

First, I defined an annotation that will be used to mark any class or methods cachable:

Java
I then defined a simple map class to store cache:

Java
A couple of interfaces to load or search key/values:

Java
Java
Following is a simple class to convert method and its args into a unique key (I suppose we can use MD5 SHA1 instead here):

Java

A utility class for storing pair of objects:

Java

Following code adds support for parsing cache annotation:

Java

Instead of Java 1.3 proxy, we can also use aspects, e.g.

Java
 And finally a unit test to test everything:

Java

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URL

Sorry, the comment form is closed at this time.

Powered by WordPress