没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
试读
14页
缘起需求:需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系统中Service或者DAO层的get/find等方法返回结果,如果数据更新(使用Create/update/delete方法),则刷新cache中相应的内容。
资源推荐
资源详情
资源评论
需要使用 Spring 来实现一个 Cache 简单的解决方案,具体需求如下:使用任意一个现有开
源 Cache Framework,要求可以 Cache 系统中 Service 或者 DAO 层的 get/find 等方法返
回结果,如果数据更新(使用 Create/update/delete 方法),则刷新 cache 中相应的内容。
根据需求,计划使用 Spring AOP + ehCache 来实现这个功能,采用 ehCache 原因之一是
Spring 提供了 ehCache 的支持,至于为何仅仅支持 ehCache 而不支持 osCache 和
JBossCache 无从得知(Hibernate???),但毕竟 Spring 提供了支持,可以减少一部分工作
量:)。二是后来实现了 OSCache 和 JBoss Cache 的方式后,经过简单测试发现几个 Cache
在效率上没有太大的区别(不考虑集群),决定采用 ehCahce。
AOP 嘛,少不了拦截器,先创建一个实现了 MethodInterceptor 接口的拦截器,用来拦截
Service/DAO 的方法调用,拦截到方法后,搜索该方法的结果在 cache 中是否存在,如果
存在,返回 cache 中的缓存结果,如果不存在,返回查询数据库的结果,并将结果缓存到
cache 中。
MethodCacheInterceptor.java
Java 代码
1. package com.co.cache.ehcache;
2.
3. import java.io.Serializable;
4.
5. import net.sf.ehcache.Cache;
6. import net.sf.ehcache.Element;
7.
8. import org.aopalliance.intercept.MethodInterceptor;
9. import org.aopalliance.intercept.MethodInvocation;
10. import org.apache.commons.logging.Log;
11. import org.apache.commons.logging.LogFactory;
12. import org.springframework.beans.factory.InitializingBean;
13. import org.springframework.util.Assert;
14.
15. public class MethodCacheInterceptor implements MethodInterceptor, Initiali
zingBean
16. {
17. private static final Log logger = LogFactory.getLog(MethodCacheInterce
ptor.class);
18.
19. private Cache cache;
20.
21. public void setCache(Cache cache) {
22. this.cache = cache;
23. }
24.
25. public MethodCacheInterceptor() {
26. super();
27. }
28.
29. /**
30. * 拦截 Service/DAO 的方法,并查找该结果是否存在,如果存在就返回 cache 中的值,
31. * 否则,返回数据库查询结果,并将查询结果放入 cache
32. */
33. public Object invoke(MethodInvocation invocation) throws Throwable {
34. String targetName = invocation.getThis().getClass().getName();
35. String methodName = invocation.getMethod().getName();
36. Object[] arguments = invocation.getArguments();
37. Object result;
38.
39. logger.debug("Find object from cache is " + cache.getName());
40.
41. String cacheKey = getCacheKey(targetName, methodName, arguments);
42. Element element = cache.get(cacheKey);
43.
44. if (element == null) {
45. logger.debug("Hold up method , Get method result and create ca
che........!");
46. result = invocation.proceed();
47. element = new Element(cacheKey, (Serializable) result);
48. cache.put(element);
49. }
50. return element.getValue();
51. }
52.
53. /**
54. * 获得 cache key 的方法,cache key 是 Cache 中一个 Element 的唯一标识
55. * cache key 包括 包名+类名+方法名,如
com.co.cache.service.UserServiceImpl.getAllUser
56. */
57. private String getCacheKey(String targetName, String methodName, Objec
t[] arguments) {
58. StringBuffer sb = new StringBuffer();
59. sb.append(targetName).append(".").append(methodName);
60. if ((arguments != null) && (arguments.length != 0)) {
61. for (int i = 0; i < arguments.length; i++) {
62. sb.append(".").append(arguments[i]);
63. }
64. }
65. return sb.toString();
66. }
剩余13页未读,继续阅读
资源评论
小小哭包
- 粉丝: 1899
- 资源: 3854
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功