007-spring cache-Cache Implementation -02-springboot EHCAHE3 implementation, Springboot caffeine implementation

Source: Internet
Author: User

First, springboot EHCAHE3 implementation steps

EhCache is a pure Java in-process caching framework, which is fast and capable, and is the default Cacheprovider in Hibernate. Ehcache is a widely used, open source Java distributed cache. Primarily for general purpose caches, Java EE and lightweight containers. It features memory and disk storage, cache loaders, cache extensions, cache exception handlers, a gzip cache servlet filter, and support for rest and soap APIs.

Code Address: Https://github.com/bjlhx15/common/tree/master/spring-cache/spring-ehcache

1, Pom

2. Start-Up class annotations

3. Business class annotations

4. Configuration

# Configure Ehcache Cache Spring: Cache  :    jcache:      config:classpath:ehcache3.xml  # # #attention, this is Jcache

5. Ehchahe configuration File "More"

<?XML version= "1.0" encoding= "UTF-8"?><ConfigXmlns:xsi= ' http://www.w3.org/2001/XMLSchema-instance 'xmlns= ' Http://www.ehcache.org/v3 'xsi:schemalocation= "Http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xs">    <!--http://www.ehcache.org/documentation/3.0/xml.html -    <!--5, <cache-template> can let you create an abstract <cache> configuration file, the profile can be further expanded.  -    <cache-templatename= "Heap-cache">        <Resources>            <!--apply for 2000 entries in a heap -            <HeapUnit= "Entries">2000</Heap>            <!--Maximum non-heap memory -            <OffheapUnit= "MB">100</Offheap>        </Resources>    </cache-template>    <Cachealias= "User"uses-template= "Heap-cache">        <expiry>            <!--tti:time to idle, max idle time -            <!--<ttl> time to live; Max Survival -            <!--<class>, custom expiry implementation -            <!--<none> But the period -            <!--minutes, seconds -            <TTLUnit= "Seconds">10</TTL>        </expiry>        <!--<ehcache:key-type>java.lang.Long</ehcache:key-type> -        <!--<ehcache:value-type>com.pany.domain.Customer</ehcache:value-type> -        <!--<ehcache:heap unit= "entries" >200</ehcache:heap> -    </Cache></Config>
Second, springboot caffeine implementation steps

Caffeine is a rewritten version of the guava cache using JAVA8, which will replace guava in Spring Boot 2.0. If Caffeine,caffeinecachemanager appears, it will be configured automatically. You can use the Spring.cache.cache-names property to create a cache at startup, and you can customize it in the following configuration (in order):

    • SPRING.CACHE.CAFFEINE.SPEC: Special Cache defined
    • Com.github.benmanes.caffeine.cache.CaffeineSpec:bean definition
    • Com.github.benmanes.caffeine.cache.Caffeine:bean definition

Code Address: Https://github.com/bjlhx15/common/tree/master/spring-cache/spring-caffeine

1, Pom

2. Start-Up class annotations

3. Business class annotations

4. Configuration

Spring:  cache:    cache-names:user    Caffeine:      spec:initialcapacity=50,maximumsize=500, Expireafterwrite=10s,refreshafterwrite=5s

where spec configuration parameters

Initialcapacity=[integer]: Initial cache space size Maximumsize=[long]: Maximum number of bars cached Maximumweight=[long]: Maximum cache weight expireafteraccess=[ Duration]: Fixed time expires after last write or access expireafterwrite=[duration]: Last write after fixed time expires Refreshafterwrite=[duration]: Create cache or last update cache after a fixed time interval, flush cache Weakkeys: Open the weak reference of key weakvalues: Open the weak reference of value softvalues: Open soft Reference for value recordstats: developing statistical functions

Note: If you use the Refreshafterwrite configuration you must also specify a cacheloader, such as:

 PackageCom.lhx.spring.cache.caffeine;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;ImportCom.github.benmanes.caffeine.cache.CacheLoader; @Configuration Public classcacheloaderself {/*** This bean,refreshafterwrite=5s must be specified for this configuration property to take effect **/@Bean PublicCacheloader<object, object>Cacheloader () {Cacheloader<object, object> Cacheloader =NewCacheloader<object, object>() {@Override PublicObject load (Object key)throwsException {return NULL; }            //override this method to return the OldValue value back to refresh the cache@Override PublicObject reload (Object key, Object OldValue)throwsException {returnOldValue;        }        }; returnCacheloader; }}

Attention:

    • When Expireafterwrite and expireafteraccess colleagues are present, the expireafterwrite shall prevail.
    • MaximumSize and maximumweight can not be used simultaneously
    • Weakvalues and softvalues can not be used simultaneously

007-spring cache-Cache Implementation -02-springboot EHCAHE3 implementation, Springboot caffeine implementation

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.