为什么我的@Cachable注释方法不使用 EHCache 缓存结果?



我在Spring中有一个拦截器,它自动连接两个不同的服务。这两个服务的方法都有来自ehcache-spring-annotations项目的@Cacheable标记,但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 {

当我运行我的应用程序时,一个方法成功地为结果使用EHCache,而另一个则没有。OrganisationInfoSericeImpl.getOrgCustomProfile()缓存正常,EventServiceImpl.getFolderEvnetsForUser缓存不正常。有人能告诉我为什么吗?

我已经尝试为两个服务使用相同的缓存,但仍然只有一个工作。我打开了ehcache-spring-annotations的DEBUG,它在启动时注册了这两个方法:

[DEBUG] 08:09:01()添加CACHE建议方法'getFolderEventsForUser'与属性:CacheableAttributeImpl [CACHE =[name = ecomOrders status = STATUS_ALIVE status = false overflowToDisk = false maxElementsInMemory = 100 maxElementsOnDisk = 0 memoryStoreEvictionPolicy = LRU timeolivesecseconds = 300 timetoidlesecseconds = 0 diskPersistent = false diskExpiryThreadIntervalSeconds = 120 cacheEventListeners:net. s.f.h cache.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()添加CACHE建议方法'getOrgCustomProfile'与属性:CacheableAttributeImpl [CACHE =[name = orgProfile status = STATUS_ALIVE eternal = false overflowToDisk = false maxElementsInMemory = 200 maxElementsOnDisk = 0 memoryStoreEvictionPolicy = LRU timeolivesecseconds = 86400 timetoidlesecseconds = 0 diskPersistent = false diskExpiryThreadIntervalSeconds = 120 cacheEventListeners:net. s.f.h cache.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:19 (UNIQUE_ID)为调用生成密钥'-1668638847278617':ReflectiveMethodInvocation:公共抽象no.finntech.base.modules.organisation.support.OrgCustomProfileBean no.finntech.service.organisation.OrganisationInfoService.getOrgCustomProfile(java.lang.String)抛出no.finntech.service.ServiceException;目标是类[no. finntechnology .service.organisation.impl. organisationinfoserviceimpl] [URI:/finn/minfinn/myitems/list],远程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. googlcode .ehcache.annotations.interceptor. ehcacheinterceptor . generatecachekey (EhCacheInterceptor.java:272)

编辑:我应该提到这两个服务是在不同的maven模块中定义的。

原因与上下文有关:组件扫描。缓存失败的服务包含在两个不同的组件扫描中。当我确定缓存工作正常时。

如何调用第二个方法,是通过organizationinfoservice接口吗?注释依赖于通过接口调用方法,因此可以生成代理来进行缓存。

如果你直接从外部调用具体类,或者从类中的另一个方法调用,注释将不起作用。

参见FAQ中的答案3和4:http://code.google.com/p/ehcache-spring-annotations/wiki/FrequentlyAskedQuestions

最新更新