@SuppressWarnings("unchecked")
 private static Object doGenerate(
     KeyGenerator keyGenerator, CacheKeyInvocationContext<?> context) {
   List<Object> parameters = new ArrayList<>();
   for (CacheInvocationParameter param : context.getKeyParameters()) {
     Object value = param.getValue();
     if (param.getParameterPosition() == context.getAllParameters().length - 1
         && context.getMethod().isVarArgs()) {
       parameters.addAll((List<Object>) CollectionUtils.arrayToList(value));
     } else {
       parameters.add(value);
     }
   }
   return keyGenerator.generate(
       context.getTarget(),
       context.getMethod(),
       parameters.toArray(new Object[parameters.size()]));
 }
  @AroundInvoke
  public Object cacheResult(InvocationContext invocationContext) throws Exception {
    if (log.isTraceEnabled()) {
      log.tracef("Interception of method named '%s'", invocationContext.getMethod().getName());
    }

    final CacheKeyInvocationContext<CacheResult> cacheKeyInvocationContext =
        contextFactory.getCacheKeyInvocationContext(invocationContext);
    final CacheKeyGenerator cacheKeyGenerator =
        cacheKeyInvocationContext
            .unwrap(CacheKeyInvocationContextImpl.class)
            .getCacheKeyGenerator();
    final CacheResult cacheResult = cacheKeyInvocationContext.getCacheAnnotation();
    final CacheKey cacheKey = cacheKeyGenerator.generateCacheKey(cacheKeyInvocationContext);
    final Cache<CacheKey, Object> cache = cacheResolver.resolveCache(cacheKeyInvocationContext);

    Object result = null;

    if (!cacheResult.skipGet()) {
      result = cache.get(cacheKey);
      if (log.isTraceEnabled()) {
        log.tracef(
            "Entry with value '%s' has been found in cache '%s' with key '%s'",
            result, cache.getName(), cacheKey);
      }
    }

    if (result == null) {
      result = invocationContext.proceed();
      if (result != null) {
        cache.put(cacheKey, result);
        if (log.isTraceEnabled()) {
          log.tracef(
              "Value '%s' cached in cache '%s' with key '%s'", result, cache.getName(), cacheKey);
        }
      }
    }

    return result;
  }
예제 #3
0
 @Override
 public GeneratedCacheKey generateCacheKey(CacheKeyInvocationContext<? extends Annotation> ckic) {
   return new PayaraGeneratedCacheKey(ckic.getKeyParameters());
 }