Java Project Development Experience (II): Using EHCACHE+SSM to implement data caching __java

Source: Internet
Author: User
Tags redis

In the development of the project, often consider to improve user access efficiency, reduce the pressure on the server, this time will use the data cache. There are a number of technologies currently implementing caching,

For example: Jcache, Ehcache, and caching server Redis,redis are important components of a distributed system, and the use of the technology and related issues are described in detail in later articles.

For now, I'm going to mention the consolidation implementation cache for Ehcache and spring projects.

Pre-Preparation: (Entity class, Dao, Service, Controller), ehcache configuration file

Related configuration of Ehcache

<?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= "1000"
        Overflowtodisk= "false" Diskpersistent= "false" timetoidleseconds= "0"
        timetoliveseconds= "600" memorystoreevictionpolicy= "LRU"/>
    <cache name= "Testcache" eternal= "false" maxelementsinmemory= "100"
        overflowtodisk= "false" Diskpersistent= "false" timetoidleseconds= "0"
        timetoliveseconds= "300" memorystoreevictionpolicy= "LRU"/>

</ehcache>


And, shortly thereafter, we need to configure the relevant beans in Applicationcontext.xml, as follows:

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

<!--find the location of the Ehcache.xml configuration file-->

    <property name= "

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

</bean>

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

<property name= "CacheManager" Cachemanagerfactory "/>

</bean>


<!--open Cache-->

<!--This particular note is the properties of mode, where the optional values are proxy and ASPECTJ, and the default is proxy. If you use a default value, the caching method can only act as a cache when it is invoked externally.

And when you choose Mode= "AspectJ" and proxy-target-class= "true", it is directly based on the class class, at which point the @cacheable annotation defined on the interface does not work

<cache:annotation-driven cache-manager= "CacheManager" mode= "AspectJ" proxy-target-class= "true" >

Then there is the code aspect:

<!--here Cachenames Select the cache--> configured in the Ehcache.xml file

Also in the MyBatis-related mapping file, which is the XML of the query statement, plus a log of the print cache data related

<cache type= "Org.mybatis.caches.ehcache.LoggingEhcache"/>   

Finally, the screenshot after the run:

(first query, go to the database query)



(second query, go to cached query)



The current version is the result of my reappearance, as the previous reader mentioned that there might be a problem with the new multiple caches, and that after ehcache2.5 CacheManager was Sington, so there would be no more caches for new, and the need to add < Property name= "Shared" value= "true" ></PROPERTY>

Ok, success. The above is about ehcache+spring configuration, if there is a problem, you can leave a message to me, we share learning.

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.