one , brief introduction

1, Cache introduction

Spring from 3.1 Right from the beginning Cache Support for . Defined org.springframework.cache.Cache and
org.springframework.cache.CacheManager Interface to unify different caching technologies . And support the use of JCache(JSR-107)
Annotations simplify our development .

Its use method and principle are similar to Spring Support for transaction management .Spring Cache
It works on the method , Its core idea is , When we call a cache method, we will store the method parameters and return results in the cache as a key value pair .



2,Cache and CacheManager Interface description 

*
Cache The interface contains various sets of cached operations , You operate the cache through this interface .

*
Cache Under the interface Spring Provides a variety of xxxCache Implementation of , such as :RedisCache,EhCache,ConcurrentMapCache

*
CacheManager Defined creation , to configure , obtain , Manage and control multiple uniquely named Cache. these Cache Exist in CacheManager In the context of .



Summary :

Every time a method that requires caching is called ,Spring
It will check whether the specified target method of the specified parameter has been called , If there is, get the result after the method call directly from the cache , If not, call the method and cache the result and return it to the user . Get the next call directly from the cache .



two ,@Cacheable Detailed introduction to the use of annotations

1, Cache usage steps

@Cacheable This annotation , It is used to use cache . So we can talk about the steps of using cache first :

1, Enable annotation based caching , use @EnableCaching Marked on SpringBoot On the main startup class of .

2, Annotation cache annotation is enough



① First step : Enable annotation based caching , use @EnableCaching Marked on springboot On the main startup class



② Step 2 : Annotation cache annotation

notes : Used here @Cacheable Annotations can cache the running results , Query the same data in the future , Get directly from cache , No need to call method .



2, Common attribute description

Let's introduce it @Cacheable Several common attributes of this annotation :

*
cacheNames/value : Used to specify the name of the cache component

*
key : Used when caching data key, It can be used to specify . The default is to use the value of the method parameter .( this key You can use spEL Expression )

*
keyGenerator :key Generator for . key and keyGenerator Use one out of two

*
cacheManager : Can be used to specify a cache manager . Which cache manager to get the cache from .

*
condition : It can be used to specify that the cache is only available when the conditions are met

*
unless : Negative cache . When unless The specified condition is true , The return value of the method will not be cached . Of course, you can also get the results to judge .( adopt #result
Get method results )

*
sync : Whether to use asynchronous mode .



① cacheNames

Used to specify the name of the cache component , In which cache is the return result of the method placed , It can be an array , Support specifying multiple caches .



② key

Used when caching data key. By default, the value of the method parameter is used . have access to spEL Expression to write .



③ keyGenerator

key Generator for , You can specify by yourself key Generator for , Generate through this generator key.



Put in the cache like this key The generation rules of are customized by you keyGenerator To generate . However, it should be noted that :

@Cacheable Properties of ,key and keyGenerator When using , Generally, one of two choices .



④ condition

Cache only when conditions are met . Should the data returned by the method be cached , Can make a dynamic judgment .



⑤ unless

Negative cache . When unless The specified condition is true , The return value of the method will not be cached .



⑥ sync

Whether to use asynchronous mode . The default is that the method is completed , Store the results returned by the method in the cache synchronously .

3,spEL to write key

As I said before , Cached key Support use spEL Expression to write , Here is a summary of the use spEL To write key Some metadata that can be used :



Technology