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 configure multiple caches using Caffeine and Spring Boot

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

Share

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

This article mainly introduces how to use Caffeine and Spring Boot to configure multiple caches, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.

Caching is key to the performance of almost all applications. Distributed caching is sometimes needed, but not always. In many cases, local caching works fine and does not require the overhead and complexity of distributed caching.

So in many applications, including plain Spring and Spring Boot, you can use @Cacheable on any method, and its results will be cached so that the next time the method is invoked, the cached results will be returned.

Spring has some default cache manager implementations, but external libraries are always better and more flexible than simple implementations. For example, Caffeine is a high-performance Java cache library. Spring Boot comes with a CaffeineCacheManager. Therefore, ideally, you would simply create a cache manager bean and cache your methods with @Cacheable annotations.

However, the cache manager provided allows you to configure only one cache specification. Cache specifications include expiration time, initial capacity, maximum size, etc. Therefore, all caches under this cache manager will be created using a single cache specification. The cache manager supports predefined cache lists as well as dynamically created caches, but uses a single cache specification in both cases. This is rarely useful for production. As a general rule, you must be careful with the built-in cache manager.

This article shows you how to define custom caches using custom specifications. However, these options do not support the dynamic default cache specification use case supported by the built-in manager. Ideally, you should be able to use any name @Cacheable and automatically create caches using some default specification, but you should also have the option to override the name of a particular cache.

This is why I decided to use a simpler approach than defining all caches in code to provide more flexibility. it extend that functionality provided by CaffeineCacheManager:

/** * Extending Caffeine cache manager to allow flexible per-cache configuration */public class FlexibleCaffeineCacheManagerextends CaffeineCacheManagerimplements InitializingBean { private Map cacheSpecs =new HashMap(); private Map builders =new HashMap(); private CacheLoader cacheLoader; @Override public void afterPropertiesSet()throws Exception { for (Map.Entry cacheSpecEntry : cacheSpecs.entrySet()) { builders.put(cacheSpecEntry.getKey(), Caffeine.from(cacheSpecEntry.getValue())); } } @Override @SuppressWarnings("unchecked") protected Cache createNativeCaffeineCache(String name) { Caffeine builder = builders.get(name); if (builder ==null) { return super.createNativeCaffeineCache(name); } if (this.cacheLoader != null) { return builder.build(this.cacheLoader); }else { return builder.build(); } } public Map getCacheSpecs() { return cacheSpecs; } public void setCacheSpecs(Map cacheSpecs) { this.cacheSpecs = cacheSpecs; } public void setCacheLoader(CacheLoader cacheLoader) { super.setCacheLoader(cacheLoader); this.cacheLoader = cacheLoader; }}

In short, it creates a caffeine builder for each specification and uses it instead of the default builder when a new cache is needed.

The sample XML configuration would then look like this:

Using Java configuration is very simple--you just need to set up cacheSpecs mapping.

While Spring has evolved into a massive framework offering a variety of features, it has not abandoned the design principle of extensibility.

Extending built-in framework classes is something that happens often and it should be in everyone's toolbox. These classes were created with extensions in mind--you'll notice a lot of protected methods in CaffeineCacheManagerare. So we should use it when we need it.

Thank you for reading this article carefully, I hope Xiaobian shared "How to use Caffeine and Spring Boot to configure multiple caches" This article is helpful to everyone, but also hope that everyone will support more, pay attention to the industry information channel, more relevant knowledge waiting for you to learn!

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