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

Local cache of springboot (guava and caffeine)

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

1. Scene description

Because the project needs to use local cache, why not use redis, etc., do not discuss, record the process, hoping to help friends in need.

two。 Solution 2.1 uses guava as the local cache

The initial idea is to use guava, because you already have guava's denpency in your project.

2.1.1 pom Fil

Three dependency are required, as follows:

Org.springframework.boot spring-boot-starter-cache org.springframework spring-context-support 4.3.18.RELEASE com.google.guava guava 24.0-jre 2.1.2 java Class

(1) GuavaCacheManager

Create a new bean, return and configure the GuavaCacheManager of guava.

Import com.google.common.cache.CacheBuilder;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.EnableCaching;import org.springframework.cache.guava.GuavaCacheManager;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.concurrent.TimeUnit;/*** * @ auther: software Lao Wang * / @ Configuration@EnableCachingpublic class GuavaCacheConfig {@ Bean public CacheManager cacheManager () {GuavaCacheManager cacheManager = new GuavaCacheManager () CacheManager.setCacheBuilder (CacheBuilder.newBuilder (). ExpireAfterWrite (6, TimeUnit.HOURS) MaximumSize (1000)); / / Software Lao Wang, 6 hours expired, up to 1000 return cacheManager;}}

(2) spring comment tags use caching

/ * @ auther: software Lao Wang * / @ Cacheable (value = "ruanjianlaowang") public VO getUserInfo (String token) {VO vo = userauthClient.verifyToken (token); return vo;}

The guava local cache is complete.

2.2 Local caching scheme provided by the spring framework

Abstract classes have been provided in spring. By default, you only need to load the jar package and configure type. But from spring5, the guava is removed and replaced with caffeine,spring. As a local cache, caffeine is much higher than guava, and we have changed it to caffeine as the local cache.

Springboot, guava, caffeine and other local caches, springboot1 and guava,springboot2 are removed from the guava cache interface, so the previous guava local cache needs to define Configuration. In this case, you only need to define the type in the configuration file and the type will be automatically injected.

2.3 caffeine local cache 2.3.1 pom file com.github.ben-manes.caffeine caffeine 2.8.0 2.3.2 configuration file spring: cache: type: caffeine cache-names: bc.gateway.ut caffeine:# spec: maximumSize=5000,expireAfterWrite=20s spec: maximumSize=5000,expireAfterAccess=3600s

The meaning of specific parameters:

/ * * @ auther: software Lao Wang * / initialCapacity= [integer]: initial cache space size maximumSize= [long]: maximum number of cache entries maximumWeight= [long]: maximum cache weight expireAfterAccess= [duration]: expired at a fixed time after the last write or access expireAfterWrite= [duration]: expired at a fixed time after the last write [duration]: create a cache or most A fixed time interval has elapsed after the last cache update Refresh cache 2.3.3 use or spring tag do not move: / * @ auther: software Lao Wang * / @ Cacheable (value = "ruanjianlaowang") public VO getUserInfo (String token) {VO vo = userauthClient.verifyToken (token) Return vo;} 2.4Notes on spring cache tags

Four tags:

(1) @ Cacheable, first query it in the cache. If not, enter the method execution, and return the value to cache after the execution is completed.

(2) @ CachePut, add or update cache content directly without query judgment, such as add or update

(3) @ CacheEvict, which is cleared directly, similar to delete.

(4) @ Caching,1,2,3 combinations

The name of the value cache, defined in the spring configuration file, must specify at least one such as: @ Cacheable (value= "laowang_cache1") @ Cacheable (value= {"" laowang_cache1, "laowang_cache2"} key cache key, which can be null. If you specify that you want to write according to the SpEL expression, if not specified, then by default all the parameters of the method are combined with @ Cacheable (value= "laowang_cache1", key= "# userName") condition cache conditions, which can be empty. Write in SpEL, return true or false, cache @ Cacheable only for true (value= "laowang_cache1", condition= "# userName.length () > 11")

Isimm "Software Lao Wang", if you think it's OK, update it in seconds! Welcome to discuss * *

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