Spring Cache Concept
Starting with Spring 3.1, a transparent way to add cache to existing Spring applications is provided, which is used like @Transaction. There is a layer of abstraction between the application level and backend storage. This layer of abstraction is designed to encapsulate various pluggable backend storage (Ehcache Guava Redis) to minimize the intrusion caused by cache to existing business code.
Spring's caching technology also has considerable flexibility. Not only can it use SpEL (Spring Expression Language) to define cache keys and various conditions, it also provides out-of-the-box cache temporary storage solutions, and also supports integration with mainstream professional caches such as EHCache.
Its characteristics are summarized as follows:
Design concept
Just like other services of the Spring framework, Spring cache first provides a layer of abstraction, and the core abstraction is mainly reflected in the two interfaces.
org.springframework.cache.Cache
org.springframework.cache.CacheManager
Cache stands for cache itself
CacheManager represents the processing and management of caches, etc. The meaning of abstraction is to block the differences in implementation details and provide scalability. This layer of Cache abstraction decouples the use of cache and the back-end storage of cache, so that the back-end storage can be replaced easily in the future.
Using Spring Cache in three steps:
Declare cache
@Cacheable("books")public Book findBook(ISBN isbn) {...} The usage is very simple. Add @cacheable and other annotations to the method to cache the result of the method.
When a method is called, first check whether there is any call to the same parameter in the cache for the method. If so, query from the cache and return the result. If not, execute the specific method logic and cache the result to cache. Of course, this series of logic is transparent to the caller. The notes for other cache operations are as follows (see the official documentation for details):
Turn on Spring Cache support
<cache:annotation-driven />
Or use the annotation @EnableCaching method
Configure cache backend storage
Spring Cache provides several built-in backend storage implementations: the following are the specific implementations of CacheManager.
Additionally, Spring Data provides two cache managers:
If you use distributed caches such as memcached or redis, you can implement Cache and CacheManager yourself and declare it in the Context. If multiple different cache implementations are required, various CacheManagers can be encapsulated together in a combination mode.
How is cached key generated
We all know that the storage method of cache is generally the key value. So in Spring cache, how is the key set? Here we need to introduce KeyGenerator, which is responsible for the key generation strategy, and the default is to use SimpleKeyGenerator
It can be seen that it is the hash value of the ordered parameter array. Of course, users can customize the key generation strategy.
Implementation of Spring Cache
The above is the general usage of Spring cache, which shows how Spring is implemented.
When learning Spring source code, there are two things to remember:
Remember these and it is easier to understand the implementation and runtime mechanism of some components in Spring.
Spring cache is no exception. It is a typical Spring AOP implementation. In Spring, aop can be simply understood as a proxy (except AspectJ). We declare that the class of @Cacheable method will be proxyed, and in the proxy, cached query and setting operations are implemented.
Creation of Cache Infrastructure
The previous article (Overview of Spring AOP module) talked about the fact that the creation process of Spring AOP is essentially to implement a BeanPostProcessor, create a proxy in the process of creating a bean, and bind all advisors applicable to the bean for the proxy, and ultimately expose it to the container.
Several key concepts of AOP master in Spring advisor advice pointcut
advice = behavior of insertion in section interception
pointcut = pointcut to the section
advisor = advice + pointcut
Spring cache also has similar processes as other AOPs
Create a cache proxy
Cache's intercept line
The cache proxy object generated in Spring cache uses the CacheProxyFactoryBean factory class. Generally speaking, the creation of standard proxy in Spring is based on ProxyFactoryBean. Here, in order to more conveniently handle cache logic, Spring introduces CacheProxyFactoryBean to specifically represent cache-related proxy. cache proxy can wrapper singleton target objects and proxy all interfaces implemented by the target objects.
As you can see, in the CacheProxyFactoryBean, an important property is CacheInterceptor. This class is an implementation class of MethodInterceptor. The responsibility of this class is to perform specific cache operations on the target object target method, which is the responsibility of the advice mentioned above.
Continue to follow, the execute method of return is the method in the parent class CacheAspectSupport
In this method, we finally find the final logic of the operation cache
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.