Detailed Explanation of Cache Abstraction for Spring Framework Learning

  • 2021-11-24 01:27:54
  • OfStack

Directory 1. Introduction to cache and buffer2. Cache Abstraction 3. spring Cache Abstraction and Multiprocess

Official Document 8.0

Spring has done a layer of abstraction for different caches, and here I will make some study notes on the use and principle by reading the documents and source code.

1. Introduction

Beginning with version 3.1, Spring Framework provides support for transparently adding caches to existing Spring applications. Similar to transactional support, cache abstraction allows a variety of caching solutions to be used uniformly with minimal impact on code.

Beginning with Spring 4.1 cache abstraction has been significantly improved with support for JSR-107 annotations and additional customization options.

cache and buffer

In terms of terminology, "buffer" and "cache" can be replaced with each other. However, they do represent different things.

Traditionally, an buffer has been used as a temporary storage between fast and slow data. Because the fast side needs to wait for the slow side (which can affect performance), the buffer relieves pressure by allowing the whole block of data to move once instead of a small block of data moving 1 point 1 point 1. This data will only be written or read once from the buffer. In addition, the buffers can be seen on the total to 1 side.

Cache, on the other hand, by defining, hiding, and not making the other party aware of its existence, also improves performance, but this is achieved by having the same data read multiple times at the same time in a fast manner.

2. Cache abstraction

The core of cache abstraction is to apply the cache to the Java method, thereby reducing the number of executions based on the information available in the cache. That is, each time the target method is called, the abstraction applies the caching behavior to check whether the method has been executed for the given parameter. If so, the cached result is returned without executing the actual method; If not, the method is executed, the result is cached and returned to the user to return the cached result the next time the method is called. Thus, the expensive method (CPU or IO binding) can only be executed once for a given set of 1 parameters, and the results can be reused without actually executing the method again. Cache logic is applied transparently without any interference to the calling program.

Cache abstraction provides other cache-related operations, such as updating the contents of the cache or deleting one or all of them, which are useful if the cache changes frequently during the processing of data.

Like other services 1 in Spring Framework, the cache service is an abstraction (not a cache implementation) that requires actual storage to store cached data-that is, the abstraction eliminates the need for developers to write cache-related logic, but it does not provide actual data storage capability.

Through the interface org. springframework. cache. Cache (cache)

And org. springframework. cache. CacheManager (cache manager)

One implementation of this abstraction can be used directly: JDK java. util. concurrent. ConcurrentMap-based caches (i.e. the default cache is ConcurrentMap based on JVM), Ehcache 2. x, Gemfire caches, Caffeine, conforming and JSR-107 caches (e.g. Ehcache 3. x). For more information about inserting other cache stores/providers, see Inserting Different Backend Caches.

3. spring cache abstraction and multi-process

Note that:

The cache abstraction of Spring does not specifically deal with multithreading or multiple processes, which are handled by cache implementations.

If you have a multi-process environment (one application is deployed on multiple nodes), you need to configure programs that provide your cache accordingly. Depending on your use case, copying interlinked data from multiple nodes is sufficient. However, if data is modified during the application process, other mechanisms are needed to notify the changes.

Caching a dedicated object is an equivalent to a typical get-if-not-found-then-proceed-and-put-eventually code block, Is found through programmatic cache interactions. No locks are used and multiple threads concurrently fetch the same data. So is deletion (eviction). If multiple threads attempt to update or delete data concurrently, you may be using outdated (stale) data. 1 Some cache providers offer more advanced features. See more details in the documentation.

To use caching, you need to look at it from two aspects:

Declare caching: Identify the methods that need to be cached and the policies they use Configure the cache: Store data and then read it into the cache.

Above is the Spring framework learning Cache abstraction details, more information about Spring framework Cache abstraction please pay attention to other related articles on this site!


Related articles: