Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report