Search in sources :

Example 1 with NoSuchCacheException

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;
}
Also used : NoSuchCacheException(cn.taketoday.cache.NoSuchCacheException) Cache(cn.taketoday.cache.Cache)

Example 2 with NoSuchCacheException

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) {
    }
}
Also used : CaffeineCacheManager(cn.taketoday.cache.support.CaffeineCacheManager) NoSuchCacheException(cn.taketoday.cache.NoSuchCacheException) Method(java.lang.reflect.Method) CacheConfiguration(cn.taketoday.cache.annotation.CacheConfiguration) Cache(cn.taketoday.cache.Cache) CaffeineCache(cn.taketoday.cache.support.CaffeineCache) Test(org.junit.jupiter.api.Test)

Aggregations

Cache (cn.taketoday.cache.Cache)2 NoSuchCacheException (cn.taketoday.cache.NoSuchCacheException)2 CacheConfiguration (cn.taketoday.cache.annotation.CacheConfiguration)1 CaffeineCache (cn.taketoday.cache.support.CaffeineCache)1 CaffeineCacheManager (cn.taketoday.cache.support.CaffeineCacheManager)1 Method (java.lang.reflect.Method)1 Test (org.junit.jupiter.api.Test)1