Spring Cache caching framework

1. Preamble

Spring Cache is a standardized caching framework under the Spring system. Spring Cache has the following advantages:

Friends who are interested in knowing the content and more related learning materials, please like and collect + comment and forward + follow me, there will be a lot of dry goods later. I have some interview questions, architecture, and design materials that can be said to be necessary for programmer interviews!
All the materials are organized into the network disk, welcome to download if necessary! Private message me to reply [111] to get it for free

  • Many types of cache

There are many types of supported caches, and common caches such as Redis, EhCache, and Caffeine are all supported. They can be used independently or in combination.

  • smooth migration

The cache supported by Spring can realize seamless and smooth migration without modifying business logic. The implementation of annotation caching relies on dynamic proxies.

In most cases, the annotation version is used, and in a few cases, the programming version can also be used. The annotation version is highly decoupled from the business code, because it relies on dynamic proxy technology, and there are certain restrictions on usage scenarios. The programming version embeds business codes, and the codes are executed sequentially, without pre-conditions for use.

2. Basic concepts

(1) Core concepts

An application can have multiple cache managers, each cache manager can have multiple caches, and each cache can store multiple records.

1. Cache Manager

The storage medium of the cache is different, the cache is connected to different databases, and the serialization of the cache value is configured by the cache manager. There are primary and secondary cache managers, and the primary (primary) cache manager is used by default.

When there is only one CacheManager in the service, this cache manager is used by default; when there are more than one cache manager, annotations need to be used to Primaryspecify the default cache manager.

2. Cache

Cache is a collection of caches with the same configuration, which can be understood as a namespace. The cache life time under the Spring Cache system is based on Cache, and does not support setting the life time in units of Key. Different services correspond to different cache configurations, which should be distinguished at the cache.

CacheName should have significant business distinction and expiration time distinction, and it should be provided as a global constant, and it should be managed in a centralized manner. It is forbidden to use magic variables to specify CacheName.

(2) Supplementary content

Generally speaking, the cached Key and Value are both of String type, especially Value is usually serialized into a JSON string.

3. Annotated version

Used to manage cached data based on annotations. Annotation caching has the following advantages:

  • highly decoupled

Use annotations to implement caching, which is highly decoupled from business.

  • flexible management

Through global configuration without modifying the cache logic, the following effects can be achieved:

In the development environment, the cache can be disabled, and the traffic can be sent to the database to expose possible performance bottlenecks as early as possible; in the test environment, the cache can be enabled, and stress testing can be performed.

(1) Dynamic proxy

The principle of Spring Cache cache annotation version and typical cases of cache configuration failure.

1. CGLib dynamic proxy

The underlying technical support for caching is CGLib dynamic proxy, which adds corresponding caching operations before and after the target method call to achieve the operations of adding cache, updating cache, and deleting cache.

If the annotation cache configuration does not take effect, check whether the target calling method is dynamically proxied.

2. Configuration failure

Configuration invalidation means that although the cache annotation is configured, the cache is still not valid.

  • final class and final method

The final class and final method do not meet the conditions of CGLib dynamic proxy, so the cache configuration will be invalid.

  • internal call

Using dependency injection to call the method of configuring the cache takes effect, and internal calls between methods do not take effect.

  • non-public method

Non-public method configuration cache does not take effect.

(2) Commonly used annotations

1. Configuration Notes

(1)EnableCaching

Marked on the SpringBoot application startup class, adding this annotation means opening the Spring Cache cache; removing it means closing the cache. If the following configuration is added to the global configuration file, even if the EnableCaching annotation is marked on the startup class, the Spring Cache cache is then turned off.

spring:
  cache:
    type: none

If you customize the cache independent of the Spring container in the application, it will not be affected by this configuration.

(2)CacheConfig

Annotated on the class, more specifically on the business service class. Unified configuration of the following parameter information:

parameter meaning Instructions for use
cacheManager cache manager Default refers to the primary CacheManager
cacheNames cache name
keyGenerator key value generator

The configuration is uniformly performed on the class, and the methods under the class automatically inherit the corresponding configuration.

2. Caching annotations

(1)Cacheable

There are two cases for adding the core annotation of the cache: one is that the corresponding key value has no cached data, execute the method first, and then decide whether to add the cache according to the condition and unless conditions; the other is that the corresponding key value has been cached, and the method body is not executed. Return the data directly.

The parameters keyGeneratorand keyare mutually exclusive, and the configuration is automatically invalidated when keyit exists keyGenerator.

  • Basic parameters
parameter meaning Instructions for use
cacheManager cache manager Default refers to the primary CacheManager
cacheNames cache name
keyGenerator key value generator
key key value
  • advanced parameters
parameter meaning Defaults Instructions for use
condition cache condition Indicates that the party that meets the condition executes the cache operation, generally using parameters as the condition
unless negative cache When the condition is true, the return value of the method will not be cached
sync sync status false Indicates how to store the method execution result in the cache

(2)CachePut

Update cache annotations. Execute regardless of whether there is cached data for the corresponding key value.

  • Basic parameters
parameter meaning Instructions for use
cacheManager cache manager Default refers to the primary CacheManager
cacheNames cache name
keyGenerator key value generator
key key value
  • advanced parameters
parameter meaning Instructions for use
condition cache condition Indicates that the party that meets the condition executes the cache operation, generally using parameters as the condition
unless negative cache When the condition is true, the return value of the method will not be cached

(3)CacheEvict

Actively clear cache annotations.

  • Basic parameters
parameter meaning Instructions for use
cacheManager cache manager Default refers to the primary CacheManager
cacheNames cache name
keyGenerator key value generator
key key value
  • advanced parameters
parameter meaning Defaults Instructions for use
condition cache condition Indicates that the party that meets the condition executes the cache operation, generally using parameters as the condition
allEntries all caches false Indicates whether to clear all caches corresponding to the current CacheName
beforeInvocation before calling false Indicates whether to clear the cache before the method call

3、KeyGenerator

By default, the SimpleKeyGenerator key value generator is used. When no key value is specified, method parameters are converted into cached Key values ​​according to the generator rules.

Guess you like

Origin blog.csdn.net/m0_69424697/article/details/125152601