I have been implementing some caching in my project using EhCache
. I have added following dependencies to my pom.xml
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.3.1</version>
</dependency>
Note the third dependency which is for EhCache
. In all the tutorials I found online, the group id is different. The reason for me to change the group id was it had been moved to org.ehcache
.
I have the following ehcache.xml file in my classpath.
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true">
<diskStore path="java.io.tmpdir"/>
<cache name="cardTypes"
maxEntriesLocalHeap="100"
maxEntriesLocalDisk="1000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
Following is my configuration file.
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.core.io.ClassPathResource;
@EnableCaching
@Configuration
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
return new EhCacheCacheManager(ehCacheCacheManager().getObject());
}
@Bean
public EhCacheManagerFactoryBean ehCacheCacheManager() {
EhCacheManagerFactoryBean factory = new EhCacheManagerFactoryBean();
factory.setConfigLocation(new ClassPathResource("ehcache.xml"));
factory.setShared(true);
return factory;
}
}
Now, I am getting an error in following line.
return new EhCacheCacheManager(ehCacheCacheManager().getObject());
That is:
In Eclipse:-
The type net.sf.ehcache.CacheManager cannot be resolved. It is indirectly referenced from required .class files
In JIdea:
Error:(21, 71) java: cannot access net.sf.ehcache.CacheManager class file for net.sf.ehcache.CacheManager not found
I have not added any net.sf.ehcache stuff into my project. As I mentioned before, the ehcache
has been moved to org.ehcache
.
Why I am getting this error? It is related to net.sf.ehcache
which is not a part of the dependencies I have added.
Should I code the second bean in a different way since the artifact has been moved? or how can I get this resolved?
Ehcache 2 is in the net.sf.ehcache
package. Most tutorial are about it since it had a long and useful life. Ehcache 3, the version you configured, is quite new (but of course better) and in the org.ehcache
package.
The package moved because having its own domain was nicer but also because the new version is quite different and be able to cohabit with the old version can be necessary (because of some framework using it).
Your error comes from the fact that EhCacheCacheManager
is using Ehcache 2. Ehcache 3 doesn't need it because it is JCache compliant. So you can use JCacheCacheManager
instead.
So, right now, you have the Spring wiring and an ehcache.xml
for Ehcache 2. And a dependency to Ehcache 3. You should align them to fix your problem.
To use Ehcache 3, the easiest is to add this in your application.properties
spring.cache.jcache.config=ehcache.xml
And this:
@EnableCaching
@Configuration
public class CacheConfig {
}
And that's it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With