3

Spring には、2 つの異なるサービスを自動配線するインターセプターがあります。どちらのサービスにも@Cacheable、ehcache-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しません。誰かが理由を教えてもらえますか?

両方のサービスで同じキャッシュを使用しようとしましたが、まだ機能するのは 1 つだけです。ehcache-spring-annotations の DEBUG をオンにすると、起動時に両方のメソッドが登録されます。

[DEBUG] 08:09:01 () 属性を持つ CACHE 推奨メソッド 'getFolderEventsForUser' を追加: CacheableAttributeImpl [cache=[ name = ecomOrders status = STATUS_ALIVE 永遠 = 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 [マスク=[]]] [] com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.getMethodAttribute(CacheAttributeSourceImpl.java:174)

[DEBUG] 08:09:01 () 属性を持つ CACHE 推奨メソッド 'getOrgCustomProfile' を追加: CacheableAttributeImpl [cache=[ name = orgProfile status = STATUS_ALIVE 永遠 = 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 [マスク=[]]] [] com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.getMethodAttribute(CacheAttributeSourceImpl.java:174)

インターセプターが自動配線されたサービスを呼び出すと、そのうちの 1 つだけがキャッシュされます。

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

編集:おそらく、2 つのサービスが異なる Maven モジュールで定義されていることに言及する必要があります。

4

2 に答える 2

4

その理由は、context:component-scan に関連していたことが判明しました。キャッシュに失敗したサービスは、2 つの異なるコンポーネント スキャンに含まれていました。キャッシングが期待どおりに機能することを解決するとすぐに。

于 2011-09-16T07:33:39.177 に答える
3

2 番目のメソッドをどのように呼び出していますか? OrganizationInfoService インターフェイス経由ですか? 注釈は、キャッシュを行うプロキシを生成できるように、インターフェイスを介してメソッドを呼び出すことに依存しています。

具象クラスを外部から、またはクラス内の別のメソッドからの呼び出しとして直接呼び出している場合、注釈は機能しません。

FAQ の回答 3 と 4 を参照してください: http://code.google.com/p/ehcache-spring-annotations/wiki/FrequentlyAskedQuestions

于 2011-09-14T07:49:48.400 に答える