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();
}
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;
}
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;
}
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);
}
}
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) {
}
}
Aggregations