- package com.woaika.loan.front.common.aop;
- import Java.lang.reflect.Method;
- import java.util.Date;
- import java.util.List;
- import javax.annotation.Resource;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.Signature;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Pointcut;
- import org.aspectj.lang.reflect.MethodSignature;
- import org.springframework.stereotype.Component;
- import com.woaika.loan.commons.annotation.Cache;
- import com.woaika.loan.commons.annotation.Flush;
- import com.woaika.loan.commons.cache.Memcache;
- import com.woaika.loan.po.CacheLog;
- import com.woaika.loan.service.log.ICacheLogService;
- /**
- * 拦截缓存
- * @author ajun
- * @email zhaojun2066@gmail.com
- * @blog http://blog.csdn.net/ajun_studio
- * 2012-3-12 上午10:51:58
- */
- @Component
- @Aspect
- public class CacheAop {
- private ICacheLogService cacheLogService;
- @Resource(name="cacheLogService")
- public void setCacheLogService(ICacheLogService cacheLogService) {
- this.cacheLogService = cacheLogService;
- }
- //定义切面
- @Pointcut("execution(* com.woaika.loan.service..*.*(..))")
- public void cachedPointcut() {
- }
- @Around("cachedPointcut()")
- public Object doAround(ProceedingJoinPoint call){
- Object result = null;
- Method[] methods = call.getTarget().getClass().getDeclaredMethods();
- Signature signature = call.getSignature();
- MethodSignature methodSignature = (MethodSignature) signature;
- Method method = methodSignature.getMethod();
- for(Method m:methods){//循环方法,找匹配的方法进行执行
- if(m.getName().equals(method.getName())){
- if(m.isAnnotationPresent(Cache.class)){
- Cache cache = m.getAnnotation(Cache.class);
- if(cache!=null){
- String tempKey = this.getKey(method, call.getArgs());
- String prefix = cache.prefix();
- String key = prefix+"_"+tempKey;
- result =Memcache.get(key);
- if(null == result){
- try {
- result = call.proceed();
- long expiration = cache.expiration();//1000*60*60*2==2小时过期
- Date d=new Date();
- d=new Date(d.getTime()+expiration);
- Memcache.set(key, result, d);
- //将key存入数据库
- CacheLog log = new CacheLog();
- log.setPrefix(prefix);
- log.setCacheKey(key);
- this.cacheLogService.add(log);
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
- }
- } else if(method.isAnnotationPresent(Flush.class)){
- Flush flush = method.getAnnotation(Flush.class);
- if(flush!=null){
- String prefix = flush.prefix();
- List<CacheLog> logs= cacheLogService.findListByPrefix(prefix);
- if(logs!=null && !logs.isEmpty()){
- //删除数据库
- int rows = cacheLogService.deleteByPrefix(prefix);
- if(rows>0){
- for(CacheLog log :logs){
- if(log!=null){
- String key = log.getCacheKey();
- Memcache.delete(key);//删除缓存
- }
- }
- }
- }
- }
- }else{
- try {
- result = call.proceed();
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
- break;
- }
- }
- return result;
- }
- /**
- * 组装key值
- * @param method
- * @param args
- * @return
- */
- private String getKey(Method method, Object [] args){
- StringBuffer sb = new StringBuffer();
- String methodName = method.getName();
- sb.append(methodName);
- if(args!=null && args.length>0){
- for(Object arg:args){
- sb.append(arg);
- }
- }
- return sb.toString();
- }
- }
- @Cache(prefix=CacheConstant.ANLI,expiration=1000*60*60*10)
- @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly=true)
- public QueryResult findAll(Integer firstIndex, Integer pageSize) {
- Map condition = new HashMap();
- return loanCaseDao.findByCondition(condition, LoanCase.class, firstIndex, pageSize);
- }
