发表于: 2017-08-07 23:43:07
1 972
今天完成的:
通过方案评审,确定了方案设计。
收获:
1.通过aop+map模拟实现一个本地缓存
spring-aop
<aop:aspectj-autoproxy/>
<bean id="cacheAdvice" class="com.RedisAopTest.util.CacheAspect"/>
<aop:config>
<!--aspect里通过ref引用通知(advice),所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类-->
<aop:aspect ref="cacheAdvice">
<!--在新增接口前加入缓存-->
<aop:pointcut expression="execution(* com.RedisAopTest.dao.StudentDAO.showAllStudent(..))"
id="cacheList"/>
<aop:around method="doCacheList" pointcut-ref="cacheList"/>
<!--<aop:pointcut expression="execution(* main.com.store.service.StoreService.findStoreById(..))"-->
<!--id="cacheSingle"/>-->
<!--<aop:around method="doCacheSingle" pointcut-ref="cacheSingle"/>-->
<!--<aop:pointcut expression="execution(* main.com.store.service.StoreService.add(..))" id="clearAdd" />-->
<!--<aop:pointcut expression="execution(* main.com.store.service.StoreService.delete(..))" id="clearDelete" />-->
<!--<aop:pointcut expression="execution(* main.com.store.service.StoreService.update(..))" id="clearUpdate" />-->
<!--<aop:before method="clearCache" pointcut-ref="clearAdd"/>-->
<!--<aop:before method="clearCache" pointcut-ref="clearDelete"/>-->
<!--<aop:before method="clearCache" pointcut-ref="clearUpdate"/>-->
</aop:aspect>
</aop:config>
aspect
private static final Logger logger=Logger.getLogger(CacheAspect.class);
// 为了线程安全,使用Collections.synchronizedMap(new HashMap());
private static Map<String, Object> aopCahche = Collections.synchronizedMap(new HashMap<String, Object>());
// private static Map<String, Object> aopCahche = new HashMap<String, Object>();
public static Map<String, Object> getAopCahche() {
return aopCahche;
}
public static void setAopCahche(Map<String, Object> aopCahche) {
CacheAspect.aopCahche = aopCahche;
}
public static List<Student> doCacheList(ProceedingJoinPoint point) throws Throwable {
logger.debug("=========================================>>正在执行aop切面方法");
//proceed()方法通过反射执行目标对象的连接点处的方法
List<Student> result = (List<Student>) point.proceed();
logger.debug("=========================================>>执行了aop切面方法");
return result;
}
public static Student doCacheSingle(ProceedingJoinPoint point) throws Throwable {
Student result = (Student) point.proceed();
return result;
}
public static void clearCache() {
aopCahche = new HashMap<String, Object>();
System.out.println("清空缓存成功!");
}
service
// List<Student> Students = new ArrayList<Student>();
// // 获取map缓存中的值
// Map<String, Object> cacheMap = CacheAspect.getAopCahche();
// System.out.println("==================================map是否为空>>"+cacheMap.isEmpty());
// Set<Entry<String, Object>> entrySet = cacheMap.entrySet();
// if (!CacheAspect.getAopCahche().isEmpty()) {
// System.out.println("======================>>查询缓存");
// for (Map.Entry<String, Object> entry : entrySet) {
// String key = entry.getKey().toString();
// System.out.println(key);
// Student Student = (Student) entry.getValue();
// Students.add(Student);
// }
// } else {
// System.out.println("==========================>>查询数据库");
// List<Student> list = studentDAOImpl.showAllStudent("%");
// for (Student Student : list) {
// cacheMap.put(String.valueOf(Student.getId()), Student);
// }
// }
- 这种缓存代码和业务代码耦合度太高,如上面的例子,service层加了太多缓存的逻辑,不便于维护和变更
- 不灵活。
- aop还是挺好用的,他是一个工具,可以在不改变原有代码结构上增加新的功能,或者对一些零碎的的不便于直接放进代码里处理的逻辑进行处理。常用的比如事务,切面curd方法。对回调数据的处理,比如用户注册成功后返回信息,信息需要上传到别的数据仓库处理,这时用aop最好。
- 用map做缓存只能用在本地,而且受jdk内存限制,一重启就没了,所以只能用在简单的开发中,最好还是用缓存框架。
遇到的问题:
1.卡在一个地方,运行的时候不知道为什么aop的方法切不到,始终用不上缓存,差了一些资料,我的mybatis没用spring整合,好像没在spring容器里的方法是不能通过aop的,暂时跑不起来
明天的计划:
把这个搞完就写复盘项目
评论