-
Notifications
You must be signed in to change notification settings - Fork 0
/
CacheResultInterceptorCustom.java
42 lines (32 loc) · 1.23 KB
/
CacheResultInterceptorCustom.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package jug.saxony.camp.infinispan.embedded.jar.interceptor;
import java.util.Objects;
import javax.cache.annotation.CacheResult;
import javax.enterprise.context.Dependent;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import javax.naming.InitialContext;
import org.infinispan.manager.EmbeddedCacheManager;
@Interceptor
@Dependent
@CacheResult
public class CacheResultInterceptorCustom {
private EmbeddedCacheManager cacheContainer;
private org.infinispan.Cache<Integer, Object> cache;
@AroundInvoke
public Object managedTransaction(InvocationContext ctx) throws Exception {
CacheResult annotation = ctx.getMethod().getAnnotation(CacheResult.class);
String cacheName = annotation.cacheName();
cacheContainer = (EmbeddedCacheManager) new InitialContext()
.lookup("java:jboss/infinispan/container/" + cacheName);
cache = cacheContainer.getCache();
System.out.println(cache.getName() + ": " + cache.getCacheConfiguration().eviction());
int cacheKey = Objects.hash(ctx.getParameters());
Object result = cache.get(cacheKey);
if (result == null || annotation.skipGet()) {
result = ctx.proceed();
cache.put(cacheKey, result);
}
return result;
}
}