In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "detailed explanation of the process of SpringBoot integrating SpringCache". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the detailed explanation of the process of SpringBoot integrating SpringCache".
1. About Spring Cache
Caching is becoming more and more important in today's applications.
Spring defines org.springframework.cache.Cache and org.springframework.cache.CacheManager interfaces from 3.1to unify different caching technologies, and supports the use of JCache (JSR-107) annotations to simplify our development.
With SpringCache, you can quickly embed your own Cache implementation, mainly with @ Cacheable, @ CachePut, @ CacheEvict, @ CacheConfig, @ Caching and other annotations.
@ Cacheable: acts on the method and is used to cache the result returned by the method. If the cache already exists, it can be obtained directly from the cache. The cached key can be specified from the input parameters, and the cached value is the returned value of the method. @ CachePut: acts on the method. The cache is re-added every time, regardless of whether the cache exists or not. The cached key can be specified from the input parameters. The cached value is the method return value and is often used for updating. CacheEvict: acts on the method to clear the cache. @ CacheConfig: acts on the class and uniformly configures the attributes of the cached annotations of this class. @ Caching: acts on the method and is used to set multiple caches at once. @ EnableCaching: acts on the class to enable the annotation function.
Second, demonstration examples
To use Spring Cache, you need to first introduce the dependency of Spring Cache.
Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-cache
Then on the startup class, we need to use @ EnableCaching to declare that caching is turned on.
@ EnableCaching / / enable caching @ SpringBootApplicationpublic class SpringbootApplication {public static void main (String [] args) {SpringApplication.run (SpringbootApplication.class, args);}}
This allows you to use annotations to manipulate the cache and create CacheService classes, where dataMap's Map stores data, eliminating database operations.
@ Slf4j@Servicepublic class CacheService {private Map dataMap = new HashMap () {{for (int I = 1; I < 100; iTunes +) {User u = new User ("code" + I, "name" + I); put (I, u);}; / / get data @ Cacheable (value = "cache", key = "'user:' + # id") public User get (int id) {log.info ("get through id {} query", id); return dataMap.get (id) } / / Update data @ CachePut (value = "cache", key = "'user:' + # id") public User set (int id, User u) {log.info ("Update id {} data", id); dataMap.put (id, u); return u;} / Delete data @ CacheEvict (value = "cache", key = "' user:' + # id") public User del (int id) {log.info ("Delete id {} data", id) DataMap.remove (id); return u;}}
The get method simulates the query, @ Cacheable is used to add the cache, the set method is used to modify, @ CachePut updates the cache, the del method is used to delete data, and @ CacheEvict is used to delete the cache. It is important to note that the annotated value represents the cache classification, not the value of the cached object.
Then create the CacheApi, which is used to call CacheService for testing.
@ RestController@RequestMapping ("cache") public class CacheApi {@ Autowired private CacheService cacheService; @ GetMapping ("get") public User get (@ RequestParam int id) {return cacheService.get (id);} @ PostMapping ("set") public User set (@ RequestParam int id, @ RequestParam String code, @ RequestParam String name) {User u = new User (code, name); return cacheService.set (id, u);} @ DeleteMapping ("del") public void del (@ RequestParam int id) {cacheService.del (id);}}
Then we open the swagger-ui interface (http://localhost:10900/swagger-ui.html) to test, call the query several times, and you can see that the get method of CacheService is executed only once for the same id. Then the update is called, and when you get again, you will find that the data has been updated, while when you call del, you can clear the cache, and the method will be called again after the query.
Source code address: https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache
Thank you for reading, the above is the content of "detailed explanation of the process of SpringBoot integrating SpringCache". After the study of this article, I believe you have a deeper understanding of the detailed explanation of the process of SpringBoot integrating SpringCache, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.