Ehcache Integrated Spring use

Source: Internet
Author: User

This article references: http://www.cnblogs.com/hoojo/archive/2012/07/12/2587556.html

Ehcache can cache pages, objects, and data while supporting cluster/distributed caching. Spring's support for Ehcache is also very good if it is easy to integrate spring and hibernate. Ehcache supports memory and disk caching, supports LRU, LFU and FIFO algorithms, supports split -type caches, and can be used as a cache plugin for hibernate. It can also provide a filter-based cache that caches the content of the response and uses GZIP compression to increase response speed

First, the preparatory work

If you have successfully added spring and Hibernate to your system, then you are ready to go to the ehcache below.

1. Download the jar Package

Ehcache object, data cache: Http://ehcache.org/downloads/destination?name=ehcache-core-2.5.2-distribution.tar.gz&bucket =tcdistributions&file=ehcache-core-2.5.2-distribution.tar.gz


2, need to add the following jar package to the Lib directory

Ehcache-core-2.5.2.jar

ehcache-spring-annotations-1.2.0.jar----Spring 3.0.5, more granular cache settings, more convenient annotations, can be specific to the return value of each method to do the cache, need ehcache-spring-annotations-1.1.x.

3, the current project SRC directory to add the configuration file

Ehcache.xml

Ehcache.xsd

These configuration files can be found in the Ehcache-core jar package.

ii. Basic usage of Ehcache


<span Style= "FONT-SIZE:18PX;" ><?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" > <diskstore path= " Java.io.tmpdir "/> <defaultcache eternal=" false "maxelementsinmemory=" + "overflowtodisk=" true "diskpersis Tent= "false" timetoidleseconds= "0" timetoliveseconds= "memorystoreevictionpolicy=" LRU "/><pre style=" bord Er-style:none; Text-align:left; padding:0px; line-height:12pt; Background-color:rgb (244, 244, 244); Margin:0em; width:100%; direction:ltr; Color:black; font-size:10pt; overflow:visible; " > <span style= "color: #008000;" ><!--</span></span> 
        Configuring Custom Caches
        Maxelementsinmemory: Maximum number of objects allowed to be created in cache
        Eternal: Whether the object in the cache is permanent, and if so, the timeout setting is ignored and the object never expires.
        Timetoidleseconds: The passivation time of cached data, that is, before an element dies,
                    The maximum time interval value for two access times, which can only be valid if the element is not permanently resident.
                    If the value is 0, it means that the element can pause for an infinite amount of time.
        Timetoliveseconds: The lifetime of the cached data, which is the maximum time interval value of an element from build to extinction.
                    This can only be valid if the element is not permanently resident, and if the value is 0, it means that the element can pause for an infinite amount of time.
        Overflowtodisk: If the disk cache is enabled when there is not enough memory.
        Memorystoreevictionpolicy: Cache is full after the elimination algorithm.
    -
<cache name= "Cache1" eternal= "false" maxelementsinmemory= "" "overflowtodisk=" true "diskpersistent=" false " timetoidleseconds= "0" timetoliveseconds= "memorystoreevictionpolicy=" "LRU"/> <cache name= "Cache2" eternal= "True" maxelementsinmemory= "overflowtodisk=" "true" Diskpersistent= "false" memorystoreevictionpolicy= "LRU"/ ></ehcache>
Code implementation
<span style= "FONT-SIZE:18PX;" > @Servicepublic class Cachemanagerimpl implements Cachemanager{private static List<net.sf.ehcache.cachemanager > cache_managers = Net.sf.ehcache.CacheManager.ALL_CACHE_MANAGERS; Log logger = Logfactory.getlog (cachemanagerimpl.class);/** * Delete content in the specified cache CacheName null means clear all cache contents * @param cachename Slow Save Name * @return */public boolean remove (String cachename) {try{if (cache_managers! = null) {for (Net.sf.ehcache.CacheManager Cachemanager:cache_managers) {if ("". Equals (cachename) | | CacheName = = null) {string[] cachenames = Cachemanager.getcachenames (); if (cachenames! = null) {for (String cache: Cachenames) {if (cachemanager.cacheexists (cache)) {Cachemanager.getcache (cache). RemoveAll ();}}} Else{if (Cachemanager.cacheexists (cachename)) {Cachemanager.getcache (cachename). RemoveAll ();}}}} catch (Exception e) {logger.error ("delete content in the specified cache failed! Cachename= "+ CacheName); return false;} return true;}} </span>
spring configuration file applicationcontext.xml configuration Ecache
<span style= "FONT-SIZE:18PX;" ><pre name= "code" class= "HTML" >1. To configure the cache bean, it is important to note that the Ehcache cache is a layer of DAO, so this section of configuration and DAO instantiation should be placed in the same configuration file <?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:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xmlns:context=" http ://www.springframework.org/schema/context "xmlns:jee=" Http://www.springframework.org/schema/jee "xmlns:lang=" Http://www.springframework.org/schema/lang "xmlns:p=" http://www.springframework.org/schema/p "xmlns:tx="/HTTP/ Www.springframework.org/schema/tx "xmlns:util=" Http://www.springframework.org/schema/util "xmlns:mvc="/HTTP/ Www.springframework.org/schema/mvc "Xmlns:ehcache=" http://ehcache-spring-annotations.googlecode.com/svn/schema/ Ehcache-spring "xsi:schemalocation=" Http://www.springframework.org/schema/lang http://www.springframework.org/ Schema/lang/spring-lang.xsd Http://www.springframework.org/schema/jee HTTP://WWW.SPRINGFRAmework.org/schema/jee/spring-jee.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/ Schema/aop/spring-aop.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.1.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/ Spring-util.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.1.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/HTTP Ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd "Default-autowire=" ByName ">.......................  <!--add Cache-->    <ehcache:annotation-driven Cache-manager= "Ehcachemanager"/>      <bean id= "Ehcachemanager" class= " Org.springframework.cache.ehcache.EhCacheManagerFactoryBean ">         < Property Name= "Configlocation" value= "Classpath:ehcache.xml"/>      </bean></span >







Ehcache Integrated Spring use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.