由于项目的原因,需要对项目中大量访问多修改少的数据进行缓存并管理,为达到开发过程中通过Annotation简单的配置既可以完成对缓存的设置与更新的需求,故而设计的该简易的解决方案。
涉及技术:
1、Spring AOP
2、Java Annotation
3、Memcache (项目中使用的缓存组件)
4、JVM基础 (Class文件结构,用于解析出方法中的形参名称,动态生成缓存key,目测效率不高0.0)
5、Ognl (用于动态解析缓存的key)
实现细节:
Annotation:LoadFromMemcached 用与method之上的注解,作用是使带有该注解的method在调用的时候先经过缓存查询,缓存中查询不到再去数据库查询并将结果缓存至缓存服务器Memcache中,
1 import java.lang.annotation.ElementType; 2 import java.lang.annotation.Retention; 3 import java.lang.annotation.RetentionPolicy; 4 import java.lang.annotation.Target; 5 6 @Retention(RetentionPolicy.RUNTIME) 7 @Target(ElementType.METHOD) 8 public @interface LoadFromMemcached { 9 10 String value();//缓存的key 11 12 int timeScope() default 600;//默认过期时间,单位秒 13 14 String condition() default "";//执行缓存查询的条件 15 16 }