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

How to add SpringBoot to Guava Cache to realize local cache

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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.

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

Development

Wechat

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

12
Report