Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

A brief introduction to springboot caffine caching and what demo is like

Shulou Source: shulou.com Published: 2022-06-02 03:33:47 10月04日 Update

This article will give you a detailed introduction to springboot caffine caching and how demo is. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Caffeine is a rewritten version of Guava cache using Java8, which will be replaced in Spring 5.0or Spring Boot 2.0.It is implemented based on the LRU algorithm and supports multiple cache expiration policies. So why does such a good thing need to be eliminated? people who have done in-depth research on local Cache should know that LRU algorithm can basically meet most scenarios, but many people put forward a series of better and more effective elimination strategies based on LRU algorithm in order to improve. For example, ARC,LIRS and W-TinyLFU provide near-optimal hit rates, and these algorithms further improve the efficiency of local caching. Caffeine configuration description: initialCapacity= [integer]: initial cache space size maximumSize= [long]: maximum number of cache entries maximumWeight= [long]: maximum cache weight expireAfterAccess= [duration]: after the last write or access expires at a fixed time expireAfterWrite= [duration]: after the last write expires for a fixed time refreshAfterWrite= [long]: after the creation of the cache or the last update of the cache after a fixed time interval Refresh cache weakKeys: open key's weak reference weakValues: open value's weak reference softValues: open value's soft reference recordStats: develop statistics function Note: when expireAfterWrite and expireAfterAccess exist at the same time, expireAfterWrite shall prevail. MaximumSize and maximumWeight cannot use weakValues and softValues at the same time. SpringbootApplication cannot be used to enable caching support: @ EnableCaching// enables caching. The specified maven that needs to be displayed is introduced: org.springframework.boot spring-boot-starter-cache com.github.ben-manes.caffeine caffeinedemo:import com.github.benmanes.caffeine.cache.Caffeine;import org.springframework.cache.caffeine.CaffeineCacheManager;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.ArrayList Import java.util.List;import java.util.concurrent.TimeUnit;@Configurationpublic class CacheConfig {private static final int DEFAULT_MAXSIZE = 10; private static final int DEFAULT_TTL = 15 * 60; / * * Personalized configuration Cache * / @ Bean (name = "caffeineCache") public CaffeineCacheManager cacheManager () {CaffeineCacheManager cacheManager = new CaffeineCacheManager () Caffeine caffeine = Caffeine.newBuilder () / initial capacity of cache .initialCapacity (10) .initireAfterWrite (DEFAULT_TTL,TimeUnit.SECONDS) / / maximumSize is used to control the maximum number of caches for cache. MaximumSize and maximumWeight cannot be used at the same time, .maximumSize (DEFAULT_MAXSIZE); cacheManager.setCaffeine (caffeine); cacheManager.setCacheNames (getNames ()); return cacheManager } private static List getNames () {List names = new ArrayList (2); names.add ("datainterface"); return names;}}

This is the end of the brief introduction of springboot caffine cache and how demo is shared here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Cache at the same time algorithm maximum time introduction content article more knowledge strategy article support configuration good excellence thing personality lieutenant general function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft Docker Huawei MariaDB