All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.fufu669.utils.CacheAopUtil Maven / Gradle / Ivy

package com.gitee.fufu669.utils;

import com.gitee.fufu669.common.CacheKeyCommon;
import com.gitee.fufu669.service.CacheService;
import org.aspectj.lang.JoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** @author wangfupeng */
public class CacheAopUtil {

	public static final Logger logger = LoggerFactory.getLogger(CacheAopUtil.class);

	public static String getCacheKey(JoinPoint joinPoint){
		Object target = joinPoint.getTarget();
		/* 当前的类的全名*/
		String targetName = target.getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		Object[] argumentsValues = joinPoint.getArgs();
		String cacheKeyBefore = "";
		cacheKeyBefore = getCacheKeyBeforeFromArgumentsValue(argumentsValues, cacheKeyBefore);
		cacheKeyBefore = targetName+":"+methodName+":"+cacheKeyBefore;
		return "cachekey:"+ CachePasswordUtil.sha256(cacheKeyBefore);
	}

	public static String getCacheKeyBeforeFromArgumentsValue(Object[] argumentsValues, String cacheKeyBefore) {
		for (int i = 0; i < argumentsValues.length; i++) {
			Object argumentsValue = argumentsValues[i];
			try {
				cacheKeyBefore += CacheJsonUtil.toJson(argumentsValue);
			} catch (Exception e) {
				logger.info("计算key时有对象无法序列化,如果有key生成则可以忽略。");
				//e.printStackTrace();
			}
		}
		return cacheKeyBefore;
	}

	public static boolean isRefreshFlag(int refreshSeconds, String cacheKey, CacheService cacheService) {
		boolean refreshFlag;
		if (refreshSeconds <= 0) {
			refreshFlag = true;
		} else {
			/*判断如果刷新时间还没到,就禁止刷新。*/
			if (cacheService.lock(CacheKeyCommon.LOCK +"refresh:" + cacheKey, refreshSeconds)) {
				refreshFlag = true;
			} else {
				refreshFlag = false;
			}
		}
		return refreshFlag;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy