1

Spring と ehcache を使用した @Cacheable が機能せず、キャッシュにデータが置かれません。アプリケーションがキャッシュ可能なメソッド getFolProfile を呼び出すと、キャッシュではなくデータベースが常に呼び出されます。私のコードで何が間違っているのか教えてください。

私のroot-context.xml:

    <cache:annotation-driven proxy-target-class="true"/>   
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:/cache/ehcache.xml"  /> 
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehcache"/>

私のサービス :

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.stereotype.Service;

    @Service
    public class FolManager {
@Autowired
FolDao folDao;



@Cacheable(value = "oneCache", key = "#email")
public FolProfileForm getFolProfile(String email) {
    return folDao.retrieveByLogin(email);
}
    }

私のehcache.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
    <diskStore path="c:/tmp" />
    <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"
    timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
    diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
    <cache name="oneCache" maxElementsInMemory="100000" maxElementsOnDisk="10000000" eternal="true" diskPersistent="true"
    overflowToDisk="true" diskSpoolBufferSizeMB="20" memoryStoreEvictionPolicy="LFU" />
    </ehcache>

助けてくれてありがとうミシェル

4

4 に答える 4

2

サービス層でキャッシングを定義しているようです。http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.htmlでは、具体的に次のように注記されています。

ノート

<cache:annotation-driven/>Bean が定義されているのと同じアプリケーション コンテキスト内の Bean で @Cacheable/@CacheEvict のみを検索します<cache:annotation-driven/>。あなたのサービス。詳細は、セクション16.2「DispatcherServlet」を参照してください。

于 2013-12-20T06:52:22.673 に答える
0

これを追加

<cache:annotation-driven cache-manager="cacheManager"  />

Spring に cache-manager の場所を伝える必要があります。

于 2013-02-26T09:47:02.760 に答える
0

proxy-target-class 属性が true に設定されている場合、クラスベースのプロキシが作成されます。CGLIB は、特定のターゲット オブジェクトのプロキシを作成するために使用されます。依存関係に CGLIB があることを確認してください。

選択肢がある場合は、proxy-target-class 属性を false に設定し、クラスに少なくとも 1 つのインターフェイスを実装して、JDK 動的プロキシを優先します。

于 2013-02-26T13:01:26.013 に答える