3

我在 Spring 中有一个拦截器,它自动连接两个不同的服务。这两个服务都有@Cacheableehcache-spring-annotations 项目中标记的方法,但使用不同的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 forever = 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 建议方法 'getOrgCustomProfile':CacheableAttributeImpl [cache=[ name = orgProfile status = STATUS_ALIVE ever = 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;目标属于 [no.finntech.service.organisation.impl.OrganisationInfoServiceImpl] [URI:/finn/minfinn/myitems/list,远程 IP:127.0.0.1,Referer:,用户代理: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 模块中定义的。

4

2 回答 2

4

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

于 2011-09-16T07:33:39.177 回答
3

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

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

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

于 2011-09-14T07:49:48.400 回答