No nonsense. Direct Source Link: https://git.oschina.net/alexgaoyh/alexgaoyh.git
use ehcache to improve the performance of the system, now with a lot of, also support distributed cache, in hibernate as a two-level cache implementation products, can improve query performance.
Pom.xml
<dependency> <groupId>org.hibernate</groupId> <artifactid>hibernate-ehcache</ Artifactid> <version>4.1.6.Final</version> </dependency>
add the ehcache configuration file under src for the project ehcache.xml
<ehcache xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation= ". /config/ehcache.xsd "><!--subdirectories can be specified below the property e.g. Java.io.tmpdir/one- <diskstore path= "Java.io.tmpdir"/> <!--Mandatory Default Cache configuration. These settings'll is applied to caches created programmtically using Cachemanager.add (String cachename)-- <defaultcache maxelementsinmemory= "10000" eternal= "false" timetoidleseconds= "timetoliveseconds=" 12 0 "overflowtodisk=" true "maxelementsondisk=" 10000000 "diskpersistent=" false "Diskexpirythreadintervalsecon ds= "memorystoreevictionpolicy=" "LRU"/> <cache name= "Org.hibernate.cache.spi.UpdateTimestampsCach E "maxelementsinmemory=" eternal= "true" overflowtodisk= "true"/><cache name= "Org.hibernate.cach E.internal.standardquerycache "maxelementsinmemory=" 10000 "etErnal= "false" timetoliveseconds= "overflowtodisk=" true "/><!--Java file annotations Find the Cache method name policy: If you do not specify a Java file in the annotations Region= "Property value for name in Ehcache.xml", use the cache with name Com.lysoft.bean.user.User (that is, the full path name of the class), and if there is no cache name that matches the class name, use the Defaultcache If user contains a set set, you need to specify its cache for example user contains Cityset collection, you also need to add configuration to Ehcache.xml--<cache name= "Javaclassna Me "maxelementsinmemory=" "eternal=" false "timetoidleseconds=" "timetoliveseconds=" Overflowtodisk = "true"/> </ehcache>
In the Spring integrated hibernate configuration file, add the following configuration
<!--Open the query cache--><prop key= "Hibernate.cache.use_query_cache" >true</prop><!--turn on level two caching-->< Prop key= "Hibernate.cache.use_second_level_cache" >true</prop><!--Cache Provider--<!-- Since spring also uses ehcache, both sides are guaranteed to use the same cache manager--><prop key= "Hibernate.cache.region.factory_class" > Org.hibernate.cache.ehcache.singletonehcacheregionfactory</prop>
Spring also uses Ehcache, so you also need to add ehcache configuration to the spring configuration file
<!--CacheManager, specifying the location of Ehcache.xml-<bean id= "Cachemanagerehcache" class= " Org.springframework.cache.ehcache.EhCacheManagerFactoryBean "><property name=" configlocation "> < value>classpath:ehcache.xml</value> </property> <!--because Hibernate also uses Ehcache, Ensure both parties use the same cache manager-- <property name= "shared" value= "true"/> </bean>
Defined in the class:
@Entity @Table (name = "T_user") @Cache (usage = cacheconcurrencystrategy.read_write, region= "Javaclassname") Public class User implements Serializable { }
By default, level two caches only cache methods such as load get, and methods like List iterator also use the cache to be used with the query cache, overriding the Query method
. Setcacheable (True)
Criteria.setcacheable (True). List ();
Verify after
spring4.x hibernate4.x Integration Ehcache annotations Annotate