欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring boot 参考手册——第31章缓存阅读笔记和注意点

程序员文章站 2022-03-03 19:37:25
...
     Spring boot 参考手册——第31章缓存阅读笔记和注意点

基于Spring boot 1.5.19.RELEASE版本
码字不易,转载请注明出处

一、阅读笔记
1. Spring boot 只是在Spring框架提供的缓存抽象能力之上为我们做了一些自动配置的工作,
本质上Spring框架也并不提供缓存存储能力,只是提供了核心的抽像层接口Cache和CacheManager,方便集成第三方缓存提供商。
Spring Boot auto-configures the cache infrastructure as long as the caching support is enabled via the @EnableCaching annotation.


2. 如果在Spring boot应用中没有定义一个类型为CacheManager的Bean或类型为CacheResolver名称为cacheResolver的Bean,那么Spring boot按以下顺序检测缓存提供商:
  • Generic
  • JCache (JSR-107) (EhCache 3, Hazelcast, Infinispan, etc)
  • EhCache 2.x
  • Hazelcast
  • Infinispan
  • Couchbase
  • Redis
  • Caffeine
  • Guava (deprecated)
  • Simple

3. 如果需要在特定环境中完全禁用缓存,则使用此属性
spring.cache.type=none
,这个属性的值可以指定为:
public enum CacheType {

	/**
	 * Generic caching using 'Cache' beans from the context.
	 */
	GENERIC,

	/**
	 * JCache (JSR-107) backed caching.
	 */
	JCACHE,

	/**
	 * EhCache backed caching.
	 */
	EHCACHE,

	/**
	 * Hazelcast backed caching.
	 */
	HAZELCAST,

	/**
	 * Infinispan backed caching.
	 */
	INFINISPAN,

	/**
	 * Couchbase backed caching.
	 */
	COUCHBASE,

	/**
	 * Redis backed caching.
	 */
	REDIS,

	/**
	 * Caffeine backed caching.
	 */
	CAFFEINE,

	/**
	 * Guava backed caching.
	 */
	@Deprecated
	GUAVA,

	/**
	 * Simple in-memory caching.
	 */
	SIMPLE,

	/**
	 * No caching.
	 */
	NONE;

}

4.如果你是手动管理依赖,那么在使用JCache, EhCache 2.x or Guava 缓存时,必须要引入spring-context-support依赖。如果你使用了spring-boot-starter-cache POM,则spring-context-support依赖会自动引入。

5.Spring boot提供了对自动配置的CacheManager进行调整配置的扩展回调接口:CacheManagerCustomizer
public interface CacheManagerCustomizer<T extends CacheManager> {

	/**
	 * Customize the cache manager.
	 * @param cacheManager the {@code CacheManager} to customize
	 */
	void customize(T cacheManager);

}

.什么时候应用调整:在CacheManager的Bean完全初始化之前调整
.如何调整:通过实现CacheManagerCustomizer 接口,并将其暴露为一个bean。
If the CacheManager is auto-configured by Spring Boot, you can further tune its configuration before it is fully initialized by exposing a bean implementing the CacheManagerCustomizer interface. 


6.在使用RedisCacheManager时,Redis会有重叠键(两个独立的缓存空间,但是key名称一样)的可能,会返回无效的值。因此务必要开启key前缀的配置项
By default, a key prefix is added to prevent that if two separate caches use the same key, Redis would have overlapping keys and be likely to return invalid values. We strongly recommend to keep this setting enabled if you create your own RedisCacheManager.

7.Spring boot自动配置RedisCacheManager的配置类为RedisCacheConfiguration:
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
@ConditionalOnBean(RedisTemplate.class)
@ConditionalOnMissingBean(CacheManager.class)
@Conditional(CacheCondition.class)
class RedisCacheConfiguration {

	private final CacheProperties cacheProperties;

	private final CacheManagerCustomizers customizerInvoker;

	RedisCacheConfiguration(CacheProperties cacheProperties,
			CacheManagerCustomizers customizerInvoker) {
		this.cacheProperties = cacheProperties;
		this.customizerInvoker = customizerInvoker;
	}

	@Bean
	public RedisCacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate) {
		RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
		cacheManager.setUsePrefix(true);
		List<String> cacheNames = this.cacheProperties.getCacheNames();
		if (!cacheNames.isEmpty()) {
			cacheManager.setCacheNames(cacheNames);
		}
                //回调CacheManagerCustomizers 接口
		return this.customizerInvoker.customize(cacheManager);
	}


二、注意点
5.1.一定是Spring boot自动配置的CacheManager,上述CacheManagerCustomizer接口实现才会被回调

If that is not the case (either you provided your own config or a different cache provider was auto-configured), [color=red]the customizer won’t be invoked at all[/color]. 


5.2 可以添加多个CacheManagerCustomizer 回调,并用@Order注解实现顺序调用
You can have as many customizers as you want and you can also order them as usual using @Order or Ordered.