我有以下方法:
@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>
任何想法为什么?
答案 0 :(得分:1)
使用@Cache
注释时,默认情况下,实体会缓存在一个区域(缓存)中,该区域的名称等于实体的完全限定类名。
如果您希望在Operator
缓存中缓存example
个实体,则应向region
添加@Cache
,例如以下示例:
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region="example")
@Entity
class Operator {
...
}
此外 - 如果缓存正常工作 - 您应该两次调用this.operatorRepository.findOne(1L);
以查看任何缓存命中。