spring boot@Cacheable中value的理解

本文深入探讨了Spring框架中的缓存管理机制,特别是如何通过@Cacheable注解配置缓存名称,并介绍了缓存是如何被自动管理和创建的过程。此外,还解释了即使未明确指定缓存名称,Spring也能正确处理缓存的原理。

先看源码

/**
	 * Names of the caches in which method invocation results are stored.
	 * <p>Names may be used to determine the target cache (or caches), matching
	 * the qualifier value or bean name of a specific bean definition.
	 * @since 4.2
	 * @see #value
	 * @see CacheConfig#cacheNames
	 */
	@AliasFor("value")
	String[] cacheNames() default {};

意思是表示一个对象,我用的redis做缓存,才开始认为是redisTemplate beanName。

后来发现名字随便起都可以,比如这样@Cacheable(value = "test1")。

测试缓存是由效果的,第一次没走缓存,第二次走了缓存。

我设置了ket='xtj1',但是在redis中用keys * 并没有发现这个key。

后来终于找到了:


竟然自动创建了一个文件夹。


之所以value可以随便指定的原理:

org.springframework.cache.support.AbstractCacheManager

@Override
	@Nullable
	public Cache getCache(String name) {
		Cache cache = this.cacheMap.get(name);
		if (cache != null) {
			return cache;
		}
		else {
			// Fully synchronize now for missing cache creation...
			synchronized (this.cacheMap) {
				cache = this.cacheMap.get(name);
				if (cache == null) {
					cache = getMissingCache(name);
					if (cache != null) {
						cache = decorateCache(cache);
						this.cacheMap.put(name, cache);
						updateCacheNames(name);
					}
				}
				return cache;
			}
		}
	}



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值