1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.test.spring;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import net.sf.ehcache.CacheManager;
22 import net.sf.ehcache.Element;
23 import net.sf.ehcache.ObjectExistsException;
24
25 import org.aopalliance.aop.Advice;
26 import org.apache.log4j.Logger;
27 import org.aspectj.lang.ProceedingJoinPoint;
28
29 @Deprecated
30 public class IdToObjectEhcacheAdvice implements Advice {
31 final Logger LOG = Logger.getLogger(IdToObjectEhcacheAdvice.class);
32
33 private CacheManager cacheManager;
34 private String cacheName;
35
36
37
38
39 public IdToObjectEhcacheAdvice() {
40 super();
41 }
42
43
44
45
46 public IdToObjectEhcacheAdvice(String cacheName) {
47 super();
48 this.cacheName = cacheName;
49 }
50
51 public Object invalidateCache(ProceedingJoinPoint pjp) throws Throwable {
52 if (cacheManager == null) {
53 cacheManager = CacheManager.getInstance();
54 try {
55 cacheManager.addCache(cacheName);
56 } catch (ObjectExistsException e) {
57
58 }
59 }
60 LOG.info("Invalidating Cache");
61 cacheManager.getCache(cacheName).remove(pjp.getArgs()[0]);
62 return pjp.proceed();
63 }
64
65 @SuppressWarnings("unchecked")
66 public Object getFromCache(ProceedingJoinPoint pjp) throws Throwable {
67 if (cacheManager == null) {
68 cacheManager = CacheManager.getInstance();
69 try {
70 cacheManager.addCache(cacheName);
71 } catch (ObjectExistsException e) {
72
73 }
74 }
75
76
77 if (pjp.getArgs().length == 1 && pjp.getArgs()[0] instanceof List) {
78 List<Object> results = new ArrayList<Object>();
79 List<String> uncachedIdList = new ArrayList<String>();
80 for (String id : (List<String>) pjp.getArgs()[0]) {
81
82 LOG.info("Looking in Cache");
83 Element cachedResult = cacheManager.getCache(cacheName).get(id);
84 if (cachedResult == null) {
85 uncachedIdList.add(id);
86 } else {
87 results.add(cachedResult.getValue());
88 }
89 }
90 if (uncachedIdList.size() > 0) {
91 List<Idable> uncachedResults = (List<Idable>) pjp.proceed();
92 if (uncachedResults != null) {
93 for (Idable uncachedResult : uncachedResults) {
94
95 LOG.info("Storing to Cache");
96 results.add(uncachedResult);
97 cacheManager.getCache(cacheName).put(
98 new Element(uncachedResult.getId(),
99 uncachedResult));
100 }
101 }
102 }
103 return results;
104 }
105 if (pjp.getArgs().length == 1 && pjp.getArgs()[0] instanceof String) {
106 String id = (String) pjp.getArgs()[0];
107 LOG.info("Looking in Cache");
108 Element resultElement = cacheManager.getCache(cacheName).get(id);
109 Object result;
110 if (resultElement == null) {
111 result = pjp.proceed();
112 LOG.info("Storing to Cache");
113 cacheManager.getCache(cacheName).put(new Element(id, result));
114 } else {
115 result = resultElement.getValue();
116 }
117 return result;
118 }
119
120 return pjp.proceed();
121 }
122
123
124
125
126 public String getCacheName() {
127 return cacheName;
128 }
129
130
131
132
133
134 public void setCacheName(String cacheName) {
135 this.cacheName = cacheName;
136 }
137
138 }