gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:47:41 30 4
gpt4 key购买 nike

我在 Spring 中有一个拦截器,它 Autowiring 两个不同的服务。这两种服务都有来自 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 () 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)

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

[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)

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

最佳答案

原来是和context:component-scan有关的原因。未能缓存的服务包含在两个不同的组件扫描中。一旦我确定缓存按预期工作。

关于java - 为什么我的 @Cachable 注释方法不使用 EHCache 缓存结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7412049/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com