References for use of annotations: http://blog.csdn.net/wjacketcn/article/details/50945887 (Invasion and deletion)
Ehcache is a pure Java-implemented cache component, and spring has introduced support for Ehcache from 3.1 onwards.
Use:
1. Adding dependencies in Pom.xml
<Dependency> <groupId>Net.sf.ehcache</groupId> <Artifactid>Ehcache</Artifactid> <version>2.9.1</version> </Dependency>
2. Add ehcache configuration file under Classpath ehcache.xml
<?XML version= "1.0" encoding= "UTF-8"?> <EhcacheUpdateCheck= "false"> <DiskstorePath= "Java.io.tmpdir"/> <Defaultcachemaxelementsinmemory= "10000"Eternal= "false"Timetoidleseconds= "+"Timetoliveseconds= "+"Overflowtodisk= "true"diskpersistent= "false"Diskexpirythreadintervalseconds= "+"Memorystoreevictionpolicy= "LRU" />
<!--this name will be used--<Cachename= "Mycache"maxelementsinmemory= "10000"Eternal= "false"Overflowtodisk= "true"Timetoidleseconds= "+"Timetoliveseconds= "$"Memorystoreevictionpolicy= "LFU" /> </Ehcache>
3, applicationcontext.xml Add configuration (similar to the thing configuration)
<!--turn on the annotation function of the cache, otherwise the annotations will not take effect - <Cache:annotation-driven/> <!--declaring the cache manager - <BeanID= "CacheManager"class= "Org.springframework.cache.ehcache.EhCacheCacheManager"> < Propertyname= "CacheManager"ref= "Ehcachefactory"></ Property> </Bean> <!--Specify the factory class and Ehcache.xml file location - <BeanID= "Ehcachefactory"class= "Org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> < Propertyname= "Configlocation"value= "Classpath:ehcache.xml"/> </Bean>
4. Add annotations at the service level
@Cacheable (value= "Mycache")//This name is the value of the name attribute in the Ehcache.xml file public list<freeurl> FindAll () { return freeurlmapper.findall (); }
Case: Interceptors let go of the certification-free path
CREATE TABLE: Freeurl
CREATE TABLE ' Freeurl ' (
' id ' int (one) is not NULL,
' URL ' varchar (255) DEFAULT NULL,
PRIMARY KEY (' id ')
)
After the above configuration is complete, configure the following
1. Configure the Interceptor path in Spring-mvc.xml
< mvc:interceptors > < class= "com.util.interceptors.LoginInterceptors"/> </ mvc:interceptors >
2. Write service code and cache the query results
@Cacheable (value= "Mycache") public list<freeurl> findAll () { return Freeurlmapper.findall (); }
3. Write Interceptor Code
Public classLogininterceptorsextendshandlerinterceptoradapter{PrivateLogger log = Logmanager.getlogger (logininterceptors.class); @ResourcePrivateFreeurlservice Freeurlservice; @Override Public BooleanPrehandle (httpservletrequest request, httpservletresponse response, Object handler)throwsException {//Get Project PathString ContextPath =Request.getcontextpath (); //Get access PathString Requestpath =Request.getservletpath (); HttpSession Session=request.getsession (); //Query the authentication-free pathlist<freeurl> list =Freeurlservice.findall (); for(Freeurl freeurl:list) {//if the request path is an authentication-free path, release if(Freeurl.geturl (). Equals (Requestpath)) {return true; } } return false; } }
4. Effect view
When the project is started, the first access calls the Freeurlservice.findall () query database and puts the query results into the cache. The second and subsequent accesses will fetch the results that have been deposited directly from the cache.
No more access to the database. You can print SQL to see that the database is queried in addition to the first access, and the subsequent queries are fetched from the cache.
Spring annotation-based Ehcache cache consolidation