SPRINGMVC Integrated cache Framework Ehcache__java cache Framework Ehcache

Source: Internet
Author: User
Tags time interval

Ehcache is the current more popular cache framework, the use of caching can greatly alleviate the pressure on the server and database, improve access efficiency, improve the server's concurrency capabilities. Next we'll see how to use the cache.

SPRINGMVC the required jar packs for integrated Ehcache:

including Ehcache-core-2.4.3.jar, Ehcache-web-2.0.4.jar, Guava-15.0.jar, Slf4j-api-1.7.12.jar, Commons-logging.jar

Jar Package Download Address http://download.csdn.net/detail/qq_33556185/9550460

After putting the jar package in the project, let's see how we can integrate it.

(i) Configure Ehcahce.xml

Updatecheck= ' false ' does not check the update of the currently used Ehcache version

Eternal: Whether the object in the cache is permanent, and if so, the timeout setting is ignored and the object never expires.

Maxelementsinmemory: Maximum number of objects allowed to be created in the cache

Overflowtodisk: Disk caching is enabled when there is not enough memory.

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 length of time.

Timetoliveseconds: The lifetime of the cached data, which is the maximum time interval of an element from construction to extinction,

This can only work if the element is not permanently resident, and if the value is 0 it means the element can pause for an infinite length of time.

Memorystoreevictionpolicy: Cache full after the elimination algorithm.

1 FIFO, advanced first Out

2 LFU is used at least, the cached element has a hit attribute, and the hit value will be cleared out of the cache.

3 LRU, the least recently used, cached element has a timestamp, and when the cache is full and there is a need to make room to cache new elements, the elements in the existing cache element that are the furthest from the current time are cleared out of the cache.

<?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" > 
<!-Default Cache--! >
    <defaultcache eternal= "false"  
        maxelementsinmemory= "10000" 
        overflowtodisk= "false"  
        timetoidleseconds= "0" 
        timetoliveseconds= "0"  
        memorystoreevictionpolicy= "LFU"/> 
   <!-Custom Cache--! >
    <cache name= "Mycache"  
        eternal= false "maxelementsinmemory=  
        " 10000 
        "overflowtodisk=" False "  
        timetoidleseconds=" 0 " 
        timetoliveseconds=" 0 "  
        memorystoreevictionpolicy=" LFU "/> 
</ehcache>

(ii) configuration of Spring-common files

Import namespaces

Xmlns:cache= "Http://www.springframework.org/schema/cache"

Http://www.springframework.org/schema/cache

Http://www.springframework.org/schema/cache/spring-cache-4.1.xsd

The specific configuration is as follows:

Enable cache annotation Feature

<cache:annotation-drivencache-manager= "CacheManager"/>

Cache Manager Factory Load Ehcache configuration

<bean id= "Cachemanagerfactory" class= "Org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >

<property name= "configlocation" value= "Classpath:ehcache.xml"/>

</bean>

Declaring the cache Manager

<bean id= "CacheManager" class= "Org.springframework.cache.ehcache.EhCacheCacheManager" >

<property name= "CacheManager" ref= "Cachemanagerfactory"/>

</bean>

To this environment has been built.

Next we'll see how to use

(iii) Use of @cacheable annotations to cache data

When there is no such object in the cache, of course, to access from the database, after detection from the database, the cache manager will put this object in the cache, the next visit, as long as the object does not die, it will be taken from the cache, no longer to check the database

Value for our custom cached Name,key property is the cached key

    @Cacheable (value= "Mycache", key= "#userName") 
    @Override public
    list<permission>findpermissionbyname (String userName) {
       System.out.println ("Check the Database ===========");
       Return Permissionrepository.findpermissionbyname (UserName);
   }

(iv) Use @cacheput annotations to update the cache

When the object in the cache is modified, the modified method uses the annotation, the cache manager finds the modified object based on the key, and then modifies the object in the cache.

    @CachePut (value= "Mycache", key= "#userId")//update Mycache cache 
    @Override public
    void Savepermission ( permissionpermission) {
       permissionrepository.savepermission (permission);
    }

(v) Use @cacheevict annotations to clear the cache

When the object is deleted, we need to clear the cache exists in the object, if not clear, the database has been deleted, but the cache is still in the query, the object will be found.

Clears the cache for the specified key

@CacheEvict (value= "Mycache", key= "#userName")

Clear out all cache

@CacheEvict (value= "Mycache", Allentries=true)


Full demo Download Address: http://www.demodashi.com/demo/10235.html

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.