Get the App
SLTechnology News&Howtos  ›  Development  › 

How to add SpringBoot to Guava Cache to realize local cache

Shulou Source: shulou.com Published: 2022-06-01 00:47:14 10月04日 Update

Most people don't understand the knowledge points of this article, so Xiaobian summarizes the following contents for everyone. The content is detailed, the steps are clear, and it has certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article.

Add guava dependency to pom.xml

com.google.guava guava 18.0

Create a CacheService that is easy to call

public interface CacheService { //Save void setCommonCache(String key,Object value); //Get Object getCommonCache(String key);}

Its implementation class

import com.google.common.cache.Cache;import com.google.common.cache.CacheBuilder;import com.wu.service.CacheService;import org.springframework.stereotype.Service;import javax.annotation.PostConstruct;import java.util.concurrent.TimeUnit;@Servicepublic class CacheServiceImpl implements CacheService { private Cache commonCache=null; @PostConstruct//proxy This initialization method is executed first when this bean is proxied public init(){void commonCache= CacheBuilder.newBuilder() //Set the initial capacity of the cache container to 10 (10 key-value pairs can be stored) .initialCapacity(10) //Maximum cache capacity is 100, LRU policy will be installed after exceeding 100-least recently used, specific Baidu-remove cache items .maximumSize(100) //Set expiration 1 minute after write cache .expireAfterWrite(60, TimeUnit.SECONDS).build(); } @Override public void setCommonCache(String key, Object value) { commonCache.put(key,value); } @Override public Object getCommonCache(String key) { return commonCache.getIfPresent(key); }} The above is about "SpringBoot how to join Guava Cache to achieve local cache" This article's content, I believe everyone has a certain understanding, I hope the content shared by Xiaobian is helpful to everyone, if you want to know more related knowledge content, please pay attention to the industry information channel.

Tags: Cache content capacity articles knowledge articles maximum value most Storage items containers that is methods more steps knowledge points Strategies Industry Information Information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi vpn Microsoft Docker Shulou Technology