File tree Expand file tree Collapse file tree 3 files changed +66
-0
lines changed
voidframework-cache/src/main/java/dev/voidframework/cache Expand file tree Collapse file tree 3 files changed +66
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 22
33import com .google .inject .AbstractModule ;
44import com .google .inject .matcher .Matchers ;
5+ import dev .voidframework .cache .annotation .CachePut ;
56import dev .voidframework .cache .annotation .CacheRemove ;
67import dev .voidframework .cache .annotation .CacheResult ;
78import 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 }
You can’t perform that action at this time.
0 commit comments