Monday, January 31, 2011

Simple Caching using Guice

I am at Google now so I decided to use Guice as the framework for my dependency injections needs. I am a big fan of Spring so I was a little concerned that Guice couldn't live up to my expectations. However, after having used it for the last 6 months or so I have to say that I am a fan. Gone are those long spring XML configuration files and replaced with Guice modules.

Recently, while working on my current project we found that some requests against our server were slow. Using my Guice-ified logging framework (maybe another post?) I was able to determine that we were making way too many queries against our persistence layer. Just looking at the queries we could see that a lot of the queries were exactly the same. Immediately, this seems like a problem that could be solved by some caching. Since the main problem was too many persistence store queries per request, I thought that a simple request scoped cache could go a long way and Guice allowed me to do this very easily. The advantage of making this a request scoped cache I really don't need to worry too much about invalidation or thread safety.

The first thing I needed to do was to determine which persistence calls I want to cache. My persistence class is below and I have annotated the cacheable methods with @Cache in addition I have annotated the class with @Cacheable.

public class {

@Cache(queryType = QueryType.LOOKUP_BY_KEY)
public Object lookup(Key key)
  // Does lookup against persistence store and returns result.

public Object save(Object resource)
  // Saves to the persistence store.

The annotations are defined as follows:

@Target({ElementType.PARAMETER, ElementType.TYPE})
public @interface Cacheable {
@Target({ElementType.PARAMETER, ElementType.METHOD})
public @interface PersistenceContextCache {
   * Used to give the interceptor a hint on how to cache the
   * result of the intercepted method.
  public enum QueryType {

  QueryType queryType();

Because I want to be able easily disable my caching layer and not let my caching logic invade my persistence code, I am using an interceptor to implement my caching logic.

 * Intercepts calls to the persistence tier and checks if we
 * already have the result in the cache. If it is in the cache 
 * then the call to the persistence tier is skipped and the
 * cached result is returned. Otherwise, we call through and  
 * cache the result.
public class CacheInterceptor implements MethodInterceptor {
   * Provides the cache which is request scoped so we don't have
   * to concern ourselves with thread safety.
  private Provider<Cache<CacheKey, Object>> cacheProvider;
  public Object invoke(MethodInvocation invocation) 
      throws Throwable {
    // Construct the cache key
    Cache cacheAnnotation =
    CacheKey cacheKey = getCacheKey(
    Object result = cacheProvider.get().getElement(cacheKey);
    // If the result is null, then we have a cache miss so call 
    // through
    if (null == result) {
      result = invocation.proceed();

      // Now add it to the cache
          cacheKey, result != null ? 
              result : new NullCacheEntry());
    return result instanceof NullCacheEntry ? null : result;

Now let's wire things together in a Guice module.

public class CacheModule extends AbstractModule {
  protected void configure() {
    CacheInterceptor cacheInterceptor = new CacheInterceptor();
    // Intercept any calls to methods annotated with @Cache on
    // a class annotated with @Cacheable.
  SoftHashMap<CacheKey, Object> provideCache() {
    return new SoftHaspMap<CacheKey, Object>();

Some things to note in the above code:
  • Since my interceptor is essentially a singleton but my cache is request scoped, I need to use a provider which allows me to mix scopes.
  • I did not show my implementation of getCacheKey. However, it should be straight-forward to see how you can come up with a Cache Key based on the data I pass in.
  • I did not include my cache invalidation code but you should be able to see that you could simply add another interceptor to clear the cache on any write.
  • I use a NullCacheEntry object to represent a "not found" resource since a Map does not allow for null values.
  • My cache is actually a SoftHashMap which I do not show the implementation of.  The idea is we want to be safe and not let our cache get too big and cause an OutOfMemoryException so these SoftReferences should be cleaned up if the memory gets low.


  1. Thanks for sharing. Nice how straightforward this stuff is with Guice. I was wondering about your remark that you found requests were slow through your logging framework. I was about to build some code a while ago that would probably do something similar, but decided against it because I didn't think it would give me much benefit over just using a profiler. Is there any particular reason you decided to not use a profiler but instead on logging and custom frameworking?

  2. The logging framework that I put in place did a lot of things like logging the request/response across all my APIs (it was also a Guice interceptor). I added some timing data to the logs so I could still have access to this data easily when running on production machines and have monitoring that would notifying me when the numbers seemed off.

    There are several ways to accomplish this but my approach seemed the most straight-forward for me at the time. Really just a personal preference thing.