Java Cache Tutorial with Cache Dependency Injection (CDI)

From Resin 4.0 Wiki

(Difference between revisions)
Jump to: navigation, search
(Created page with "{{Caching}} {{Cookbook}} <web-app xmlns="http://caucho.com/ns/resin" xmlns:ee="urn:java:ee" xmlns:resin="urn:java:com.caucho.resin"> <resin:ClusterCac...")
 
Line 22: Line 22:
 
     if (cachedValue == null) {
 
     if (cachedValue == null) {
 
       cachedValue = doLongCalculation(key);
 
       cachedValue = doLongCalculation(key);
 
+
 
       _cache.put(key, cachedValue);
 
       _cache.put(key, cachedValue);
 
     }
 
     }

Revision as of 00:00, 5 January 2012

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:ClusterCache ee:Named="my-cache"/>

</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