In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
SpringBoot how to cache, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Caching technology is a technology that makes all developers love and hate. We love caching because caching can bring us orders of magnitude of response and traffic, but the most fascinating thing is the most dangerous. If the cache is not used well, it will also be disaster-level. Especially for some businesses involving the company's main cash flow, if we bring a certain loss to the company because we use the cache improperly, it is no less than the big brother who deleted the database. Well, today, let's take a look at what things are available in springboot's cache. Learn, come little by little, and slowly accumulate your own experience in order to rise abruptly based on accumulated strength.
I. JSR107 cache specification
In order to unify the cache development specification and improve the scalability of the system, J2EE issued the JSR107 cache specification. Mainly Java Caching defines five interfaces, which are CachingProvider, CacheManager, Cache, Entry and Expiry.
Let's take a look at it separately and in detail.
CachingProvider:
Multiple CacheManager can be created, configured, obtained, managed, and controlled, and an Application can access multiple CachingProvider during operation.
CacheManager:
You can create, configure, get, manage, and control multiple uniquely named Cache that exist in the context of the CacheManager. A CacheManager is owned by only one CachingProvider.
Cache:
Is a data structure similar to Map and temporarily stores values indexed by Key. A Cache is owned by only one CacheManager.
Entry:
Is a Key-Value pair stored in Cache.
Expiry:
Each entry cached in Cache has a defined validity period. Once this time is exceeded, the entry will be expired, and once expired, the entry will be inaccessible, updated, and deleted. The validity period of the cache can be set through ExpiryPolicy.
If this explanation gives you a bit of a circle, it doesn't matter. Let's take a look at the picture below.
To sum up, there can be multiple cache providers (CachingProvider) in an application, a cache provider can obtain multiple cache managers (CacheManager), a cache manager manages different caches (Cache), there are cache key-value pairs (Entry) in the cache, and each entry has an expiration date (Expiry). The relationship between the cache manager and the cache is somewhat similar to the relationship between connection pools and connections in a database.
II. SpringBoot cache abstraction
In my own opinion, without all the theoretical explanations of the source code, it's all empty talk, or just bullshit, so let's take a look at the source-level operation of the cache.
Spring has defined org.springframework.cache.CacheManager and org.springframework.cache.Cache interfaces since version 3.1 to unify different caching technologies and supports the use of JSR-107 annotations to simplify development. In IDEA, when you quickly create a Spring Boot project using Spring Initializr, check Cache and configure debug=true in the configuration file to view the automatic configuration items of Spring Boot. The configuration classes for caching are as follows:
Org.springframework.boot.autoconfigure.cache.GenericCacheConfigurationorg.springframework.boot.autoconfigure.cache.JCacheCacheConfigurationorg.springframework.boot.autoconfigure.cache.EhCacheCacheConfigurationorg.springframework.boot.autoconfigure.cache.HazelcastCacheConfigurationorg.springframework.boot.autoconfigure.cache.InfinispanCacheConfigurationorg.springframework.boot.autoconfigure.cache.CouchbaseCacheConfigurationorg.springframework.boot.autoconfigure.cache.RedisCacheConfigurationorg.springframework.boot.autoconfigure.cache.CaffeineCacheConfigurationorg.springframework.boot.autoconfigure.cache.GuavaCacheConfigurationorg.springframework.boot.autoconfigure.cache.SimpleCacheConfigurationorg.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration
After starting the project, you can see that only the automatic configuration class SimpleCacheConfiguration is matched in the console, while in the SimpleCacheConfiguration class, a CacheManager is registered in the container with the @ Bean annotation, so you can see that the default CacheManager of Spring Boot is ConcurrentMapCacheManager.
SimpleCacheConfiguration matched:-Cache org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration automatic cache type (CacheCondition)-@ ConditionalOnMissingBean (types: org.springframework.cache.CacheManager; SearchStrategy: all) did not find any beans (OnBeanCondition)
Similarly, let's show it vividly through a picture.
If you enter the source code of @ Caching, you can see that cacheable, put, and evict can be used in the combined annotations.
Public @ interface Caching {Cacheable [] cacheable () default {}; CachePut [] put () default {}; CacheEvict [] evict () default {};}
The use of @ Caching
@ Caching (cacheable = {@ Cacheable (key = "# name")}, put = {@ CachePut (key = "# result.id"), @ CachePut (key = "# result.cNo")}) @ Cacheable, @ CachePut, @ CacheEvict
Key
# cached key, which can be empty or written using SpEL expressions: @ Cacheable (value= "stu", key= "userName")
Condition
# conditions for caching can be empty or written with SpEL expressions. Cache is cached / cleared only for true. # can be judged before and after method execution: @ Cacheable (value= "stu", condition= "userName.length () > 2")
Unless
# used to negate the cache, and can be judged only after the method is executed. You can also use SpEL expressions to write # true does not cache, false caches example: @ Cacheable (value= "stu", unless= "userName = = null")
@ Cacheable
Before the annotation method is executed, check to see if there is this data in the cache. By default, look up the data in the cache according to the value of the parameter as key. If you do not run this method and put the execution results of the method in the cache, and then call the method later, you can directly use the data in the cache.
@ CachePut
The annotated method must be executed, and it runs when the target method is called first, and then the result of the target method is put into the cache, but when updating the data in the cache, pay attention to the key value, otherwise the data in the cache cannot be updated.
@ CacheEvict
In this annotation, allEntries = true means that you want to clear all data in a cache. BeforeInvocation = false means that the cleanup of the cache is performed after the method is executed, and if an exception occurs, the data in the cache is not cleared. This is @ CacheEvict
Comment by default. BeforeInvocation = true means that the cleanup of the cache is performed before the method is executed, exceptions, and so on, and the data in the cache is also cleared.
Generation Strategy of key
The generation of key is generated by SimpleKeyGenerator by default, while the generation strategy of SimpleKeyGenerator is: if there is no parameter: key=new SimpleKey (); if there is a parameter: the value of the key= parameter if there are multiple parameters: key=new SimpleKey (params)
After reading the above, have you mastered how SpringBoot caches? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.