use of cn.taketoday.cache.NoSuchCacheException in project today-framework by TAKETODAY.
the class AbstractCacheInterceptor method obtainCache.
/**
* Obtain a Target method's {@link Cache} object
*
* @param method Target method
* @param cacheConfig {@link CacheConfig}
* @return {@link Cache}
* @throws NoSuchCacheException If there isn't a {@link Cache}
*/
protected Cache obtainCache(Method method, CacheConfig cacheConfig) {
String name = prepareCacheName(method, cacheConfig.cacheName());
Cache cache = getCache(name, cacheConfig);
if (cache == null) {
throw new NoSuchCacheException(name);
}
return cache;
}
use of cn.taketoday.cache.NoSuchCacheException in project today-framework by TAKETODAY.
the class CacheableInterceptorTests method cacheableAttributes.
@Test
void cacheableAttributes() throws Exception {
CacheExpressionOperations operations = new CacheExpressionOperations();
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
CacheableInterceptor interceptor = new CacheableInterceptor(cacheManager);
interceptor.setExceptionResolver(new DefaultCacheExceptionResolver());
// cacheName
Method getUser = CacheUserService.class.getDeclaredMethod("getUser", String.class);
MethodKey methodKey = new MethodKey(getUser, Cacheable.class);
CacheConfiguration cacheable = operations.getConfig(methodKey);
Cache users = interceptor.getCache("users", cacheable);
assertThat(users).isInstanceOf(CaffeineCache.class);
assertThat(users.getName()).isEqualTo("users");
// key
Cache cache = interceptor.obtainCache(getUser, cacheable);
assertThat(cache).isEqualTo(users);
CacheConfiguration cacheableClone = new CacheConfiguration();
cacheableClone.mergeCacheConfigAttributes(cacheable);
cacheableClone.setCacheName("users1");
Cache users1 = interceptor.obtainCache(getUser, cacheableClone);
assertThat(users1).isNotEqualTo(cache).isNotEqualTo(users);
cacheManager.setDynamicCreation(false);
cacheableClone.setCacheName("users2");
try {
Cache users2 = interceptor.obtainCache(getUser, cacheableClone);
fail("obtainCache error");
} catch (NoSuchCacheException ignored) {
}
}
Aggregations