Sto cercando di implementare la cache di Spring 3.1 come spiegato here e here, ma non sembra funzionare: il mio metodo viene eseguito ogni volta anche se è contrassegnato con @cacheable . Che cosa sto facendo di sbagliato?Spring 3.1 @Cacheable - metodo ancora eseguito
L'ho spostato in un caso di test di junit con un proprio file di configurazione per isolarlo dal resto della mia applicazione, ma il problema si verifica ancora. Qui ci sono i file rilevanti:
Primavera-test-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml"/>
</beans>
ehcache.xml
<ehcache>
<diskStore path="java.io.tmpdir"/>
<cache name="cache"
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
MyTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-test-servlet.xml"})
@Component
public class MyTest extends TestCase {
@Test
public void testCache1(){
for(int i = 0; i < 5; i++){
System.out.println("Calling someMethod...");
System.out.println(someMethod(0));
}
}
@Cacheable("testmethod")
private int someMethod(int val){
System.out.println("Not from cache");
return 5;
}
}
rilevanti: record Pom: (primavera -version = 3.1.1.RELEASE)
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
quando si esegue il test, Primavera mette fuori alcuni messaggi di debug che assomiglia la cache viene inizializzata senza errori
DEBUG: config.ConfigurationHelper - No CacheManagerEventListenerFactory class specified. Skipping...
DEBUG: ehcache.Cache - No BootstrapCacheLoaderFactory class specified. Skipping...
DEBUG: ehcache.Cache - CacheWriter factory not configured. Skipping...
DEBUG: config.ConfigurationHelper - No CacheExceptionHandlerFactory class specified. Skipping...
DEBUG: store.MemoryStore - Initialized net.sf.ehcache.store.MemoryStore for cache
DEBUG: disk.DiskStorageFactory - Failed to delete file cache.data
DEBUG: disk.DiskStorageFactory - Failed to delete file cache.index
DEBUG: disk.DiskStorageFactory - Matching data file missing (or empty) for index file. Deleting index file /var/folders/qg/xwdvsg6x3mx_z_rcfvq7lc0m0000gn/T/cache.index
DEBUG: disk.DiskStorageFactory - Failed to delete file cache.index
DEBUG: ehcache.Cache - Initialised cache: cache
DEBUG: config.ConfigurationHelper - CacheDecoratorFactory not configured. Skipping for 'cache'.
DEBUG: config.ConfigurationHelper - CacheDecoratorFactory not configured for defaultCache. Skipping for 'cache'.
ma l'output di debug non mostra i controlli della cache tra le chiamate di metodo al SomeMethod e l'istruzione print da inside someMethod prints every time.
C'è qualcosa che mi manca?
In aggiunta a questo, è necessario per estrarre su un'interfaccia in modo che Spring possa scrivere dinamicamente un wrapper per la tua classe. – RockMeetHardplace