先看源码
/**
* 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;
}
}
}