Try @Cacheable from jcabi-aspects. With a single annotation you make the entire method result cacheable in memory:
public class Resource {
@Cacheable(lifetime = 5, unit = TimeUnit.SECONDS)
public String load(URL url) {
return url.openConnection().getContent();
}
}
Also, read this article: http://www.yegor256.com/2014/08/03/cacheable-java-annotation.html