Skip to content

Commit 117743d

Browse files
Add CachePut annotation (#102)
Signed-off-by: Thibault Meyer <meyer.thibault@gmail.com>
1 parent db4ee0a commit 117743d

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package dev.voidframework.cache.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Annotation indicating that the result of the method will
10+
* be cached regardless cache value already exists.
11+
*
12+
* @since 1.7.0
13+
*/
14+
@Target(ElementType.METHOD)
15+
@Retention(RetentionPolicy.RUNTIME)
16+
public @interface CachePut {
17+
18+
/**
19+
* Key of the cache in which result is stored.
20+
*
21+
* @return Key of the cache in which result is stored
22+
* @since 1.7.0
23+
*/
24+
String key() default "{class}.{method}";
25+
26+
/**
27+
* Retention time (in seconds).
28+
*
29+
* @return Retention time (in seconds)
30+
* @since 1.7.0
31+
*/
32+
int timeToLive() default -1;
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dev.voidframework.cache.module;
2+
3+
import dev.voidframework.cache.annotation.CachePut;
4+
import org.aopalliance.intercept.MethodInvocation;
5+
6+
/**
7+
* Intercepts method call when annotation {@link CachePut} is used.
8+
*
9+
* @since 1.7.0
10+
*/
11+
public final class CacheInterceptorPut extends CacheInterceptor {
12+
13+
@Override
14+
public Object invoke(final MethodInvocation methodInvocation) throws Throwable {
15+
16+
if (cacheEngine == null) {
17+
return methodInvocation.proceed();
18+
}
19+
20+
final CachePut cachePut = methodInvocation.getMethod().getAnnotation(CachePut.class);
21+
final String cacheKey = resolveCacheKey(methodInvocation, cachePut.key());
22+
23+
final Object value = methodInvocation.proceed();
24+
this.cacheEngine.set(cacheKey, value, cachePut.timeToLive());
25+
26+
return value;
27+
}
28+
}

voidframework-cache/src/main/java/dev/voidframework/cache/module/CacheModule.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.inject.AbstractModule;
44
import com.google.inject.matcher.Matchers;
5+
import dev.voidframework.cache.annotation.CachePut;
56
import dev.voidframework.cache.annotation.CacheRemove;
67
import dev.voidframework.cache.annotation.CacheResult;
78
import dev.voidframework.cache.engine.CacheEngine;
@@ -16,12 +17,16 @@ public final class CacheModule extends AbstractModule {
1617
@Override
1718
protected void configure() {
1819

20+
final CacheInterceptor cacheInterceptorPut = new CacheInterceptorPut();
1921
final CacheInterceptor cacheInterceptorRemove = new CacheInterceptorRemove();
2022
final CacheInterceptor cacheInterceptorResult = new CacheInterceptorResult();
2123

24+
requestInjection(cacheInterceptorPut);
2225
requestInjection(cacheInterceptorRemove);
2326
requestInjection(cacheInterceptorResult);
27+
2428
bind(CacheEngine.class).toProvider(CacheEngineProvider.class);
29+
bindInterceptor(Matchers.any(), Matchers.annotatedWith(CachePut.class), cacheInterceptorPut);
2530
bindInterceptor(Matchers.any(), Matchers.annotatedWith(CacheRemove.class), cacheInterceptorRemove);
2631
bindInterceptor(Matchers.any(), Matchers.annotatedWith(CacheResult.class), cacheInterceptorResult);
2732
}

0 commit comments

Comments
 (0)