Imagine having a tool that can automatically detect JPA and Hibernate performance issues.
Hypersistence Optimizer is that tool!
Introduction
Memoization is a method-level caching technique for speeding-up consecutive invocations.
This post will demonstrate how you can achieve request-level repeatable reads for any data source, using Spring AOP only.
Spring Caching
Spring offers a very useful caching abstracting, allowing you do decouple the application logic from the caching implementation details.
Spring Caching uses an application-level scope, so for a request-only memoization we need to take a DIY approach.
Request-level Caching
A request-level cache entry life-cycle is always bound to the current request scope. Such cache is very similar to Hibernate Persistence Context that offers session-level repeatable reads.
First we are going to define a Memoizing marker annotation:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Memoize {
}
This annotation is going to explicitly mark all methods that need to be memoized.
To distinguish different method invocations we are going to encapsulate the method call info into the following object type:
public class InvocationContext {
public static final String TEMPLATE = "%s.%s(%s)";
private final Class targetClass;
private final String targetMethod;
private final Object[] args;
public InvocationContext(Class targetClass, String targetMethod, Object[] args) {
this.targetClass = targetClass;
this.targetMethod = targetMethod;
this.args = args;
}
public Class getTargetClass() {
return targetClass;
}
public String getTargetMethod() {
return targetMethod;
}
public Object[] getArgs() {
return args;
}
@Override
public boolean equals(Object that) {
return EqualsBuilder.reflectionEquals(this, that);
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
@Override
public String toString() {
return String.format(TEMPLATE, targetClass.getName(), targetMethod, Arrays.toString(args));
}
}
Few know about the awesomeness of Spring Request/Session bean scopes.
Because we require a request-level memoization scope, we can simplify our design with a Spring request scope that hides the actual HttpSession resolving logic:
@Component
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "request")
public class RequestScopeCache {
public static final Object NONE = new Object();
private final Map<InvocationContext, Object> cache = new HashMap<InvocationContext, Object>();
public Object get(InvocationContext invocationContext) {
return cache.containsKey(invocationContext) ? cache.get(invocationContext) : NONE;
}
public void put(InvocationContext methodInvocation, Object result) {
cache.put(methodInvocation, result);
}
}
Since a mere annotation means nothing without a runtime processing engine, we must, therefore, define a Spring Aspect implementing the actual memoization logic:
@Aspect
public class MemoizerAspect {
@Autowired
private RequestScopeCache requestScopeCache;
@Around("@annotation(com.vladmihalcea.cache.Memoize)")
public Object memoize(ProceedingJoinPoint pjp) throws Throwable {
InvocationContext invocationContext = new InvocationContext(
pjp.getSignature().getDeclaringType(),
pjp.getSignature().getName(),
pjp.getArgs()
);
Object result = requestScopeCache.get(invocationContext);
if (RequestScopeCache.NONE == result) {
result = pjp.proceed();
LOGGER.info("Memoizing result {}, for method invocation: {}", result, invocationContext);
requestScopeCache.put(invocationContext, result);
} else {
LOGGER.info("Using memoized result: {}, for method invocation: {}", result, invocationContext);
}
return result;
}
}
Testing time
Let’s put all this to a test. For simplicity sake, we are going to emulate the request-level scope memoization requirements with a Fibonacci number calculator:
@Component
public class FibonacciServiceImpl implements FibonacciService {
@Autowired
private ApplicationContext applicationContext;
private FibonacciService fibonacciService;
@PostConstruct
private void init() {
fibonacciService = applicationContext.getBean(FibonacciService.class);
}
@Memoize
public int compute(int i) {
LOGGER.info("Calculate fibonacci for number {}", i);
if (i == 0 || i == 1)
return i;
return fibonacciService.compute(i - 2) + fibonacciService.compute(i - 1);
}
}
The Spring application context used for this test looks as follows:
If we are to calculate the 10th Fibonnaci number, we’ll get the following result:
Calculate fibonacci for number 10
Calculate fibonacci for number 8
Calculate fibonacci for number 6
Calculate fibonacci for number 4
Calculate fibonacci for number 2
Calculate fibonacci for number 0
Memoizing result 0, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([0])
Calculate fibonacci for number 1
Memoizing result 1, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([1])
Memoizing result 1, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([2])
Calculate fibonacci for number 3
Using memoized result: 1, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([1])
Using memoized result: 1, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([2])
Memoizing result 2, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([3])
Memoizing result 3, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([4])
Calculate fibonacci for number 5
Using memoized result: 2, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([3])
Using memoized result: 3, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([4])
Memoizing result 5, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([5])
Memoizing result 8, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([6])
Calculate fibonacci for number 7
Using memoized result: 5, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([5])
Using memoized result: 8, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([6])
Memoizing result 13, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([7])
Memoizing result 21, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([8])
Calculate fibonacci for number 9
Using memoized result: 13, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([7])
Using memoized result: 21, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([8])
Memoizing result 34, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([9])
Memoizing result 55, for method invocation: com.vladmihalcea.cache.FibonacciService.compute([10])
Online Workshops
If you enjoyed this article, I bet you are going to love my upcoming Online Workshops.