In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how spring redis annotations implement caching mechanisms. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.
1. Xml configuration
2. Cache annotations @ Cacheable, @ CacheEvict, @ CachePut
I. detailed explanation of the usage of @ Cacheable
1. Where is it used? Used in methods or classes.
2. What's the difference between these two uses?
The return value of this method will be cached.
Used on a class to indicate that all methods of the class support this annotation
3. What is the result after using it? The next time this method is called with the same method and the same parameters, the value will be taken directly from the cache without the need to execute the method again.
4. How is the returned value stored in the cache? Stored in the cache as key-value pairs, value is the return value, and key is generated by two policies: default policy and custom policy
5. How to use the default policy and default policy?
Default policy: double "::" after the value value, and use the json format when the parameter is an object:
CacheConfig (cacheNames= "enterprise") / / public interface EnterpriseRepo extends JpaRepository,JpaSpecificationExecutor {@ Cacheable (value= "cash2") Enterprise findByid (Integer id); @ CachePut (value= "cash2") Enterprise save (Enterprise enterprise);}
Custom policy: the key attribute is used to specify the key corresponding to the return result of the Spring cache method. This attribute supports SpringEL expressions. When we do not specify this attribute, Spring will generate the key using the default policy.
A custom policy means that we can specify our key through Spring's EL expression. The EL expressions here can use method parameters and their corresponding properties. When using method parameters, we can directly use "# parameter name" or "# p parameter index". Here are a few examples of using parameters as key.
@ Cacheable (value= "users", key= "# id") public User find (Integer id) {return null;} @ Cacheable (value= "users", key= "# p0") public User find (Integer id) {return null;} @ Cacheable (value= "users", key= "# user.id") public User find (User user) {return null @ Cacheable (value= "users", key= "# p0.id") public User find (User user) {return null;}
In addition to using the above method parameters as key, Spring also provides us with a root object that can be used to generate key. Through this root object, we can get the following information.
We can also omit "# root" when we want to use the properties of the root object as the key, because Spring uses the properties of the root object by default. Such as:
@ Cacheable (value= {"users", "xxx"}, key= "caches [1] .name") public User find (User user) {return null;}
6. The condition attribute specifies the conditions under which it occurs.
Sometimes we may not want to cache all the returned results of a method. You can do this through the condition property. The condition property is empty by default, indicating that all invocation scenarios will be cached. Its value is specified by an SpringEL expression, which means caching occurs when it is true; when it is false, it means that no caching processing occurs, that is, the method executes every time the method is called. The following example indicates that caching occurs only if the id of the user is even.
@ Cacheable (value= {"users"}, key= "# user.id", condition= "# user.id%2==0") public User find (User user) {System.out.println ("find user by user" + user); return user;}
2. @ CachePut
In an environment that supports Spring Cache, for the method marked with @ Cacheable, Spring checks whether the cache element of the same key exists in the Cache before each execution. If so, the method is no longer executed, but the result is directly obtained from the cache and returned, otherwise it is executed and the returned result is stored in the specified cache. @ CachePut can also declare that a method supports caching. Unlike @ Cacheable, the method using the @ CachePut annotation does not check whether there is a previously executed result in the cache before execution, but executes the method each time and stores the result in the specified cache in the form of a key-value pair.
It is generally used in save and update methods.
@ CachePut can also be annotated on classes and methods. When using @ CachePut, we can specify the same properties as @ Cacheable.
@ CachePut ("users") / / executes the method each time and stores the result in the specified cache
Public User find (Integer id) {return null;}
3. @ CacheEvict
@ CacheEvict is used to annotate methods or classes that need to clear cached elements. When marked on a class, it means that the execution of all methods in it triggers a cache cleanup operation. The properties that @ CacheEvict can specify are value, key, condition, allEntries, and beforeInvocation. The semantics of value, key, and condition are similar to the corresponding attributes of @ Cacheable. That is, value indicates the Cache on which the cleanup operation occurred (corresponding to the name of the Cache); key indicates which key needs to be cleared, and if not specified, the key;condition generated by the default policy is used to indicate the conditions under which the purge operation occurs. Let's take a look at the two new properties allEntries and beforeInvocation.
1. AllEntries attribute
AllEntries is a boolean type that indicates whether all elements in the cache need to be cleared. The default is false, which means it is not needed. When allEntries is specified as true, Spring Cache ignores the specified key. Sometimes we need to Cache all the elements at once, which is more efficient than clearing elements one by one.
@ CacheEvict (value= "users", allEntries=true) public void delete (Integer id) {System.out.println ("delete user by id:" + id);}
2. BeforeInvocation attribute
The cleanup operation is triggered by default after the corresponding method executes successfully, that is, the cleanup operation is not triggered if the method fails to return successfully because an exception is thrown. Using beforeInvocation, you can change the time when the cleanup operation is triggered, and when we specify the property value as true, Spring clears the specified element in the cache before calling the method.
@ CacheEvict (value= "users", beforeInvocation=true) public void delete (Integer id) {System.out.println ("delete user by id:" + id);} Thank you for reading! This is the end of how to implement the caching mechanism for spring redis annotations. I hope the above content can be helpful to you so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.
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.