Search in sources :

Example 1 with CacheInvokeConfig

use of com.alicp.jetcache.anno.method.CacheInvokeConfig in project jetcache by alibaba.

the class CachePointcut method matchesImpl.

private boolean matchesImpl(Method method, Class targetClass) {
    if (!matchesThis(method.getDeclaringClass())) {
        return false;
    }
    if (exclude(targetClass.getName())) {
        return false;
    }
    String key = getKey(method, targetClass);
    CacheInvokeConfig cac = cacheConfigMap.getByMethodInfo(key);
    if (cac == CacheInvokeConfig.getNoCacheInvokeConfigInstance()) {
        return false;
    } else if (cac != null) {
        return true;
    } else {
        cac = new CacheInvokeConfig();
        CacheConfigUtil.parse(cac, method);
        String name = method.getName();
        Class<?>[] paramTypes = method.getParameterTypes();
        parseByTargetClass(cac, targetClass, name, paramTypes);
        if (!cac.isEnableCacheContext() && cac.getCachedAnnoConfig() == null && cac.getInvalidateAnnoConfigs() == null && cac.getUpdateAnnoConfig() == null) {
            cacheConfigMap.putByMethodInfo(key, CacheInvokeConfig.getNoCacheInvokeConfigInstance());
            return false;
        } else {
            cacheConfigMap.putByMethodInfo(key, cac);
            return true;
        }
    }
}
Also used : CacheInvokeConfig(com.alicp.jetcache.anno.method.CacheInvokeConfig)

Example 2 with CacheInvokeConfig

use of com.alicp.jetcache.anno.method.CacheInvokeConfig in project jetcache by alibaba.

the class JetCacheInterceptor method invoke.

@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
    if (configProvider == null) {
        configProvider = applicationContext.getBean(ConfigProvider.class);
    }
    if (configProvider != null && globalCacheConfig == null) {
        globalCacheConfig = configProvider.getGlobalCacheConfig();
    }
    if (globalCacheConfig == null || !globalCacheConfig.isEnableMethodCache()) {
        return invocation.proceed();
    }
    Method method = invocation.getMethod();
    Object obj = invocation.getThis();
    CacheInvokeConfig cac = null;
    if (obj != null) {
        String key = CachePointcut.getKey(method, obj.getClass());
        cac = cacheConfigMap.getByMethodInfo(key);
    }
    if (cac == null || cac == CacheInvokeConfig.getNoCacheInvokeConfigInstance()) {
        return invocation.proceed();
    }
    CacheInvokeContext context = configProvider.getCacheContext().createCacheInvokeContext(cacheConfigMap);
    context.setTargetObject(invocation.getThis());
    context.setInvoker(invocation::proceed);
    context.setMethod(method);
    context.setArgs(invocation.getArguments());
    context.setCacheInvokeConfig(cac);
    context.setHiddenPackages(globalCacheConfig.getHiddenPackages());
    return CacheHandler.invoke(context);
}
Also used : ConfigProvider(com.alicp.jetcache.anno.support.ConfigProvider) CacheInvokeConfig(com.alicp.jetcache.anno.method.CacheInvokeConfig) CacheInvokeContext(com.alicp.jetcache.anno.method.CacheInvokeContext) Method(java.lang.reflect.Method)

Example 3 with CacheInvokeConfig

use of com.alicp.jetcache.anno.method.CacheInvokeConfig in project jetcache by alibaba.

the class CacheContext method createCacheInvokeContext.

public CacheInvokeContext createCacheInvokeContext(ConfigMap configMap) {
    CacheInvokeContext c = newCacheInvokeContext();
    c.setCacheFunction((invokeContext, cacheAnnoConfig) -> {
        Cache cache = cacheAnnoConfig.getCache();
        if (cache == null) {
            if (cacheAnnoConfig instanceof CachedAnnoConfig) {
                cache = createCacheByCachedConfig((CachedAnnoConfig) cacheAnnoConfig, invokeContext);
            } else if ((cacheAnnoConfig instanceof CacheInvalidateAnnoConfig) || (cacheAnnoConfig instanceof CacheUpdateAnnoConfig)) {
                CacheInvokeConfig cacheDefineConfig = configMap.getByCacheName(cacheAnnoConfig.getArea(), cacheAnnoConfig.getName());
                if (cacheDefineConfig == null) {
                    String message = "can't find @Cached definition with area=" + cacheAnnoConfig.getArea() + " name=" + cacheAnnoConfig.getName() + ", specified in " + cacheAnnoConfig.getDefineMethod();
                    CacheConfigException e = new CacheConfigException(message);
                    logger.error("Cache operation aborted because can't find @Cached definition", e);
                    return null;
                }
                cache = createCacheByCachedConfig(cacheDefineConfig.getCachedAnnoConfig(), invokeContext);
            }
            cacheAnnoConfig.setCache(cache);
        }
        return cache;
    });
    return c;
}
Also used : CacheConfigException(com.alicp.jetcache.CacheConfigException) CacheInvokeConfig(com.alicp.jetcache.anno.method.CacheInvokeConfig) CacheInvokeContext(com.alicp.jetcache.anno.method.CacheInvokeContext) Cache(com.alicp.jetcache.Cache) EnableCache(com.alicp.jetcache.anno.EnableCache)

Aggregations

CacheInvokeConfig (com.alicp.jetcache.anno.method.CacheInvokeConfig)3 CacheInvokeContext (com.alicp.jetcache.anno.method.CacheInvokeContext)2 Cache (com.alicp.jetcache.Cache)1 CacheConfigException (com.alicp.jetcache.CacheConfigException)1 EnableCache (com.alicp.jetcache.anno.EnableCache)1 ConfigProvider (com.alicp.jetcache.anno.support.ConfigProvider)1 Method (java.lang.reflect.Method)1