Search in sources :

Example 1 with CacheConfiguration

use of cn.taketoday.cache.annotation.CacheConfiguration in project today-framework by TAKETODAY.

the class CacheableInterceptor method invoke.

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Method method = invocation.getMethod();
    MethodKey methodKey = new MethodKey(method, Cacheable.class);
    CacheConfiguration cacheable = expressionOperations.getConfig(methodKey);
    CacheEvaluationContext context = expressionOperations.prepareContext(methodKey, invocation);
    if (expressionOperations.passCondition(cacheable.condition(), context)) {
        // pass the condition
        Cache cache = obtainCache(method, cacheable);
        Object key = expressionOperations.createKey(cacheable.key(), context, invocation);
        if (cacheable.sync()) {
            // for sync
            try {
                return cache.get(key, invocation::proceed);
            } catch (CacheValueRetrievalException e) {
                throw e.getCause();
            }
        } else {
            Object value = get(cache, key);
            if (value == null) {
                value = invocation.proceed();
                if (expressionOperations.allowPutCache(cacheable.unless(), value, context)) {
                    put(cache, key, value);
                }
            }
            return value;
        }
    }
    return invocation.proceed();
}
Also used : CacheValueRetrievalException(cn.taketoday.cache.CacheValueRetrievalException) Method(java.lang.reflect.Method) CacheConfiguration(cn.taketoday.cache.annotation.CacheConfiguration) Cache(cn.taketoday.cache.Cache)

Example 2 with CacheConfiguration

use of cn.taketoday.cache.annotation.CacheConfiguration in project today-framework by TAKETODAY.

the class CacheEvictInterceptor method invoke.

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Method method = invocation.getMethod();
    MethodKey methodKey = new MethodKey(method, CacheEvict.class);
    CacheConfiguration cacheEvict = expressionOperations.getConfig(methodKey);
    // before
    if (cacheEvict.beforeInvocation()) {
        if (cacheEvict.allEntries()) {
            clear(obtainCache(method, cacheEvict));
        } else {
            Object key = expressionOperations.createKey(cacheEvict.key(), expressionOperations.prepareContext(methodKey, invocation), invocation);
            evict(obtainCache(method, cacheEvict), key);
        }
        return invocation.proceed();
    }
    // after
    // if any exception occurred in this operation will not do evict or clear
    Object proceed = invocation.proceed();
    if (cacheEvict.allEntries()) {
        clear(obtainCache(method, cacheEvict));
    } else {
        Object key = expressionOperations.createKey(cacheEvict.key(), expressionOperations.prepareContext(methodKey, invocation), invocation);
        evict(obtainCache(method, cacheEvict), key);
    }
    return proceed;
}
Also used : Method(java.lang.reflect.Method) CacheConfiguration(cn.taketoday.cache.annotation.CacheConfiguration)

Example 3 with CacheConfiguration

use of cn.taketoday.cache.annotation.CacheConfiguration in project today-framework by TAKETODAY.

the class CachePutInterceptor method invoke.

/**
 * Put cache operation
 *
 * @param invocation the method invocation join-point
 */
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    // process
    Object result = invocation.proceed();
    // put cache
    Method method = invocation.getMethod();
    MethodKey methodKey = new MethodKey(method, CachePut.class);
    CacheConfiguration cachePut = expressionOperations.getConfig(methodKey);
    CacheEvaluationContext context = expressionOperations.prepareContext(methodKey, invocation);
    // use ${result.xxx}
    context.setVariable(Constant.KEY_RESULT, result);
    if (expressionOperations.passCondition(cachePut.condition(), context)) {
        Object key = expressionOperations.createKey(cachePut.key(), context, invocation);
        put(obtainCache(method, cachePut), key, result);
    }
    return result;
}
Also used : Method(java.lang.reflect.Method) CacheConfiguration(cn.taketoday.cache.annotation.CacheConfiguration)

Example 4 with CacheConfiguration

use of cn.taketoday.cache.annotation.CacheConfiguration in project today-framework by TAKETODAY.

the class CachePutInterceptorTests method testInContext.

@Test
void testInContext() throws Exception {
    CacheExpressionOperations operations = new CacheExpressionOperations();
    try (StandardApplicationContext context = new StandardApplicationContext()) {
        context.register(CachePutConfig.class);
        context.refresh();
        CachePutInterceptor interceptor = context.getBean(CachePutInterceptor.class);
        Method save = CacheUserService.class.getDeclaredMethod("save", User.class);
        // CachePut
        MethodKey methodKey = new MethodKey(save, CachePut.class);
        interceptor.setExpressionOperations(operations);
        CacheConfiguration cachePut = operations.getConfig(methodKey);
        Cache users = interceptor.getCache("users", cachePut);
        User today = new User(1, "TODAY", 20, "666", "666", "男", new Date());
        CacheUserService userService = context.getBean(CacheUserService.class);
        userService.save(today);
        Object by_id_666 = users.get("by_id_666");
        assertThat(by_id_666).isEqualTo(today);
        User user = userService.getUser("666");
        assertThat(today).isEqualTo(user);
        user = userService.getUser("666");
        assertThat(today).isEqualTo(user);
        user = userService.getUser("666");
        assertThat(today).isEqualTo(user);
        user = userService.getUser("666");
        assertThat(today).isEqualTo(user);
        assertThat(today).isEqualTo(user).isEqualTo(by_id_666);
        // access time     no Cacheable Interceptor
        assertThat(userService.getAccessTime()).isEqualTo(4);
    }
}
Also used : StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) User(test.demo.config.User) Method(java.lang.reflect.Method) CacheConfiguration(cn.taketoday.cache.annotation.CacheConfiguration) Date(java.util.Date) Cache(cn.taketoday.cache.Cache) Test(org.junit.jupiter.api.Test)

Example 5 with CacheConfiguration

use of cn.taketoday.cache.annotation.CacheConfiguration 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

CacheConfiguration (cn.taketoday.cache.annotation.CacheConfiguration)7 Method (java.lang.reflect.Method)7 Cache (cn.taketoday.cache.Cache)5 Test (org.junit.jupiter.api.Test)4 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)3 Date (java.util.Date)3 User (test.demo.config.User)3 CaffeineCache (cn.taketoday.cache.support.CaffeineCache)2 CacheValueRetrievalException (cn.taketoday.cache.CacheValueRetrievalException)1 NoSuchCacheException (cn.taketoday.cache.NoSuchCacheException)1 CaffeineCacheManager (cn.taketoday.cache.support.CaffeineCacheManager)1