Java Cache Tutorial with Cache Dependency Injection (CDI)
From Resin 4.0 Wiki
(Difference between revisions)
Line 5: | Line 5: | ||
xmlns:resin="urn:java:com.caucho.resin"> | xmlns:resin="urn:java:com.caucho.resin"> | ||
− | <resin: | + | <resin:LocalCache ee:Named="my-cache"> |
− | + | <name>my-cache</name> | |
+ | </resin:LocalCache> | ||
+ | |||
</web-app> | </web-app> | ||
Revision as of 00:00, 5 January 2012
<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; } }