To be perfected..

If you are familiar with the Spring transaction module, spring Cache will be much easier to understand. The implementation of Spring Cache is basically the same as things in terms of framework API abstraction. It is implemented using AOP. To minimize service intrusion.

Similar to the transaction support, the caching abstraction allows consistent use of various caching solutions with minimal impact on the code.

Annotations parsing

In the above case, there are several annotations to be concerned about: @cacheconfig, @cacheput, @cacheable, @cacheevict, as well as the attributes of these annotations; Here’s a look at how these annotations are parsed and how they work.

@CacheConfig

attribute meaning case
cacheNames/value Name of cache Example: @cacheput (value= “mycache”) @cachePUT (cacheNames={” cache1 “, “cache2”}
keyGenerator The key generator
cacheManager Cache manager
cacheResolver A cache instance used to intercept method calls

@CachePut

attribute meaning case
cacheNames/value Name of cache Example: @cacheput (value= “mycache”) @cacheput (value={” cache1 “, “cache2”}
key The key of cache For example: @ CachePut (value = “testcache”, # key = “userName”)
condition Conditions for caching For example: @ CachePut (value = “testcache”, condition = “# userName. The length () > 2”)

@Cacheable

attribute meaning case
cacheNames/value Name of cache Each cache name represents a cache object. Multiple cache objects are created when a method fills in multiple cache names. Caches with the same parameter are overwritten when multiple methods use the same cache name. Therefore, we usually use “package name + class name + method name” or use RequestMapping of the interface as the cache name to prevent problems caused by name duplication. Single cache name: @cacheable (value= “myCache”) Multi-cache name: @cacheable (value={” cache1 “, “cache2”}
key The key of cache Key marks each cache under the cache object. If you do not specify a key, the system automatically generates a key for all the input parameters of the method, meaning that the same input parameter value will return the same cached result. If key is specified, the list of incoming arguments used is written as an SpEL expression. The same cached result is returned as long as the userName value is the same, no matter how many input arguments the method has. Testcache @ Cacheable (value = “, “# key =” userName “)
condition Conditions for caching Method results are cached only when the conditions are met. If this parameter is not specified, the cache is considered unconditional. The condition is written using SpEL expressions and returns true or false. Only true is cached. @cacheable (value= “testCache”,condition= “#userName. Length ()>2”)

@CacheEvict

attribute meaning case
cacheNames/value Name of cache Deletes the cache object with the specified name. It must be used in conjunction with one of the following parameters for example: @cacheevict (value= “mycache”) or @cacheevict (value={” cache1 “, “cache2”}
key The key of cache Example: @cacheevict (value= “testCache”,key= “#userName”)
condition Conditions for caching Example: @cacheevict (value= “testCache”,condition= “#userName. Length ()>2”)
allEntries Method clears all caches after execution The default is false, and if true is specified, all caches are cleared immediately after the method call. For example: @ CacheEvict (value = “testcache”, allEntries = true)
beforeInvocation Clear all caches before the method executes The default is false. If true, the cache is cleared before the method is executed. By default, the cache is not cleared if an exception is thrown by method execution. Example: @cacheevict (value= “testcache”, beforeInvocation=true)

interception

Methods to perform

conclusion

Think about the benefits of this design in terms of interface + implementation separation, and what implications it has for our daily work

Refer to the link

  • Docs. Spring. IO/spring – the boot…
  • Docs. Spring. IO/spring – fram…