Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Questions - CDI Dependency and Disable Caching for testing with in-memory db #151
Thanks for the great work.
I am using Caffeine JCache provider with Play in Scala with Guice as the DI framework. Two things I would like to clarify:
I get a compilation error:
I have to add "org.jsr107.ri" % "cache-annotations-ri-cdi" % "1.0.0" or "javax.enterprise" % "cdi-api" % "2.0-PFD2" for the compilation to work. Is this expected, or is there a missing dependency somewhere?
For testing, I don't want to enable Caffeine, because I am using some in-memory database, and enabling caching may affect some of the test results. What I did is this in my test module.
Somehow this works, but I am not sure what CacheManager is returned by the above. Is there a default implementation provided by the reference implementation?
Thanks for your patience.
The Guice RI should work. Scala requires annotations to fully resolve, whereas Java ignores them unless reflective resolved, so there may be an ri compile time dependency you have to promote to runtime. I'd check the ri pom.
This Guice test might be helpful.
JCache uses a ServiceLoader to discover the implementation. In your test, it's resolving to Caffeine again.
I'd be weary of jcache outside of framework integrations. It has a brittle, limited api with lots of quirks. I don't think it was a positive step forward.
Thanks for the fast response.
I did a dependency check and found javax.cache.annotation.CacheResult annotation uses javax.enterprise.util.Nonbinding
But javax.enterprise.util.Nonbinding is not in the dependency chain.
I think this is where I get the configureCachingProvider code from... However, what I need is to plug in a dummy implementation. I guess I can write one myself...
You mean it is not recommended to use JCache annotations outside of frameworks? I thought using such annotations is an easy way to enable caching instead of doing all the explicit gets and puts in the code... If not then what is a good way to enable caching without polluting my code with such cross-cutting concern?
Nice find. In their POM it states,
<!--This is only needed if you are using a CDI based implementation of the annotations support. In CDI 1.1. we should be able to remove this dependency completely. --> <dependency> <groupId>javax.enterprise</groupId> <artifactId>cdi-api</artifactId> <version>1.0-SP4</version> <scope>provided</scope> <optional>true</optional> ... </dependency>
You could request that they remove the dependency usage since CDI 1.1 was released 4 years ago.
It is my opinion that JCache does more harm than good, but you are welcome to disagree. The only value I see is to ease framework integrations, but it is highly flawed for application developers.
The annotations are quirky and interact poorly with the rest of JCache. For example,
You'll discover more of these quirks as you become familiar with JCache and your use cases become modestly complex. You might then prefer to use Caffeine's apis directly which are fairly succinct and, hopefully, provide the right escape hatches to not get in your way. But you can definitely start with JCache, evaluate, and migrate if needed.