星空网 > 软件开发 > Java

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

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

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

@Aspectpublic 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

 




原标题:利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

看样定货会:https://www.goluckyvip.com/tag/35109.html
康宁餐具 Corelle Brands :https://www.goluckyvip.com/tag/35110.html
康小璇:https://www.goluckyvip.com/tag/35111.html
康小璇 托娅 :https://www.goluckyvip.com/tag/35112.html
康众汽配:https://www.goluckyvip.com/tag/35113.html
考拉自营海外仓:https://www.goluckyvip.com/tag/35114.html
你更喜欢一个人旅行,还是一群人旅行? :https://www.vstour.cn/a/408249.html
延安市区景点都收费吗 延安景点要门票吗:https://www.vstour.cn/a/408250.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流