SearchCacheAspect.java 4.89 KB
package com.yoho.search.aop.cache;

import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.MD5Util;
import com.yoho.search.cache.beans.SearchCacheFactory;
import com.yoho.search.cache.beans.SearchCacheService;
import com.yoho.search.cache.model.SearchCache;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.SearchApiResult;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Component
@Aspect
@Order(Integer.MAX_VALUE - 1)
public class SearchCacheAspect {

    @Autowired
    private SearchCacheService searchCacheService;
    @Autowired
    private SearchCacheFactory searchCacheFactory;


    /**
     * @目前参数只支持Map和HttpRequest
     * @param pjp
     * @param <T>
     * @return
     * @throws Throwable
     */
    @Around("@annotation(com.yoho.search.aop.cache.SearchCacheAble)")
    public <T> T doCache(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature signature = (MethodSignature) pjp.getSignature();

        // 1、检查注解
        SearchCacheAble searchCacheAble = signature.getMethod().getAnnotation(SearchCacheAble.class);
        if (searchCacheAble == null) {
            return (T) pjp.proceed();
        }

        // 2、获取Type的返回类型
        Type type = signature.getMethod().getGenericReturnType();

        // 3、检查cacheKey
        RedisKeyBuilder redisKeyBuilder = this.getRedisKeyBuilder(pjp, searchCacheAble);
        if (StringUtils.isBlank(redisKeyBuilder.getKey())) {
            return (T) pjp.proceed();
        }

        // 4、缓存获取
        SearchCache searchCache = searchCacheFactory.getAspectSearhCache(searchCacheAble);
        T result = searchCacheService.getSerializableObjectFromCache(searchCache, redisKeyBuilder, type);
        if (result != null) {
            return result;
        }

        //5、执行
        result = (T) pjp.proceed();
        if (result == null) {
            return result;
        }
        if (result instanceof SearchApiResult && ((SearchApiResult) result).getCode() != 200) {
            return result;
        }

        //6、加入缓存
        searchCacheService.addSerializableObjectToCache(searchCache, redisKeyBuilder, result);
        return result;
    }

    // 获取请求的参数
    private RedisKeyBuilder getRedisKeyBuilder(ProceedingJoinPoint pjp, SearchCacheAble searchCacheAble) {
        Object[] arges = pjp.getArgs();
        Object object = arges[0];
        if (object instanceof HttpServletRequest) {
            return this.getRedisKeyBuilder((HttpServletRequest) object, searchCacheAble);
        }
        if (object instanceof Map) {
            return this.getRedisKeyBuilder((Map<?, ?>) object, searchCacheAble);
        }
        return null;
    }

    // 获取httpServletRequest的缓存key
    private RedisKeyBuilder getRedisKeyBuilder(HttpServletRequest httpServletRequest, SearchCacheAble searchCacheAble) {
        Map<String, String> paramMap = HttpServletRequestUtils.transParamType(httpServletRequest);
        return this.getRedisKeyBuilder(paramMap, searchCacheAble);
    }

    // 获取请求的参数
    private RedisKeyBuilder getRedisKeyBuilder(Map<?, ?> paramMap, SearchCacheAble searchCacheAble) {
        List<String> includeParams = Arrays.asList(searchCacheAble.includeParams());
        List<String> excludeParams = Arrays.asList(searchCacheAble.excludeParams());
        String paramKey = "";
        if (!includeParams.isEmpty()) {
            paramKey = HttpServletRequestUtils.genParamStringWithIncludeParams(paramMap, includeParams);
        } else if (!excludeParams.isEmpty()) {
            paramKey = HttpServletRequestUtils.genParamStringWithExcludeParams(paramMap, excludeParams);
        } else {
            paramKey = HttpServletRequestUtils.genParamString(paramMap);
        }
        return this.getRedisKeyBuilderByParamKey(paramKey, searchCacheAble);
    }

    private RedisKeyBuilder getRedisKeyBuilderByParamKey(String paramKey, SearchCacheAble searchCacheAble) {
        RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance();
        redisKeyBuilder.appendFixed("YOHOSEARCH:AOP:NEW:");
        redisKeyBuilder.appendFixed(searchCacheAble.cacheName()).appendFixed(":");
        redisKeyBuilder.appendVar(MD5Util.string2MD5(paramKey));
        return redisKeyBuilder;
    }
}