java - EhCache - 不使用 spring 数据进行缓存

标签 java spring caching jpa 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 存储库(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> 

有什么想法吗?

最佳答案

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

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

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

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

关于java - EhCache - 不使用 spring 数据进行缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21604317/

相关文章:

java - Izpack 安装程序在 Windows 上失败并出现 java.lang.NullPointerException

java - 无需 AJAX 即可动态添加和删除组件?

java - XML Pull 解析器异常 - kSOAP2

java - 哪个 Spring 注解与来自 MQTTCallBack 的消息一起使用

Android 在 View 下方绘制阴影 : dispatchDraw called often - any cache missed

caching - CSS 背景图片间歇性地出现在谷歌浏览器中

java - Jaxb 映射双向关系

JavaConfig Spring Web Flow 返回 404 not found (JSP)

java - 如何返回 Spring Boot 中模型中不存在的字段?

.net - 尝试访问 "Microsoft.Practices.EnterpriseLibrary.Caching"时 "CacheFactory.GetCacheManager();"有什么问题?