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: