我在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 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建议方法'getOrgCustomProfile':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 )
当拦截器调用自动服务的服务时,只有其中一个缓存:
[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模块中定义的。
答案 0 :(得分:3)
你是如何调用第二种方法的,是通过OrganisationInfoService接口吗?注释依赖于通过接口调用方法,因此可以生成执行缓存的代理。
如果您直接在外部调用具体类,或者作为来自类中其他方法的调用,则注释将不起作用。
请参阅常见问题解答中的答案3和4: http://code.google.com/p/ehcache-spring-annotations/wiki/FrequentlyAskedQuestions
答案 1 :(得分:3)
事实证明,原因与上下文有关:组件扫描。 无法缓存的服务包含在两个不同的组件扫描中。一旦我解决了缓存按预期工作的情况。