【问题标题】:EhCache - not caching with spring dataEhCache - 不缓存弹簧数据
【发布时间】:2014-03-03 11:33:45
【问题描述】:

我有以下方法:

@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 存储库 (spring-data) 进行数据库操作。我配置了二级缓存。但没有命中/未命中。我的 Operator 实体注释如下:

@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> 

有什么想法吗?

【问题讨论】:

    标签: java spring caching jpa ehcache


    【解决方案1】:

    当使用@Cache 注解时,默认情况下实体被缓存在一个区域(缓存)中,其名称等于实体的完全限定类名。

    如果您希望将Operator 实体缓存在example 缓存中,则应将region 添加到@Cache,如下例所示:

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

    另外 - 如果缓存工作正常 - 您当然应该调用 this.operatorRepository.findOne(1L); 两次以查看任何缓存命中。

    【讨论】:

      猜你喜欢
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-03
      • 2016-09-11
      • 2016-09-20
      • 2019-04-07
      • 1970-01-01
      相关资源
      最近更新 更多