注釈ベースのメソッドキャッシュ
5482 ワード
Webプロジェクトでは、データベースのクエリーに時間がかかるため、同じメソッドの同じパラメータのリクエストは、毎回データベースを調べるのではなくキャッシュに保存する必要があります.メソッドの戻り値はMongodbまたはredisに保存できます.
具体的な実装:
@MethodCache注記
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MethodCache {
}MethodCacheAspect接面
この接面の核心思想は,メソッドの署名+実パラメトリックオブジェクトのハッシュ値をkeyとして,まずキャッシュから取り,取れない場合は具体的なメソッドを呼び出してクエリーし,クエリー結果をキャッシュに保存することであり,そのうち1回に1つのスレッドしか調べられないように設定したり,再試行回数を設定したり,空の結果を保存するかどうかを設定したりすることができる.
@Aspect @Order(value = 2) @Component public class MethodCacheAspect {
}
具体的な実装:
@MethodCache注記
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MethodCache {
/**
* , 60
*
* @return int
*/
int expireSeconds() default 60;
/**
* key, , key
*
* @return String
*/
String key() default "";
/**
* ,
*
* @return boolean
*/
boolean limitQuery() default true;
/**
*
*
* @return int
*/
int limitQuerySeconds() default 5;
/**
*
*
* @return boolean
*/
boolean saveEmptyResult() default true;
}MethodCacheAspect接面
この接面の核心思想は,メソッドの署名+実パラメトリックオブジェクトのハッシュ値をkeyとして,まずキャッシュから取り,取れない場合は具体的なメソッドを呼び出してクエリーし,クエリー結果をキャッシュに保存することであり,そのうち1回に1つのスレッドしか調べられないように設定したり,再試行回数を設定したり,空の結果を保存するかどうかを設定したりすることができる.
@Aspect @Order(value = 2) @Component public class MethodCacheAspect {
@Resource
private JedisPool jedisPool;
@Resource
private DisLockUtil disLockUtil;
private static final Logger LOGGER = LoggerFactory.getLogger(MethodCacheAspect.class);
private static final String EMPTY_RESULT = "NULL";
/**
*
*
* @param proceedingJoinPoint
* @param methodCache
* @return Object
* @throws Throwable
*/
@Around("@annotation(methodCache)")
public Object execute(ProceedingJoinPoint proceedingJoinPoint, MethodCache methodCache) throws Throwable {
MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = methodSignature.getMethod();
//
int size = proceedingJoinPoint.getArgs().length;
//
List
}