001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.components.cache.interceptor;
017
018
019import com.jfinal.aop.Interceptor;
020import com.jfinal.aop.Invocation;
021import com.jfinal.log.Log;
022import io.jboot.components.cache.annotation.CacheEvict;
023import io.jboot.components.cache.annotation.CachesEvict;
024
025import java.lang.reflect.Method;
026import java.util.ArrayList;
027import java.util.List;
028
029/**
030 * 清除缓存操作的拦截器
031 */
032public class CachesEvictInterceptor implements Interceptor {
033    private static final Log LOG = Log.getLog(CachesEvictInterceptor.class);
034
035
036    @Override
037    public void intercept(Invocation inv) {
038
039        Method method = inv.getMethod();
040
041        CachesEvict cachesEvict = method.getAnnotation(CachesEvict.class);
042        if (cachesEvict == null) {
043            inv.invoke();
044            return;
045        }
046
047        CacheEvict[] evicts = cachesEvict.value();
048        List<CacheEvict> beforeInvocations = null;
049        List<CacheEvict> afterInvocations = null;
050
051        for (CacheEvict evict : evicts) {
052            if (evict.beforeInvocation()) {
053                if (beforeInvocations == null) {
054                    beforeInvocations = new ArrayList<>();
055                }
056                beforeInvocations.add(evict);
057            } else {
058                if (afterInvocations == null) {
059                    afterInvocations = new ArrayList<>();
060                }
061                afterInvocations.add(evict);
062            }
063        }
064
065        Class<?> targetClass = inv.getTarget().getClass();
066        try {
067            doCachesEvict(inv.getArgs(), targetClass, method, beforeInvocations, inv.isActionInvocation());
068            inv.invoke();
069        } finally {
070            doCachesEvict(inv.getArgs(), targetClass, method, afterInvocations, inv.isActionInvocation());
071        }
072    }
073
074
075    private void doCachesEvict(Object[] arguments
076            , Class<?> targetClass
077            , Method method
078            , List<CacheEvict> cacheEvicts
079            , boolean isAction) {
080
081        if (cacheEvicts == null || cacheEvicts.isEmpty()) {
082            return;
083        }
084
085        for (CacheEvict evict : cacheEvicts) {
086            try {
087                Utils.removeCache(arguments, targetClass, method, evict, isAction);
088            } catch (Exception ex) {
089                LOG.error(ex.toString(), ex);
090            }
091        }
092    }
093}