Ejemplo n.º 1
0
  public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
    try {
      Map<Class, CacheOp> cacheOpMap = parseCacheAnnotations(m);

      if (cacheOpMap.size() > 0) {
        boolean cacheablePresent = false;
        Object result = null;
        CacheOp cacheOp = cacheOpMap.get(Cacheable.class);
        if (cacheOp != null) {
          List<Cacheable> cacheables = cacheOp.cacheOps;
          if (!CollectionUtils.isEmpty(cacheables)) {
            cacheablePresent = true;
            result = handleCacheable(m, args, cacheables.get(0));
          }
        }

        if (!cacheablePresent) {
          result = m.invoke(obj, args);
        }

        cacheOp = cacheOpMap.get(CacheEvict.class);
        if (cacheOp != null) {
          List<CacheEvict> cacheEvicts = cacheOp.cacheOps;
          if (!CollectionUtils.isEmpty(cacheEvicts)) {
            for (CacheEvict cacheEvict : cacheEvicts) {
              handleCacheEvict(args, cacheEvict);
            }
          }
        }

        cacheOp = cacheOpMap.get(CacheUpdate.class);
        if (cacheOp != null) {
          List<CacheUpdate> cacheUpdates = cacheOp.cacheOps;
          if (!CollectionUtils.isEmpty(cacheUpdates)) {
            for (CacheUpdate cacheUpdate : cacheUpdates) {
              handleCacheUpdate(args, cacheUpdate);
            }
          }
        }

        return result;
      }
      return m.invoke(obj, args);
    } catch (InvocationTargetException e) {
      throw e.getTargetException();
    } catch (Exception e) {
      throw new RuntimeException("unexpected invocation exception", e);
    }
  }
Ejemplo n.º 2
0
  private Map<Class, CacheOp> parseCacheAnnotations(AnnotatedElement ae) {
    Map<Class, CacheOp> cacheOpMap = new HashMap<>();

    Collection<Cacheable> cacheables = getAnnotations(ae, Cacheable.class);
    if (cacheables != null) {
      CacheOp<Cacheable> cacheOp = lazyGetCacheOp(cacheOpMap, Cacheable.class);
      cacheOp.addAll(cacheables);
    }

    Collection<CacheEvict> cacheEvicts = getAnnotations(ae, CacheEvict.class);
    if (cacheEvicts != null) {
      CacheOp<CacheEvict> cacheOp = lazyGetCacheOp(cacheOpMap, CacheEvict.class);
      cacheOp.addAll(cacheEvicts);
    }

    Collection<CacheUpdate> cacheUpdates = getAnnotations(ae, CacheUpdate.class);
    if (cacheUpdates != null) {
      CacheOp<CacheUpdate> cacheOp = lazyGetCacheOp(cacheOpMap, CacheUpdate.class);
      cacheOp.addAll(cacheUpdates);
    }

    Collection<Caching> cachings = getAnnotations(ae, Caching.class);
    if (cachings != null) {
      for (Caching caching : cachings) {
        Cacheable[] _cacheables = caching.cacheable();
        if (!CollectionUtils.isEmpty(_cacheables)) {
          CacheOp<Cacheable> cacheOp = lazyGetCacheOp(cacheOpMap, Cacheable.class);
          cacheOp.addAll(Arrays.asList(_cacheables));
        }

        CacheEvict[] _cacheEvicts = caching.cacheEvict();
        if (!CollectionUtils.isEmpty(_cacheEvicts)) {
          CacheOp<CacheEvict> cacheOp = lazyGetCacheOp(cacheOpMap, CacheEvict.class);
          cacheOp.addAll(Arrays.asList(_cacheEvicts));
        }

        CacheUpdate[] _cacheUpdates = caching.cacheUpdate();
        if (!CollectionUtils.isEmpty(_cacheUpdates)) {
          CacheOp<CacheUpdate> cacheOp = lazyGetCacheOp(cacheOpMap, CacheUpdate.class);
          cacheOp.addAll(Arrays.asList(_cacheUpdates));
        }
      }
    }

    return cacheOpMap;
  }