3

I have an interceptor in Spring, which autowires two different services. Both services have methods that are tagged with @Cacheable from the ehcache-spring-annotations project, but with different cacheNames.

public class MenuInterceptor extends HandlerInterceptorAdapter {
    @Autowired
    private EventService eventService;

    @Autowired
    private OrganisationInfoService orgService;

    @Override
    public final void postHandle(HttpServletRequest request,
                       HttpServletResponse response,
                       Object handler,
                       ModelAndView modelAndView) throws SystemException {
        eventService.getFolderEventsForUser(123);
        orgService.getOrgCustomProfile("abc");

    }

@Service
public class EventServiceImpl implements EventService {
    @Override
    @Cacheable(cacheName = "ecomOrders")
    public Collection<FolderEventBean> getFolderEventsForUser(long loginId) throws SystemException {


@Service("organisationInfoService")
public class OrganisationInfoServiceImpl implements OrganisationInfoService {
    @Override
    @Cacheable(cacheName="orgProfile")
    public OrgCustomProfileBean getOrgCustomProfile(String orgHierarchyString) throws ServiceException {

When I run my application, one method successfully uses EHCache for the result, while the other does not. The OrganisationInfoSericeImpl.getOrgCustomProfile() caches properly, while the EventServiceImpl.getFolderEvnetsForUser does not. Can someone please tell me why?

I have tried to use same cache for both services, but still only one of them works. I turned on DEBUG for ehcache-spring-annotations, and it registers both methods during startup:

[DEBUG] 08:09:01 () Adding CACHE advised method 'getFolderEventsForUser' with attribute: CacheableAttributeImpl [cache=[ name = ecomOrders status = STATUS_ALIVE eternal = false overflowToDisk = false maxElementsInMemory = 100 maxElementsOnDisk = 0 memoryStoreEvictionPolicy = LRU timeToLiveSeconds = 300 timeToIdleSeconds = 0 diskPersistent = false diskExpiryThreadIntervalSeconds = 120 cacheEventListeners: net.sf.ehcache.statistics.LiveCacheStatisticsWrapper hitCount = 0 memoryStoreHitCount = 0 diskStoreHitCount = 0 missCountNotFound = 0 missCountExpired = 0 ], cacheKeyGenerator=HashCodeCacheKeyGenerator [includeMethod=true, includeParameterTypes=true, useReflection=false, checkforCycles=false], entryFactory=null, exceptionCache=null, parameterMask=ParameterMask [mask=[]]] [] at com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.getMethodAttribute(CacheAttributeSourceImpl.java:174)

[DEBUG] 08:09:01 () Adding CACHE advised method 'getOrgCustomProfile' with attribute: CacheableAttributeImpl [cache=[ name = orgProfile status = STATUS_ALIVE eternal = false overflowToDisk = false maxElementsInMemory = 200 maxElementsOnDisk = 0 memoryStoreEvictionPolicy = LRU timeToLiveSeconds = 86400 timeToIdleSeconds = 0 diskPersistent = false diskExpiryThreadIntervalSeconds = 120 cacheEventListeners: net.sf.ehcache.statistics.LiveCacheStatisticsWrapper hitCount = 0 memoryStoreHitCount = 0 diskStoreHitCount = 0 missCountNotFound = 0 missCountExpired = 0 ], cacheKeyGenerator=HashCodeCacheKeyGenerator [includeMethod=true, includeParameterTypes=true, useReflection=false, checkforCycles=false], entryFactory=null, exceptionCache=null, parameterMask=ParameterMask [mask=[]]] [] at com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.getMethodAttribute(CacheAttributeSourceImpl.java:174)

When the interceptor calls the autowired services, only one of them caches:

[DEBUG] 08:09:19 (UNIQUE_ID) Generated key '-1668638847278617' for invocation: ReflectiveMethodInvocation: public abstract no.finntech.base.modules.organisation.support.OrgCustomProfileBean no.finntech.service.organisation.OrganisationInfoService.getOrgCustomProfile(java.lang.String) throws no.finntech.service.ServiceException; target is of class [no.finntech.service.organisation.impl.OrganisationInfoServiceImpl] [URI: /finn/minfinn/myitems/list, Remote IP: 127.0.0.1, Referer: , User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2] at com.googlecode.ehcache.annotations.interceptor.EhCacheInterceptor.generateCacheKey(EhCacheInterceptor.java:272)

EDIT: I should probably mention that the two services are defined in different maven modules.

Community
  • 1
  • 1
Nicolai
  • 3,698
  • 3
  • 30
  • 34

2 Answers2

4

It turns out the reason was related to context:component-scan. The service that failed to cache, was included in two different component scans. As soon as I resolved that the caching worked as expected.

Nicolai
  • 3,698
  • 3
  • 30
  • 34
3

How are you calling the second method, is it through the OrganisationInfoService interface? The annotations rely on calling the method through an interface so a proxy can be generated that does the caching.

If you are calling the concrete class directly either externally or as a call from another method in the class the annotations won't work.

See answers 3 and 4 in the FAQ: http://code.google.com/p/ehcache-spring-annotations/wiki/FrequentlyAskedQuestions

Paolo
  • 22,188
  • 6
  • 42
  • 49
  • Both are called through their respective interfaces. – Nicolai Sep 14 '11 at 08:08
  • Hmm, the first cache has a timeToLive value of 300secs compared to the other one (which is 86400s) - this means cached objects will be evicted after 5mins regardless of last access time, so if the method is only called rarely for a given loginId it'll look like things aren't cached. Might that be the reason? – Paolo Sep 14 '11 at 08:23
  • At the moment I am just testing this out, and checking if it is cached at all. The calls are made within a few seconds of each other. The cache proxy is never used for the EventService, so there must be some sort of setup/configuration issue I guess. – Nicolai Sep 14 '11 at 09:30