2014-02-06 52 views
1

我有以下方法:的Ehcache - 不帶彈簧的數據緩存

@Scheduled(fixedRate = 20000) 
@Async 
public void test() { 

Operator operator = this.operatorRepository.findOne(1L); 
System.out.println(operator.getName()); 

org.springframework.cache.ehcache.EhCacheCache c = (EhCacheCache) cacheManager.getCache("example"); 
System.out.println("HIT: "+c.getNativeCache().getStatistics().getExtended().get().component(GetOutcome.HIT).count().value()); 
System.out.println("MISS_EXPIRED: "+c.getNativeCache().getStatistics().getExtended().get().component(GetOutcome.MISS_EXPIRED).count().value()); 
System.out.println("MISS_NOT_FOUND: "+c.getNativeCache().getStatistics().getExtended().get().component(GetOutcome.MISS_NOT_FOUND).count().value()); 

System.out.println(c.getNativeCache().getStatistics().cacheHitOperation().count().value()); 
System.out.println("MISS COUNT: " +c.getNativeCache().getStatistics().cacheMissCount()); 
System.out.println("HIT COUNT: " +c.getNativeCache().getStatistics().cacheHitCount()); 
System.out.println("MISS_EXPIRED COUNT: " +c.getNativeCache().getStatistics().cacheMissExpiredCount()); 
System.out.println("CACHE_SIZE: " + c.getNativeCache().getStatistics().getSize()); 

} 

輸出是:

HIT: 0 
MISS_EXPIRED: 0 
MISS_NOT_FOUND: 0 
0 
MISS COUNT: 0 
HIT COUNT: 0 
MISS_EXPIRED COUNT: 0 
CACHE_SIZE: 0 

正如你可以看到我使用JPA庫(彈簧數據)我的數據庫操作。我配置了二級緩存。但沒有命中\錯過。我的操作實體,註釋是這樣的:

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 

這裏是我的XML配置:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 

    <property name="url" value="${mysql_url}" /> 
    <property name="username" value="xxx" /> 
    <property name="password" value="xxx" /> 

    <property name="initialSize" value="10" /> 
    <property name="maxActive" value="100" /> 
    <property name="maxIdle" value="15" /> 
    <property name="minIdle" value="10" /> 
    <property name="timeBetweenEvictionRunsMillis" value="10000" /> 
    <property name="minEvictableIdleTimeMillis" value="60000" /> 

    <property name="validationQuery" value="/* ping */ SELECT 1" /> 
    <property name="testOnBorrow" value="true" /> 
    <property name="testWhileIdle" value="true" /> 

    <property name="removeAbandoned" value="true" /> 
    <property name="removeAbandonedTimeout" value="300" /> 
</bean> 

<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 

    <property name="packagesToScan" 
     value="com.xxx.model, com.xxx.shared.model" /> 
    <property name="dataSource" ref="dataSource" /> 

    <property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.hbm2ddl.auto">update</prop> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
      <prop key="hibernate.cache.use_second_level_cache">true</prop> 
      <prop key="hibernate.cache.use_query_cache">true</prop> 
      <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> 
      <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop> 
      <prop key="hibernate.generate_statistics">true</prop> 
     </props> 
    </property> 
    <property name="jpaDialect"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> 
    </property> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
      <property name="database" value="MYSQL" /> 
      <property name="showSql" value="false" /> 
      <property name="generateDdl" value="false" /> 
     </bean> 
    </property> 
    <property name="jpaPropertyMap"> 
     <map> 
      <entry key="hibernate.connection.autocommit" value="false" /> 
     </map> 
    </property> 

</bean> 

和我的緩存的xml:

<ehcache name="test"> 
<cache name="example" maxElementsInMemory="1000" eternal="false" statistics="true" 
    overflowToDisk="false" timeToIdleSeconds="0" timeToLiveSeconds="600"> 
    <cacheEventListenerFactory 
     class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" 
     properties="replicateAsynchronously=false,replicatePuts=false 
     ,replicateUpdates=true,replicateUpdatesViaCopy=false 
     ,replicateRemovals=true" 
     propertySeparator="," /> 
    <bootstrapCacheLoaderFactory 
     class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" 
     properties="bootstrapAsynchronously=true, maximumChunkSizeBytes=5000000" /> 
</cache> 

<cacheManagerPeerProviderFactory 
    class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" 
    properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, 
       multicastGroupPort=4446, timeToLive=32" /> 
<cacheManagerPeerListenerFactory 
    class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" 
    properties="hostName=localhost, port=40001, socketTimeoutMillis=2000" /> 
</ehcache> 

任何想法,爲什麼?

回答

1

當使用@Cache註釋時,默認情況下,實體被緩存在名稱等於實體的完全限定類名的區域(緩存)中。

如果你想Operator實體在example緩存到緩存,您應該添加一個region@Cache,如下面的例子:

@Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region="example") 
@Entity 
class Operator { 
    ... 
} 

而且 - 如果緩存正常工作 - 你應該打電話this.operatorRepository.findOne(1L);當然兩次查看任何緩存命中。