利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)

主题:
这份代码是开发中常见的代码,查询数据库某个主表的数据,为了提高性能,做一次缓存,每次调用时先拿缓存数据,有则直接返回,没有才向数据库查数据,降低数据库压力。

public Merchant loadCachedMerchant(String merchantId) {
    String key = this.createCacheKey(merchantId);
    Merchant merchant = (Merchant) this.memCachedClient.get(key);// 先看缓存
    if (merchant == null) {
        merchant = this.merchantDao.searchMerchantByMerchantId1(merchantId);//候查数据库
        if (merchant != null) {
            merchant = extraCacheMerchant(merchant);
            this.memCachedClient.put(key, merchant, 5 * 60);
        }
    }

    return merchant;
}

然而一般我门在项目初始或在发展的过程中,并不能很好的规划出哪些业务时需要缓存的,而我们发现这个先取缓存再数据库的逻辑是共通的,那么我们就想到了面向切面开发来解决这个问题也是很合适的。

构想下,可以想到我们可以利用注解,对那些需要有这段缓存逻辑的方法单独提供一个注解,这样我们就能找到这些切面,也就是有这个特定注解的方法,比如叫@ServiceCache

这个@ServiceCache注解需要的参数有缓存key的组成方式,以及有效时间。

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ServiceCache {
    int value() default -1;

    int expire() default 60;

    ServiceCache.Key key() default ServiceCache.Key.JSON;

    String[] includeKeys() default {};

    boolean sync() default false;

    boolean nullPattern() default true;

    public static enum Key {
        JSON,
        TO_STRING;

        private Key() {
        }
    }
}

这里又加了一个sync参数用来控制同步执行,这个同步的参数是用来解决什么问题的呢?我们都明白这里使用缓存的方式是为了解决对数据库的频繁调用的问题,这些并发的调用可能导致数据库的压力,那么在我们的逻辑中:先检查缓存,在没有的情况下会去数据库找,而有一种极端的情况是,当大量请求并发时,到这个判断逻辑检查缓存同时发现没有,也就是数据库的数据尚未放入缓存,此时这些并发都会去数据库找,那么数据库依然有风险出现并发调用。

@Aspect
public class ServiceCacheAnnotationAspect {
    private static Logger log = LoggerFactory.getLogger(ServiceCacheAnnotationAspect.class);
    private ICache serviceCache;
    private Object syncLock = new Object();

    public ServiceCacheAnnotationAspect() {
    }
/**
 * 切面匹配为class使用了@Service 或 @Repository 并且方法使用了自定义的@ServiceCache
 **/
    @Around("(@within(org.springframework.stereotype.Service)||
    @within(org.springframework.stereotype.Repository))&&
    @annotation(com.xiaoka.freework.cache.annotation.ServiceCache)") private Object cacheProcess(ProceedingJoinPoint jp) throws Throwable { Class targetClz = jp.getTarget().getClass(); String methodName = jp.getSignature().getName(); if(!(jp.getSignature() instanceof MethodSignature)) { log.warn("该方法接口无法启用缓存功能: {}", jp.getSignature().toLongString()); return jp.proceed(); } else { MethodSignature methodSign = (MethodSignature)jp.getSignature(); ServiceCache sc = ServiceCacheUtils.single().findServiceCache(targetClz, methodSign.getMethod()); if(sc == null) { return jp.proceed(); } else { int expire = sc.value() >= 0?sc.value():sc.expire();//获取定义的过期时间 if(expire > 0) { String cacheKey = ServiceCacheUtils.single().buildCacheKey(sc, targetClz, methodName, jp.getArgs()); Object rval = null; if(sc.sync()) { Object var9 = this.syncLock; synchronized(this.syncLock) {// 这里做了同步 rval = this.cacheInvoke(sc, jp, cacheKey, expire);//这里实现我们核心逻辑 } } else { rval = this.cacheInvoke(sc, jp, cacheKey, expire); } return rval instanceof ServiceCacheAnnotationAspect.Blank?null:rval; } else { return jp.proceed(); } } } } private Object cacheInvoke(ServiceCache sc, ProceedingJoinPoint jp, String cacheKey, int expire) throws Throwable { log.debug("Load from cache for key : {}", cacheKey); Object rval = this.serviceCache.get(cacheKey); if(rval == null) {//缓存中没有,就要去数据库拿,拿完就放缓存 log.info("Miss from cache, load backend for key : {}", cacheKey); rval = jp.proceed();//执行目标方法 rval = rval == null && sc.nullPattern()?ServiceCacheAnnotationAspect.Blank.INST:rval; if(rval != null) { this.serviceCache.put(cacheKey, rval, expire); } } return rval; } public void setServiceCache(ICache serviceCache) { this.serviceCache = serviceCache; ServiceCacheUtils.single().setCache(serviceCache); } private static class Blank implements Serializable { private static final long serialVersionUID = 3203712628835590212L; private static final ServiceCacheAnnotationAspect.Blank INST = new ServiceCacheAnnotationAspect.Blank(); private Blank() { } } }


实际使用中的例子是这样的:

@ServiceCache(expire = 600, includeKeys = { "name" })
    public CarProvinceEntity selectProvinceByName(String name) {
        return commonDao.mapper(CarProvinceEntity.class).source(Source.SLAVE)
                        .sql("selectByName").session().selectOne(name);
    }

如此,对于开发业务的人员来说就比较方便了。通过aop结合注解,可以在项目中做一些切面的事情已经成为很多项目底层框架的一部分,比如计算方法耗时,打日志,国际化等等的业务。

相关知识点链接:

http://www.cnblogs.com/killbug/p/5271291.html

http://samter.iteye.com/blog/410618
http://itindex.net/detail/29812-aop
http://www.ibm.com/developerworks/cn/java/j-lo-aopi18n/index.html

 

 

 

 

--------------------20161020 补充线-----------------------

发现,实际项目编码时经常用到aop的思想,利用spring实现非常方便,所以再补充一个简易的例子:

因为业务方法调用可能抛出异常,但是有些方法在有异常情况下也不应该阻断流程,所以要try catch住,所以就写一个注解统一将这个需求做掉。

注解:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface NoThrowException {


}

注解解析,利用spring切面匹配注解,代理实际业务方法,catch住异常。

@Aspect
public class NoThrowExceptionAspect {

    @Around("@annotation(aspect.test.annotation.NoThrowException)")
    public void tryAround(ProceedingJoinPoint joinPoint){
        try{
            joinPoint.proceed();
        } catch (Throwable throwable) {
//            throwable.printStackTrace();
        }
    }
}

实际使用:

 @NoThrowException
    public void dost() throws Exception {

        System.out.printf("do st");
        throw new Exception("TEST");
    }

 

以上非常简单明了,spring的很多功能后续要深入使用。

 

遇到瓶颈,要静下心来,加油!

 

posted on 2016-03-21 13:07  每当变幻时  阅读(2248)  评论(0编辑  收藏  举报

导航