Use Case
Imagine you have some Java code that does lots and lots of computation. All the time intensive calculations is performed by the class SlowCalculator which implements the interface Calculator:
public static interface Calculator {
public String calculate(int a, String b);
}
public static void main(String[] args) {
Calculator c = new SlowCalculator();
// call c.calculate() a lot of times here...
}
You notice that calculate() is often called with the same parameters which lead to the exact same result (SlowCalculator is stateless). This means it is possible to cache values so there’s no need to recompute. Using the generic CachingProxy™ described below, you can create a cached proxy for any class with just one single line of code:
// ...
public static void main(String[] args) {
Calculator c = new SlowCalculator();
c = CachedProxy.create(Calculator.class, c);
// call c.calculate() a lot of times here...
}
That’s it, and the application is blazingly fast again.
UPDATE: Support for null values, transparently handles exceptions, better hash, nullpointer-bugfix.
UPDATE: Here is an article “Memoization in Java Using Dynamic Proxy Classes” that does (almost) exactly the same as this code.

