pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
application.yml
spring:
aop:
proxy-target-class: true
代码
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface Cacheable {
String key();
String value() default "";
int expireTime() default 3600;
}
package acw.com.sys.config;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
@Aspect
@Component
@EnableAspectJAutoProxy
public class RedisConfig {
@Autowired
private RedisTemplate redisTemplate;
public static final String GET_CACHE_GET = "get";
public static final String GET_CACHE_query = "query";
public static final String GET_CACHE_select = "select";
public static final String GET_CACHE_list = "list";
public static final String SET_CACHE_ADD = "add";
public static final String SET_CACHE_INSERT = "insert";
public static final String SET_CACHE_SAVE = "save";
public static final String SET_CACHE_BATCH_INSERT = "batchInsert";
public static final String SET_CACHE_BATCH_UPDATE = "batchUpdate";
public static final String SET_CACHE_UPDATE = "update";
public static final String SET_CACHE_DELETE = "delete";
private static final Pattern GET_CACHE_PATTERN = Pattern.compile("^((get)|(query)|(select)|(list)).*$");
private static final Pattern SET_CACHE_PATTERN = Pattern.compile("^((add)|(insert)|(save)|(batchInsert)|(batchUpdate)|(update)|(delete)).*$");
@Pointcut("@annotation(acw.com.sys.annotation.Cacheable)")
public void pointCut(){
}
@Around("pointCut()")
public Object doCache(ProceedingJoinPoint point)throws Throwable{
Object result = null;
Object [] args = point.getArgs();
String arg = "";
if (args != null && args.length>0){
arg = String.valueOf(args[0]);
}
String className = point.getTarget().toString().split("@")[0];
String methodName = point.getSignature().getName();
Cacheable cacheable
= ((MethodSignature)point.getSignature()).getMethod().getAnnotation(Cacheable.class);
String redisKey = className + "." +cacheable.value()+ methodName +":" + arg;
if(GET_CACHE_PATTERN.matcher(cacheable.key()).matches()){
result = redisTemplate.opsForValue().get(redisKey);
if (result != null){
return result;
}
result = point.proceed();
if (result == null){
result = "不存在该数据";
}
redisTemplate.opsForValue().set(redisKey,result,cacheable.expireTime(), TimeUnit.SECONDS);
}else if (SET_CACHE_PATTERN.matcher(cacheable.key()).matches()){
result = point.proceed();
Set<String> keys = redisTemplate.keys(className+ "." +cacheable.value()+"*");
redisTemplate.delete(keys);
}
return result;
}
}