2013-04-03 15 views
5

Sto scrivendo una webapp e sto cercando di memorizzare nella cache alcuni pojos che vengono chiamati dal controller.Spring 3.1.1 MVC @Cacheable non viene colpito

Non è stato possibile ottenere alcun risultato quando ho provato a utilizzare org.springframework.cache.annotation.Cacheable, quindi sono passato a com.googlecode.ehcaceh.annotations.Cacheable e non riesco ancora a visualizzarlo cache.

Il mio codice è simile al seguente:

@Controller 
public class Conttroller { 
    @RequestMapping(method = RequestMethod.GET).... 
    public Person getPerson(String id) { 
     LOG.debug("Trying to get resource...); 
     Person person = personService.detect(id); 
     LOG.debug("got person"); 

e il servizio si presenta come:

public class PersonService { 

@Cacheable(cacheName="deviceCache") 
public Person detect(String id) { 
LOG.debug("cache missed") 

mio applicationContext sembra

<context:component-scan base-package="com.mycompany" /> 


     <ehcache:annotation-driven cache-manager="ehCacheManager" /> 
     <bean id="cacheManager"  class="org.springframework.cache.ehcache.EhCacheCacheManager"> 
      <property name="cacheManager"> 
       <ref bean="ehCacheManager" /> 
      </property> 
     </bean> 
     <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="/META-INF/ehcache.xml" /> 

e la mia EHCache assomiglia:

<defaultCache eternal="false" maxElementsInMemory="1000" 
    overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" 
    timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/> 

<cache name="deviceCache" 
    maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false" 
    timeToIdleSeconds="0" timeToLiveSeconds="300" 
    memoryStoreEvictionPolicy="LRU" /> 

Ciò che è interessante è che memorizza nella cache bene nel mio test di unità ...

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration({"classpath:META-INF/applicationContext-test.xml"}) 
public class PersonServiceTest { 

private static final Logger LOG = LoggerFactory.getLogger(PersonServiceTest.class); 
@Test 
public void testCache() { 
    LOG.debug("Getting person..."); 
    Device detect = personService.detect("test"); 
    LOG.debug("Getting person again ..."); 
    detect = personService.detect("test"); 
    LOG.debug("Done"); 
} 

che produce:

13:45:02.885 [main] DEBUG PersonServiceTest - Getting person... 
13:45:02.887 [main] DEBUG c.g.e.a.i.CacheAttributeSourceImpl - Adding CACHE advised method 'detect' with attribute: CacheableAttributeImpl [cacheInstanceResolver=com.googleco[email protected]5c2bfdff, cacheKeyGenerator=HashCodeCacheKeyGenerator [includeMethod=true, includeParameterTypes=true, useReflection=false, checkforCycles=false], parameterMask=ParameterMask [mask=[]]] 
13:45:02.889 [main] DEBUG c.g.e.a.i.EhCacheInterceptor - Generated key '-1043428721379252' for invocation: ReflectiveMethodInvocation: public abstract com.mycompany.Person com.mycompany.PersonServiceImpl.detect(java.lang.String); target is of class [com..mycompany.PersonServiceImpl] 
13:45:02.889 [main] DEBUG PersonServiceTest - Missed 
13:45:02.927 [main] DEBUG PersonServiceTest - Getting person again ... 
13:45:02.927 [main] DEBUG c.g.e.a.i.EhCacheInterceptor - Generated key '-1043428721379252' for invocation: ReflectiveMethodInvocation: 
13:45:02.927 [main] DEBUG PersonServiceTest - Done 

Ma l'uscita della mia guerra (attraversando tomcat7/eclipse wtp) è:

DEBUG net.sf.ehcache.config.ConfigurationFactory Configuring ehcache from InputStream 
DEBUG net.sf.ehcache.config.BeanHandler Ignoring ehcache attribute xmlns:xsi 
DEBUG net.sf.ehcache.config.BeanHandler Ignoring ehcache attribute xsi:noNamespaceSchemaLocation 
DEBUG net.sf.ehcache.util.PropertyUtil propertiesString is null. 
DEBUG net.sf.ehcache.config.ConfigurationHelper No CacheManagerEventListenerFactory class specified. Skipping... 
DEBUG net.sf.ehcache.Cache No BootstrapCacheLoaderFactory class specified. Skipping... 
DEBUG net.sf.ehcache.Cache CacheWriter factory not configured. Skipping... 
DEBUG net.sf.ehcache.config.ConfigurationHelper No CacheExceptionHandlerFactory class specified. Skipping... 
DEBUG net.sf.ehcache.Cache No BootstrapCacheLoaderFactory class specified. Skipping... 
DEBUG net.sf.ehcache.Cache CacheWriter factory not configured. Skipping... 
DEBUG net.sf.ehcache.config.ConfigurationHelper No CacheExceptionHandlerFactory class specified. Skipping... 
DEBUG net.sf.ehcache.store.MemoryStore Initialized net.sf.ehcache.store.NotifyingMemoryStore for deviceCache 
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Pass-Through Statistic: LOCAL_OFFHEAP_SIZE 
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Pass-Through Statistic: LOCAL_OFFHEAP_SIZE_BYTES 
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Pass-Through Statistic: LOCAL_DISK_SIZE 
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Pass-Through Statistic: LOCAL_DISK_SIZE_BYTES 
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Pass-Through Statistic: WRITER_QUEUE_LENGTH 
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Pass-Through Statistic: REMOTE_SIZE 
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: OFFHEAP_GET 
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: OFFHEAP_PUT 
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: OFFHEAP_REMOVE 
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: DISK_GET 
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: DISK_PUT 
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: DISK_REMOVE 
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: XA_COMMIT 
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: XA_ROLLBACK 
DEBUG net.sf.ehcache.statistics.extended.ExtendedStatisticsImpl Mocking Operation Statistic: XA_RECOVERY 
DEBUG net.sf.ehcache.Cache Initialised cache: deviceCache 
DEBUG net.sf.ehcache.config.ConfigurationHelper CacheDecoratorFactory not configured. Skipping for 'personCache'. 
DEBUG net.sf.ehcache.config.ConfigurationHelper CacheDecoratorFactory not configured for defaultCache. Skipping for 'personCache'. 
DEBUG com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl Adding CACHE advised method 'detect' with attribute: CacheableAttributeImpl [cacheInstanceResolver=com.googleco[email protected]7a1b0c08, cacheKeyGenerator=HashCodeCacheKeyGenerator [includeMethod=true, includeParameterTypes=true, useReflection=false, checkforCycles=false], parameterMask=ParameterMask [mask=[]]] 
Apr 3, 2013 2:03:12 PM org.springframework.web.context.ContextLoader initWebApplicationContext 
DEBUG com.mycompany.Controller trying to get resource' 
DEBUG com.mycompany.PersonServiceImpl Missed 
DEBUG com.mycompany.Controller got person' 
DEBUG com.mycompany.Controller trying to get resource' 
DEBUG com.mycompany.PersonServiceImpl Missed 
DEBUG com.mycompany.Controller got person' 

Quindi la mia domanda è: perché funziona nel mio test di unità e non webapp? E come utilizzerei l'annotazione a molla invece dell'annotazione googlecode?

risposta

3

Anche se avevo un'interfaccia, avevo l'annotazione @Cacheable sulla classe di implementazione. Una volta spostato nella classe di interfaccia, è stato memorizzato nella cache.

10

Il servizio PersonService non sta implementando un'interfaccia. EHCache-spring-annotazioni richiede un'interfaccia, come descritto nella FAQ:

Requisito 1: la classe deve attuare alcune interfaccia (qualsiasi). Se la classe non implementa un'interfaccia , questo progetto non sarà in grado di creare un Proxy per simulare la classe e applicare la semantica della cache attorno ai metodi annotati .

+0

Ho lo stesso problema anche con esso implementazione di un'interfaccia –

+0

È stato un po 'che ho usato, ma mi ricordo vagamente di dover aggiungere un argomento del metodo fittizio a uno dei miei cache metodi no-args in modo che Ehcache potrebbe abbinare il valore restituito nella cache con quel metodo. Vale la pena provare.... – nickdos

0

è necessario abilitare l'annotazione della cache eh nel contesto di primavera xml o nel file di configurazione Spring. per esempio

<ehcache:annotation-driven cache-manager="ehCacheManager" /> 
Problemi correlati