Springmvc+mybatis+maven+ehcache Cache Implementation __java

Source: Internet
Author: User

The so-called cache, is the program or the system will often call the object in memory, so that it can be used to quickly call, do not have to create new duplicate instances. This can reduce system overhead and improve system efficiency.

Caching can be divided into two major categories:

First, through file caching, as the name implies file caching refers to the storage of data on disk, whether you are in XML format, serialized file dat format or other file format;

Second, the memory cache, that is, the implementation of a class static map, the map for the regular additions and deletions check.

Introduction of Ehcache Caching system

EhCache is a pure Java in-process caching framework, with fast, lean and so on, is the default Cacheprovider in Hibernate.

EhCache applies the schema diagram, and the following figure is where the EhCache is located in the application:

The main characteristics of EhCache are:

    1. Fast, lean;

    2. Simple;

    3. A variety of caching strategies;

    4. Cache data has two levels: memory and disk, so there is no need to worry about capacity;

    5. Cached data is written to disk during the virtual machine reboot;

    6. Can be passed through RMI, pluggable APIs, such as distributed caching;

    7. A listening interface with cache and cache manager;

    8. Supports multiple cache manager instances and multiple cache regions for an instance;

    9. Provides Hibernate cache implementations;

Two, MAVEN add Ehcache core Package

In the Pom.xml configuration file, add

<!--Ehcache Related packages-->
   <dependency>
       <groupId>net.sf.ehcache</groupId>
       < artifactid>ehcache</artifactid>
       <version>2.7.5</version>
   </dependency>
   <dependency>
       <groupId>com.googlecode.ehcache-spring-annotations</groupId>
       < artifactid>ehcache-spring-annotations</artifactid>
       <version>1.2.0</version>
   </ Dependency>

Third, add a configuration file

Add Applicationcontext-ehcache.xml, Ehcache.xml, under the Resource folder (usually Src/main/resources/meta-inf)

1, Applicationcontext-ehcache.xml content is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:c= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/C"
Xmlns:cache= "Http://www.springframework.org/schema/cache" xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc" xmlns:jee= "Http://www.springframework.org/schema/jee"
xmlns:lang= "Http://www.springframework.org/schema/lang" xmlns:mvc= "Http://www.springframework.org/schema/mvc"
xmlns:p= "http://www.springframework.org/schema/p" xmlns:task= "Http://www.springframework.org/schema/task"
xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:util= "Http://www.springframework.org/schema/util" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop.xsd Http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/ Spring-cache.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd Http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/ Spring-jdbc.xsd Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/ Spring-jee.xsd Http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/ Spring-lang.xsd Http://www.springframework.org/schema/mvc Http://www.springframework.org/schema/mvc/sprinG-mvc.xsd Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd HTTP://WWW.S Pringframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd "> <!--Open Spring cache --> <cache:annotation-driven cache-manager= "CacheManager"/> <bean id= "cacheManagerFactory" class= "org . Springframework.cache.ehcache.EhCacheManagerFactoryBean "> <property name=" configlocation "value=" classpath : Meta-inf/ehcache.xml "/> </bean> <bean id=" CacheManager "class=" Org.springframework.cache.ehcache.EhCa Checachemanager "> <property name=" CacheManager "ref=" cachemanagerfactory "/> </bean> </beans >

This is to turn on the spring cache

2, Ehcache.xml content is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <ehcache xmlns:xsi=
"Http://www.w3.org/2001/XMLSchema-instance" "Xsi:nonamespaceschemalocation=" Ehcache.xsd "updatecheck=" true "
monitoring= "AutoDetect" dynamicconfig= "true" > <diskstore path= "${java.io.tmpdir}/${system.project_name" }/cache "/>
maxelementsondisk= "10000000" diskpersistent= "false" diskexpirythreadintervalseconds= "120" memorystoreevictionpolicy= "LRU"/> <cache name= "Basecache" "maxelementsinmemory=" 1000 "eternal=" false "overflowtodisk=" true "diskspoolbuffersizemb=" 20 "
timetoidleseconds= "timetoliveseconds=" memorystoreevictionpolicy= "LFU"/> </ehcache>

Here is the Add caching policy

Description

1. The address of Java.io.tmpdir refers to:

The operating system does not have the same directory as the system attribute on the
windows:java.io.tmpdir:[c:\docume~1\joshua\locals~1\temp\] on

Solaris: java.io.tmpdir:[/var/tmp/] on

Linux:java.io.tmpdir: [/tmp] on

Mac OS x:java.io.tmpdir: [/tmp] the

    default Temporary-file directory is specified by the system property Java.io.tmpdir. On UNIX Systems The ' default value ' is typically '/tmp ' or '/var/tmp '; On the Microsoft Windows systems It is typically "c:\temp". A different value may was given to this system property when the Java virtual machine is invoked, but programmatic changes To the, are not guaranteed to have any effect upon the "temporary directory used by".

To specify the Java.io.tmpdir System, can invoke the JVM as follows:

JAVA-DJAVA.IO.TMPDIR=/PATH/TO/TMPDI R by

Default this value should come from the TMP environment variable on Windows systems

2. Data meaning

    Unique identification of Name:cache
    maxelementsinmemory: The maximum number of cached objects in memory
    Maxelementsondisk: The maximum number of cached objects on the disk, if 0 represents Infinity
    Eternal:element is permanently valid, once set, timeout will not work
    Overflowtodisk: Configure this property, when the number of element in memory reaches Maxelementsinmemory, Ehcache will be element written to disk
    Timetoidleseconds: Set the allowable idle time of element before expiration. Only if the element is not permanently valid, the optional attribute, the default value is 0, which is idle time Infinity
    timetoliveseconds: Set the element to allow survival time before it expires. The maximum time is between the creation time and the expiration time. Used only if the element is not permanently valid, the default is 0, which is the element lifetime Infinity  
    diskpersistent: Whether to cache virtual machine Restart period Data  
    Diskexpirythreadintervalseconds: Disk expiration thread run interval, default is 120 seconds
    DISKSPOOLBUFFERSIZEMB: This parameter sets the buffer size of the Diskstore (disk cache). The default is 30MB. Each cache should have its own buffer
    Memorystoreevictionpolicy: When the maxelementsinmemory limit is reached, Ehcache will clean up the memory according to the specified policy. The default policy is LRU (least recently used). You can set to FIFO (first-in first Out)
or LFU (less used)

Four, DAO layer to do configuration

 import Org.springframework.cache.CacheManager;
    Import org.springframework.cache.annotation.Cacheable; ... @TriggersRemove (cachename= "Basecache", removeall=true) public Entity Save (Entity Entity) throws Crudexc
    eption {return entity; @TriggersRemove (cachename= "Basecache", removeall=true) public Entity update (Entity Entity) throws Crudexception
    {return entity;
            @TriggersRemove (cachename= "Basecache", removeall=true) public void del (Entity Entity) throws Crudexception { @Cacheable (value= "Basecache", key = "' FindAll '") Public list<entity> FindAll () throws 

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.