1. 程式人生 > >spring [email protected]中value的理解

spring [email protected]中value的理解

先看原始碼

/**
	 * 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;
			}
		}
	}