First, native realization 1.1, POM
<!--Cache - <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-cache</Artifactid> </Dependency>
1.2. Program entry or configuration class, turn on cache usage
@SpringBootApplication @enablecaching Public class Applicationmain { publicstaticvoid main (string[] args) { Springapplication.run (Applicationmain. class , args);} }
1.3, program use annotations
@CacheConfig【New in Spring 4 】 : Primarily used to configure some common cache configurations that are used in this class. Here @CacheConfig(cacheNames = "users") : The content returned in this data Access object is configured to be stored in a cache object named users, and we can also define it directly by the name of the cache set that we configure without using that annotation @Cacheable .
Configuration of 4 Annotation:
@Cacheable If spring has cached data before the method executes, if there is a direct return. If there is no data, the method is called and the method return value is stored in the cache.
= "#p0" = "#p0", Condition = "#p0. Length () < 3" Readers can experiment on their own. Unless: Another cache condition parameter, not required, requires the use of a spel expression. It differs from the condition parameter where it is judged by its timing, which is judged after the function is called, so it can be judged by the result. Keygenerator: Used to specify the key generator, not required. If you need to specify a custom key generator, we need to implement the Org.springframework.cache.interceptor.KeyGenerator interface and use this parameter to specify. It is important to note that this parameter is mutually exclusive with key CacheManager: Used to specify which cache manager to use, not required. You need to use cacheresolver only if there are multiple: used to specify that the cache parser is used, not required. The Org.springframework.cache.interceptor.CacheResolver interface is required to implement its own cache parser, which is specified with this parameter.
@CachePut In any case, put the fan Hu value of the method into the cache. Configured on a function, can be cached according to the parameters of the definition of the condition, it @Cacheable is different, it is really called function every time, so it is mainly used for data addition and modification operations. Its parameters and @Cacheable similar, the specific function can refer to @Cacheable the analysis of the parameters on the face
@CacheEvict Delete one or more data from the cache. Configured on a function, typically used on a delete method, to remove the data from the cache. In addition to the same parameters as @cacheable, it has the following two parameters:
Allentries: Not required, default is False. When True, all data beforeinvocation is removed: Not required, false by default, and data is removed after the method is called. When True, data is removed before the method is called.
@Caching can combine multiple annotation collections on a single method by @caching annotations
Code Address: Https://github.com/bjlhx15/common/tree/master/spring-cache/spring-org-cache
006-spring cache-Cache Implementation -01-native implementation