使用 AOP 和注解实现方法缓存(上)

前言

做web开发中,提高系统效率,经常用到缓存操作。但是代码中到处都是Cache的操作,为了提高代码的质量,在大神的指导下使用面向切面编程,提供一个基于Method的统一的Cache切入功能。

启用@AspectJ支持

通过在你的Spring的配置中引入下列元素来启用Spring对@AspectJ的支持:

<aop:aspectj-autoproxy/>

自定义一个Cache注解

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface Cache {
    /**     * 缓存过期时间,单位是秒     */    
    int expire() default 10;
}

完成切面

使用注解来创建切面,around方法获取缓存值,如果没有命中到缓存值。应该调用目标函数,通过目标函数计算实际值。

@Aspect
@Component
public class CacheIntercepter{
    @Around("@annotation(com.spring.aop.annoation.Cache)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        if (joinPoint == null) {
            return  null;
        }
        String key = getKey(joinPoint);
        CacheClient client = new CacheClient();
        String clientValue = client.getValue(key);
        if (clientValue != null) {
            return clientValue;
        }
        //如果没有命中到缓存值。应该调用目标函数,通过目标函数计算实际值。
        return joinPoint.proceed();
    }

    /**
     * 根据参数生成cachekey
     * @param joinPoint
     * @return
     */
    private String getKey(ProceedingJoinPoint joinPoint) {
        String result = "";
        if (joinPoint.getArgs() == null) {
            return result;
        }
        for (Object key : joinPoint.getArgs()) {
            if (key != null) {
                result += "_" + key.toString();
            }
        }
        return result;
    }
}

缓存客户端的实现

为简化起见,假设key为空值的时候,缓存值为null;

public class CacheClient {
    public String getValue(String key){
        if (key == null || "".equals(key)) {
            System.out.println("未命中cache");
        } else {
            System.out.println("命中cache");
            return "success";
        }
        return  null;
    }
}

测试用咧


public class CacheTest extends AbstractTest{

    @Autowired
    private UserService userService;

    @Test
    public void test(){
        {
            String cache_result = userService.getId("12");
            System.out.println("命中cache的结果:"+cache_result);
        }

        {
            String uncache_result = userService.getId(null);
            System.out.println("未命中cache的结果:"+uncache_result);
        }
    }
}

运行结果

命中cache
命中cache的结果:success
未命中cache
未命中cache的结果:我的计算值

第一版的代码请移步github,注意是feature-aspectj-aop分支。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,281评论 19 139
  • 本章内容: 面向切面编程的基本原理 通过POJO创建切面 使用@AspectJ注解 为AspectJ切面注入依赖 ...
    谢随安阅读 3,240评论 0 9
  • 纵使时光飞逝,你亦是我忘不掉的美丽。 2017年8月1日 星期二 晴 今天无聊刷朋友圈,一幅图片吸引住我的目光,那...
    映月月影阅读 693评论 5 10
  • 文/大壮 图/大壮和她的朋友们 出行最重要的是:和谁,什么时候。 Summer Cassadee Pope -...
    田大壮是我阅读 581评论 1 3
  • 父亲节。去年姐姐就跟我说,应该把这张照片晒出来。结果,直到今年,还是选择了珍藏!
    槿琼阅读 138评论 0 0