About the use of the Ehcache cache (simple vs. Redis)

Source: Internet
Author: User

Objective

Recently in doing a project, an interface to query data to return the total data needs 7000+ milliseconds, has been considering the optimization of the problem, optimization also has the original of a query to query all, in the query for different days. The result is 1500+, although the optimization of a lot of, but the data structure will change, the foreground rendering information will be more cumbersome, and temporarily did not update. So later adopted a caching mechanism, the query data cache 10 hours, although the first query is slow, but later will be much better.

Body 1. About selection: Redis or Ehcache

When using the cache, the first reaction was: Redis, but then decided to use Ehcache,ehcache primarily to cache some simple data. Redis is too heavy and requires a server.

For a comparison of Redis and Ehcache:

    • Ehcache is cached directly in the JVM virtual machine, fast and efficient, but the cache sharing is troublesome, the cluster distributed application is inconvenient.
    • Redis is accessed through the socket to the cache service, the efficiency is lower than ecache, much faster than the database, processing clusters and distributed cache convenient, has a mature program. If it is a single application or a high-demand application for cache access, use Ehcache. Redis is recommended for large systems where cache sharing, distributed deployment, and cache content are large.

It is important to note that Spring provides an abstraction of the caching feature: the ability to bind different cache solutions (such as Ehcache), but does not provide the implementation of the caching functionality directly. It is very convenient to use the cache in annotation mode.

2. Use of Ehcache

Overall process:

    • Add dependency
    • Configuring Bean:ehcachemanagerfactorybean and CacheManager
    • Add annotations on the method that requires caching: @Cacheable (cachenames = "Autotransmission"): The name needs to be used in XML
    • Configures the cache information in the cache XML file.

Add dependency

<dependency>     <groupId>net.sf.ehcache</groupId>     <artifactid>ehcache</ Artifactid>     <version>2.10.4</version></dependency>

Configuring Bean:ehcachemanagerfactorybean and CacheManager

@Configuration @enablecaching Public classEhcacheconfig {Private Static FinalLogger Logger = Loggerfactory.getlogger (ehcacheconfig.class); @Bean PublicEhcachemanagerfactorybean Ehcachemanagerfactorybean () {Ehcachemanagerfactorybean EhCacheManagerFactoryBean 
    =NewEhcachemanagerfactorybean (); Ehcachemanagerfactorybean.setconfiglocation (NewClasspathresource ("Ehcache.xml")); Root directory configuration file locationreturnEhcachemanagerfactorybean; } @Bean PublicCacheManager CacheManager () {Logger.info ("Ehcachecachemanager"); Ehcachecachemanager CacheManager=NewEhcachecachemanager ();        Cachemanager.setcachemanager (Ehcachemanagerfactorybean (). GetObject ()); returnCacheManager; }}

Add annotations on the method that requires caching: @Cacheable (cachenames = "Autotransmission"): The name needs to be used in XML

// Cache, Save 10 hours     Public Ajaxjson autotransmissionsevendays (String industrycode) {

Configures the cache information in the cache XML file.

<?XML version= "1.0" encoding= "UTF-8"?><EhcacheXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:nonamespaceschemalocation= "Http://ehcache.org/ehcache.xsd"UpdateCheck= "false">    <DiskstorePath= "Java.io.tmpdir/tmp_ehcache"/>    <DefaultcacheEternal= "false"maxelementsinmemory= "+"Overflowtodisk= "false"diskpersistent= "false"Timetoidleseconds= "0"Timetoliveseconds= "All"Memorystoreevictionpolicy= "LRU"/>    <Cachename= "Autotransmission"Eternal= "false"maxelementsinmemory= "+"Overflowtodisk= "false"diskpersistent= "false"Timetoidleseconds= "0"Timetoliveseconds= "36000"Memorystoreevictionpolicy= "LRU">    </Cache><!--Where: Name is the method name, Timetoliveseconds: Is the total amount of time that the cache exists. -
</Ehcache>

To this cache is already ready to use.

About the use of the Ehcache cache (simple vs. Redis)

Related Article

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.