Reputation: 6532
I have the following method:
@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());
}
The output is:
HIT: 0
MISS_EXPIRED: 0
MISS_NOT_FOUND: 0
0
MISS COUNT: 0
HIT COUNT: 0
MISS_EXPIRED COUNT: 0
CACHE_SIZE: 0
As you can see i'm using jpa repositories (spring-data) for my db operations. i configured a 2nd level cache. and yet no hits\misses. my Operator entity is annotated like this:
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
here is my xml configurations:
<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>
and my cache 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>
Any ideas why?
Upvotes: 1
Views: 1791
Reputation: 19000
When using @Cache
annotation, by default entities are cached in a region (cache) whose name equals to the fully qualified class name of the entity.
If you want the Operator
entities to be cached in the example
cache, you should add a region
to @Cache
, such as in the following example:
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region="example")
@Entity
class Operator {
...
}
Also - if cache works correctly - you should call this.operatorRepository.findOne(1L);
twice of course to see any cache hits.
Upvotes: 1