In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "Spring @ Cacheable how to read configuration constants", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "Spring @ Cacheable how to read configuration constants"!
Spring @ Cacheable reads the configuration constant property ①: valueString REDIS_DATABASE= "database"
Name for cacheable. By default, a double colon is added after the call, and the manual call is followed by::
Attribute ②: keyString REDIS_KEY_PREFIX= "'wtf_'"
Need to use single quotation mark'to prevent parsing by expression
Easy to use
@ Cacheable (value = RedisConstant.REDIS_DATABASE, key = (RedisConstant.REDIS_KEY_PREFIX + "+ # wtf")) Spring cache management (Cacheable)
The cache management of Spring is quite convenient. In line with the style of other features, add a comment where you need to manage, either a method or a class.
Using Spring Cache requires us to do two things:
Declare that some methods use caching
Configure Spring support for Cache
Like Spring's support for transaction management, Spring supports Cache in two ways: annotation-based and XML-based configuration. Let's take a look at the annotation-based approach.
Annotation-based support
Spring provides us with several annotations to support Spring Cache. Its core is @ Cacheable and @ CacheEvict. A method that uses the @ Cacheable tag caches the results returned by Spring Cache after execution, while a method that uses the @ CacheEvict tag removes some elements from the Spring Cache before or after the method execution. In the future, we will describe in detail several annotations provided by Spring based on its support for Cache.
@ Cacheable
@ Cacheable can be marked on a method or on a class. When the tag is on a method, it means that the method supports caching, and when the tag is on a class, it means that all the methods of that class support caching. For a method that supports caching, Spring caches its return value after it is called to ensure that the next time the method is executed with the same parameters, you can get the result directly from the cache without having to execute the method again. When caching the return value of a method, Spring is cached as a key-value pair, and the value is the return result of the method. As for keys, Spring supports two policies, default policy and custom policy, which will be explained later. It is important to note that caching is not triggered when a method that supports caching is called inside the object.
@ Cacheable can specify three properties, value, key, and condition.
The value property specifies the Cache name
The value attribute must be specified, which indicates the Cache on which the return value of the current method will be cached, corresponding to the name of the Cache. It can be a Cache or multiple Cache, and it is an array when you need to specify multiple Cache.
@ Cacheable ("cache1") / / Cache is public User find (Integer id) {return null;} @ Cacheable ({"cache1", "cache2"}) / / Cache is public User find (Integer id) {return null;} that occurs on cache1 and cache2
Customize key using the key property
The key property is used to specify the corresponding key when the result of the Spring cache method is returned. This attribute supports SpringEL expressions. When we do not specify this attribute, Spring will generate the key using the default policy. Let's take a look at the custom policy first, and the default policy will be described separately later.
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.
The attribute name describes the example methodName current method name # root.methodNamemethod current method # root.method.nametarget currently called object # root.targettargetClass the array of class#root.targetClassargs current method parameters of the currently called object # root.args [0] caches the Cache#root.caches [0] .name used by the currently called method
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;}
The condition attribute specifies the condition 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;} @ 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.
@ 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;} @ 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 occurred (corresponding to the name of the Cache)
Key indicates which key needs to be cleared. If not specified, the key generated by the default policy will be used.
Condition represents the condition under which the cleanup operation occurs.
Let's take a look at the two new properties allEntries and beforeInvocation.
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);}
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);}
In fact, in addition to using @ CacheEvict to clear cache elements, when we use Ehcache as the implementation, we can also configure Ehcache's own drive policy, which is specified through Ehcache's configuration file. Since Ehcache is not the focus of this article, I won't dwell on it here.
At this point, I believe you have a better understanding of "Spring @ Cacheable how to read configuration constants". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.