In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use multiple dynamic caches in Spring. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
CompositeCacheManager in theory, Spring should allow the use of multiple cache managers. It works by asking if the underlying cache manager has a cache with the requested name. The problem is when you need a cache that is dynamically created based on some global configuration. This is a common situation when you don't want to define the cache manually, but just want to add @ Cacheable and have spring (and the underlying cache manager) create the cache for you using some reasonable default values.
This is good until you need to have multiple cache managers. For example: one for local caching and one for distributed caching. In many cases, distributed caching is required; however, not all method calls need to be distributed-some method calls can be local calls to handle its instances. and you don't want to burden the distributed cache with something that can be saved locally. Whether you can configure a distributed cache provider to specify some caches as local caches, even if it is handled by a distributed cache provider-- maybe, but I don't guarantee it will be easy.
Therefore, faced with this problem, I had to design some simple mechanisms to specify some caches as "distributed" and some as "local". CompositeCacheManager alone won't do this, so I extended the distributed cache manager (in this case, Hazelcast, but it can be done through any provider):
/ * Hazelcast cache manager that handles only cache names with a specified prefix for distributed caches * / public class OptionalHazelcastCacheManager extends HazelcastCacheManager {private static final String DISTRIBUTED_CACHE_PREFIX = "d:"; public OptionalHazelcastCacheManager (HazelcastInstance hazelcast) {super (hazelcast);} @ Override public Cache getCache (String name) {if (name = = null | |! name.startsWith (DISTRIBUTED_CACHE_PREFIX)) {return null;} return super.getCache (name) }}
And the corresponding compound cache manager configuration:
This basically means that any cache whose name starts with d: ("distributed") should be handled by the distributed cache manager. Otherwise, proceed to the next cache manager (in this case, Caffeine). So when you want to define a method with cacheable results, you have to decide whether it is @ Cacheable ("d:cachename") or just @ Cacheable ("cachename").
This may be one of the many ways to solve the problem, but I like its simplicity. Caching is difficult (especially distributed caching), and while we are lucky to have Spring abstracting most of the content, sometimes we have to deal with special cases ourselves.
Thank you for reading! This is the end of this article on "how to use multiple dynamic caches in Spring". I hope the above content can be of some help to you, so that you can 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.