J'ai implémenté de la mise en cache dans mon projet en utilisant EhCache
. J'ai ajouté les dépendances suivantes à mon pom.xmlimpossible d'accéder à net.sf.ehcache.CacheManager, fichier de classe pour net.sf.ehcache.CacheManager non trouvé
<!-- 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>
Notez la troisième dépendance qui est pour EhCache
. Dans tous les didacticiels que j'ai trouvés en ligne, l'identifiant du groupe est différent. La raison pour moi de changer l'identifiant du groupe était qu'il avait été déplacé à org.ehcache
. J'ai le fichier ehcache.xml suivant dans mon 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>
Voici mon fichier de configuration.
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;
}
}
Maintenant, je reçois une erreur dans la ligne suivante.
return new EhCacheCacheManager(ehCacheCacheManager().getObject());
C'est:
Dans Eclipse: -
Le type net.sf.ehcache.CacheManager ne peut pas être résolu. Il est indirectement référencé à partir requis .class
En JIdea:
Error:(21, 71) java: cannot access net.sf.ehcache.CacheManager class file for net.sf.ehcache.CacheManager not found
Je n'ai pas ajouté de choses net.sf.ehcache dans mon projet. Comme je l'ai déjà mentionné, le ehcache
a été déplacé au org.ehcache
.
Pourquoi cette erreur se produit-elle? Il est lié à net.sf.ehcache
qui ne fait pas partie des dépendances que j'ai ajoutées.
Dois-je coder le second grain d'une manière différente depuis que l'artefact a été déplacé? ou comment puis-je résoudre ce problème?
Merci beaucoup. Après avoir essayé plusieurs choses, la solution que j'ai trouvée était d'utiliser le vieux Ehcache 2. Cela a résolu le problème. Cependant, c'est aussi une solution. J'accepte ceci. :) – vigamage
merci ;-) Comme je l'ai dit, déplacer la dépendance à ehcache 2 fonctionne bien sûr :-) – Henri