2013-04-03 6 views
5

मैं एक वेबपैप लिख रहा हूं, और नियंत्रक से बुलाए गए कुछ pojos को कैश करने की कोशिश कर रहा हूं।वसंत 3.1.1 एमवीसी @ कैशेबल हिट नहीं किया जा रहा है

मैं org.springframework.cache.annotation.Cacheable का उपयोग करने की कोशिश करते समय कुछ भी काम नहीं कर पा रहा था, इसलिए मैंने com.googlecode.ehcaceh.annotations.Cacheable पर स्विच किया, और मैं अभी भी इसे प्राप्त नहीं कर सकता कैश।

मेरे कोड इस तरह दिखता है:

@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"); 

और सेवा की तरह दिखता है:

public class PersonService { 

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

मेरे applicationContext

<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" /> 

तरह लग रहा है और मेरे ehcache की तरह दिखता है:

<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" /> 

दिलचस्प बात यह है कि यह मेरी इकाई परीक्षण में ठीक कैश है ...

@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"); 
} 

जो पैदा करता है:

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 

लेकिन मेरी युद्ध के निर्गम (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' 

तो मेरा सवाल यह है कि यह मेरे यूनिट परीक्षण में क्यों काम करता है, न कि वेबएप? और मैं googlecode एनोटेशन के बजाय वसंत एनोटेशन का उपयोग कैसे करूं?

उत्तर

3

भले ही मेरे पास इंटरफेस था, मेरे पास कार्यान्वयन कक्षा पर @ कैशेबल एनोटेशन था। एक बार जब मैं इसे इंटरफ़ेस कक्षा में ले गया, तो इसे कैश किया गया।

10

आपकी PersonService सेवा एक इंटरफ़ेस को कार्यान्वित नहीं कर रही है।

आवश्यकता 1: अपनी कक्षा कुछ (किसी भी) इंटरफ़ेस को लागू करना चाहिए FAQ में वर्णित के रूप ehcache वसंत-एनोटेशन, एक अंतरफलक की आवश्यकता है। यदि आपकी कक्षा इंटरफ़ेस को कार्यान्वित नहीं करती है, तो यह प्रोजेक्ट आपकी कक्षा नकल करने के लिए प्रॉक्सी बनाने में सक्षम नहीं होगा और आपके एनोटेटेड विधियों के आसपास कैश सेमेन्टिक्स लागू करेगा।

+0

के लिए मैं इसे एक अंतरफलक –

+0

को लागू करने के लिए एक समय हो गया है के बाद से मैं इसे इस्तेमाल किया है, लेकिन मैं थोड़ा मेरी कैश की गई कोई आर्ग तरीकों में से एक के लिए एक डमी विधि तर्क जोड़ने के लिए याद के साथ भी यही समस्या है तो यह है कि Ehcache उस विधि के साथ कैश किए गए वापसी मूल्य से मिलान कर सकता है। एक कोशिश के काबिल है.... – nickdos

0

आपको अपने वसंत संदर्भ xml या स्प्रिंग कॉन्फ़िगरेशन फ़ाइल में एएच कैश एनोटेशन को सक्षम करने की आवश्यकता है। उदाहरण

<ehcache:annotation-driven cache-manager="ehCacheManager" /> 
संबंधित मुद्दे