首页 文章

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

提问于
浏览
2

我在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 则没有 . 有人可以告诉我为什么吗?

我曾尝试为这两种服务使用相同的缓存,但仍然只有其中一种可以使用 . 我打开了DEBUG for ehcache-spring-annotations,它在启动时注册了两个方法:

[DEBUG] 08:09:01()添加CACHE建议方法'getFolderEventsForUser',其属性为: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()使用属性:Cache添加CACHE建议方法'getOrgCustomProfile' ableAttributeImpl [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)

当拦截器调用自动服务的服务时,只有其中一个缓存:

[DEBUG] 08:09:19(UNIQUE_ID)为调用生成密钥'-1668638847278617':ReflectiveMethodInvocation:public abstract no.finntech.base.modules.organisation.support.OrgCustomProfileBean no.finntech.service.organisation.OrganisationInfoService.getOrgCustomProfile( java.lang.String)抛出no.finntech.service.ServiceException; target是类[no.finntech.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]在com.googlecode.ehcache.annotations.interceptor.EhCacheInterceptor.generateCacheKey(EhCacheInterceptor.java:272)

编辑:我应该提一下,这两个服务是在不同的maven模块中定义的 .

2 回答

  • 3

    事实证明,原因与上下文有关:组件扫描 . 无法缓存的服务包含在两个不同的组件扫描中 . 一旦我解决了缓存按预期工作的问题 .

  • 3

    你是如何调用第二种方法的,是通过OrganisationInfoService接口吗?注释依赖于通过接口调用方法,因此可以生成执行缓存的代理 .

    如果您直接在外部调用具体类,或者作为来自类中其他方法的调用,则注释将不起作用 .

    请参阅常见问题解答中的答案3和4:http://code.google.com/p/ehcache-spring-annotations/wiki/FrequentlyAskedQuestions

相关问题