AOP, Annotation-based caching solution for Guice projects…

I just released a new version of the visural-common library (0.3.1) which includes support for a simple, flexible annotation based cache, for non-distributed projects.

Why non-distributed projects?

Unlike most other caching libraries I don’t offer integrations with ehcache or other high-end caching solutions. This is an all-in-one-solution, which offers in-instance caching.

By that I mean caching that is scoped only with an object instance. This is particularly useful in situations where you have code which is scoped in “request” or “session” scope with Guice. It may not be appropriate to cache across all instances (like with a “service pattern”).

Anyhow, here’s an example of what this solution looks like:

    @Cache(maxEntries=10000, timeToLive=5000)
    public myExpensiveOperation(Object arg1, Object arg2) {
        // do some expensive stuff, e.g. database read
    }

To enable it you need to implement the “Cacheable” interface. There are 3 basic ways to do this:


class CacheService extends AbstractCacheable {
    // .....
}

class CacheService implements Cacheable {

    private transient CacheData data;

    public CacheData __cacheData() {
        return data;
    }

    @Inject
    public void __cacheData(CacheData data) {
        this.data = data;
    }

    // .....
}

public class CacheService implements Cacheable {

    private final CacheData data;

    @Inject
    protected CacheService(CacheData data) {
        this.data = data;
    }

    public CacheData __cacheData() {
        return data;
    }

    // .....
}

If you’re already using Guice, it’s a pretty simple, low dependency and easy to configure solution for caching.

For the full details head on over to the visural-common wiki…

Related posts:

  1. Guice / Java IoC best practices – using annotations for configuration
  2. Clustering Guice Java Web Applications
  3. The 5 Minute Guice Primer
  4. 301 Redirects Made Easy In Java
  5. Automatic release packaging w/ version numbers for Netbeans Java Projects…

This entry was posted in Java, Software Engineering and tagged caching, Google, guice, Java, open-source, visural-common. Bookmark the permalink.

Comments are closed.