In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to cache Spring. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
In order to reduce the pressure on the database, we put the data in the cache and read it directly from the cache when the request for access comes. Caches are generally based on memory, and the reading speed is relatively fast. There are more common caches on the market: memcache, redis, mongodb, guava cache and so on.
General use of caching
When you use caching, the common logic is as follows:
Generate key according to condition
Read the data from the cache, and if the data is read successfully, return
If the data does not exist, read it from the database according to the condition
Put data read from the database into the cache
Return data
For every scenario that uses caching, the above logic has to be rewritten, whether it is irritable or a waste of time. Is there an easy way to complete the above logic? Of course there is. This is the Spring Cache I'm going to introduce to you today.
Spring Cache
Spring Cache is not mysterious, and it is very easy to use. It is made up of annotations, and the most commonly used annotation is @ Cacheable. This annotation is used on the method, and when the annotated method is called, it is first queried from the cache, if it does not exist in the cache, the method is executed, and then the return value of the method is put into the cache. The specific usage is as follows:
First of all, we use Spring Boot to build the environment in IDEA. On the page of selecting dependencies, we select Lombok and Cache, and the main choice is Cache. After the project is built, let's take a look at the dependencies of pom.xml:
We see that cache is automatically added to the dependency. Next, we will add the annotation @ EnableCaching using cache to the startup class of SpringBoot, as shown in the figure:
Then we write the controller of the test, as follows:
We use the @ RestController annotation, so it returns the result in Json format. Then we use the @ Cacheable annotation on the method, which is our protagonist today.
CacheNames: when there are multiple caches in the system, specify which caches are used by this method.
Key: cached key, you can use spEL expressions. In the above example, the input parameter name is used.
There are other keywords that are not listed here, such as:
Sync: true or false, when the concurrency is very large, synchronization is turned on, ensuring that only one thread executes the method, and the other threads will wait and then read the data from the cache.
Condition: the condition for using caching.
KeyGenerator: specifies the generator of the key.
We started the project and visited http://localhost:8080/cache/test?name=allen for the first time in the browser. As a result, the response was slow. After 5 seconds, the page displayed the result:
Let's take a look at the background log again:
The print statement is printed, indicating that the method was executed the first time it was accessed. We request the same address in the browser again, and the result is the same result, and the log is not printed in the background, which is exactly the same as the above two pictures. Indicates that the request has been cached and the method has not been executed.
On how to cache Spring to share here, I hope the above content can be of some help to 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.