This is done for both JPA and hibernate level two caches
Code directory:
This is the base jar package, if less, go to maven to download
<!--Spring--<dependency> <groupId>org.springframework</groupId> & Lt;artifactid>spring-context</artifactid> <version>${org.springframework-version}</version> ; <exclusions> <!--Exclude Commons Logginginch Favor of SLF4J-<exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> < /exclusions> </dependency> <dependency> <groupid>org.springframework</gro Upid> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-ver Sion}</version> </dependency> <!--AspectJ--<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${org.aspectj-version}</version> </dependency><!--Test--<dependency& Gt <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--https://Mvnrepository.com/artifact/net.sf.ehcache/ehcache-core --<dependency> <groupId>net.sf.ehcache</groupId> <artifactid>ehcache-core</ Artifactid> <version>2.6.6</version> </dependency> <!--https://mvnrepository.com/artifact/org.springframework/spring-test --<dependency> <groupId>org.springframework</groupId> <artifactid>spring-test& Lt;/artifactid> <version>${org.springframework-version}</version> </dependency>< !--AspectJ--
Ehcache.xml:
<?xml version="1.0"encoding="UTF-8"? ><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation="http://ehcache.org/ehcache.xsd"UpdateCheck="false"> <cache name="Basecache"Eternal="false"Maxentrieslocalheap=" $"Overflowtodisk="false"diskpersistent="false"Timetoidleseconds=" -"Statistics="true"Timetoliveseconds=" -"/></ehcache>
Here are two kinds of bean configuration, one is XML (Ehcacheconfig.xml), one is Java (Ehcacheconfig.java), as follows:
Ehcacheconfig.xml:
<?xml version="1.0"encoding="UTF-8"? ><beans xmlns="Http://www.springframework.org/schema/beans"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"Xmlns:cache="Http://www.springframework.org/schema/cache"xsi:schemalocation="Http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsdhttp//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--enable Caching--<cache:annotation-driven cache-manager="CacheManager"/> <bean id="cm" class="Com.spring.ehcache.CacheMethod"></bean> <bean id="ehcachemanagerfactory" class="Org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configlocation"Value="Classpath:ehcache.xml"/> </bean> <!--This bean's ID must be called CacheManager, or it will error No Bean named'CacheManager' isDefined--> <bean id="Caachemanager" class="Org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="CacheManager" ref="ehcachemanagerfactory"/> <property name="Transactionaware"Value="true"/> </bean> </beans>
Ehcacheconfig. Java:
Package Com.spring.ehcache;import Net.sf.ehcache.cachemanager;import Org.springframework.cache.annotation.enablecaching;import Org.springframework.cache.ehcache.EhCacheCacheManager ; Import Org.springframework.cache.ehcache.ehcachemanagerfactorybean;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import Org.springframework.core.io.ClassPathResource, @Configuration @enablecaching//Enable caching Public classehcacheconfig {@Bean (name="Ehcachecachemanager") PublicEhcachecachemanager Ehcachecachemanager (cachemanager cm) {Ehcachecachemanager Ehcachecachemanager=NewEhcachecachemanager (); Ehcachecachemanager.settransactionaware (true); Ehcachecachemanager.setcachemanager (CM); returnEhcachecachemanager; } @Bean (Name="Ehcachemanagerfactorybean") PublicEhcachemanagerfactorybean Ehcachemanagerfactorybean () {String src="Ehcache.xml"; System. out. println ("Ehcachemanagerfactorybean ."); Ehcachemanagerfactorybean Ehfactorybean=NewEhcachemanagerfactorybean (); Ehfactorybean.setconfiglocation (NewClasspathresource (SRC)); returnEhfactorybean; } @Bean (Name="cm") PublicCachemethod Cachemethod () {return NewCachemethod (); } }
Cachemethod.java: This is the cache test class, if there is a cache, the inside of the Getstr () method will be executed once, otherwise it will be executed multiple times
Package Com.spring.ehcache;import org.springframework.cache.annotation.Cacheable; Public classCachemethod { PublicCachemethod () {System. out. println ("Cachemethod ."); }
@Cacheable indicates that spring should first look up the return value of the method in the cache before calling the method. If the value can be found, the cached value is returned. Otherwise the method is called and the return value is placed in the cache
@CachePut indicates that spring should place the cached value of the method in the cache. The cache is not checked until the method is called, and the method is always called
@CacheEvict indicates that spring should clear one or more entries in the cache
//@Caching This is a grouped note that can apply multiple other cached annotations at the same time @Cacheable ("Basecache") PublicString getstr () {System. out. println ("get Str.."); return "Test Get str"; }}
Testcache.java
Package Com.spring.ehcache;import Org.junit.test;import org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.cache.ehcache.ehcachecachemanager;import Org.springframework.cache.ehcache.ehcachemanagerfactorybean;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Org.springframework.test.context.contextconfiguration;import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration (Classes=ehcacheconfig.class) @RunWith (Springjunit4classrunner.class)//Springjunit4classrunner.class Use note that JUnit's version requires more than 9 Public classTestcache {@AutowiredPrivateCachemethod cm; @AutowiredPrivateEhcachemanagerfactorybean Ehcachemanagerfactorybean; @AutowiredPrivateEhcachecachemanager Ehcachecachemanager; /** * use java config bean **/@Test Public voidGetCache () {System. out. println (Ehcachemanagerfactorybean); System. out. println (Ehcachecachemanager); System. out. println (Cm.getstr ()); System. out. println (Cm.getstr ()); System. out. println (Cm.getstr ()); } /** * Use XML config bean * public static void Main (string[] args) {ApplicationContext app = new CLASSPATHXM Lapplicationcontext ("Com/spring/ehcache/ehcacheconfig.xml"); System.out.println (App.getbean ("ehcachemanagerfactory")); System.out.println (App.getbean ("CacheManager")); System.out.println (((Cachemethod) App.getbean ("cm")). Getstr ()); System.out.println (((Cachemethod) App.getbean ("cm")). Getstr ()); System.out.println (((Cachemethod) App.getbean ("cm")). Getstr ()); } */}
Configuration of the Ehcache