Java Cache Tutorial with Cache Dependency Injection (CDI)

From Resin 4.0 Wiki

Revision as of 00:00, 5 January 2012 by Ferg (Talk | contribs)
Jump to: navigation, search

Squirrel-48.pngCookbook-48.png

<web-app xmlns="http://caucho.com/ns/resin"
        xmlns:ee="urn:java:ee"
        xmlns:resin="urn:java:com.caucho.resin">

  <resin:LocalCache ee:Named="my-cache">
    <name>my-cache</name>
  </resin:LocalCache>
 
</web-app>
import javax.inject.Inject;
import javax.cache.Cache;

public class MyService {
  @Inject @Named("my-cache")
  private Cache<String,String> _cache;

  public String doStuff(String key)
  {
    String cachedValue = _cache.get(key);

    if (cachedValue == null) {
      cachedValue = doLongCalculation(key);

      _cache.put(key, cachedValue);
    }

    return cachedValue;
  }
}
Personal tools
TOOLBOX
LANGUAGES