1 package org.apache.torque.manager;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.Serializable;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.jcs.access.GroupCacheAccess;
29 import org.apache.jcs.access.exception.CacheException;
30 import org.apache.torque.TorqueException;
31
32
33
34
35
36
37
38
39 public class MethodResultCache
40 {
41
42 private GroupCacheAccess jcsCache;
43
44
45 private Map<String, Object> groups;
46
47
48 private static Log log = LogFactory.getLog(MethodResultCache.class);
49
50
51
52
53
54
55 public MethodResultCache(GroupCacheAccess cache)
56 {
57 this.jcsCache = cache;
58 groups = new HashMap<String, Object>();
59 }
60
61
62
63
64
65
66 protected MethodResultCache()
67 {
68
69 }
70
71
72
73
74 public void clear()
75 {
76 if (jcsCache != null)
77 {
78 try
79 {
80 jcsCache.clear();
81 groups.clear();
82 }
83 catch (CacheException ce)
84 {
85 log.error(new TorqueException(
86 "Could not clear cache due to internal JCS error.", ce));
87 }
88 }
89 }
90
91 protected Object getImpl(MethodCacheKey key)
92 {
93 Object result = null;
94 if (jcsCache != null)
95 {
96 synchronized (this)
97 {
98 result = jcsCache.getFromGroup(key, key.getGroupKey());
99 }
100 }
101
102 if (result != null && log.isDebugEnabled())
103 {
104 log.debug("MethodResultCache saved expensive operation: " + key);
105 }
106
107 return result;
108 }
109
110
111 protected Object putImpl(MethodCacheKey key, Object value)
112 throws TorqueException
113 {
114
115 String group = key.getGroupKey();
116 if (!groups.containsKey(group))
117 {
118 groups.put(group, null);
119 }
120
121 Object old = null;
122 if (jcsCache != null)
123 {
124 try
125 {
126 synchronized (this)
127 {
128 old = jcsCache.getFromGroup(key, group);
129 jcsCache.putInGroup(key, group, value);
130 }
131 }
132 catch (CacheException ce)
133 {
134 throw new TorqueException
135 ("Could not cache due to internal JCS error", ce);
136 }
137 }
138 return old;
139 }
140
141 protected Object removeImpl(MethodCacheKey key)
142 {
143 Object old = null;
144 if (jcsCache != null)
145 {
146 synchronized (this)
147 {
148 old = jcsCache.getFromGroup(key, key.getGroupKey());
149 jcsCache.remove(key, key.getGroupKey());
150 }
151 }
152 return old;
153 }
154
155
156
157
158
159
160
161
162
163
164
165 @SuppressWarnings("unchecked")
166 public <T> T get(Serializable instanceOrClass, String method, Serializable ... arg)
167 {
168 T result = null;
169 if (jcsCache != null)
170 {
171 try
172 {
173 MethodCacheKey key = new MethodCacheKey(instanceOrClass, method, arg);
174 result = (T) getImpl(key);
175 }
176 catch (Exception e)
177 {
178 log.error("Problem getting object from cache", e);
179 }
180 }
181
182 return result;
183 }
184
185
186
187
188
189
190
191
192
193
194 public <T> void put(T value, Serializable instanceOrClass, String method, Serializable ... arg)
195 {
196 try
197 {
198 MethodCacheKey key = new MethodCacheKey(instanceOrClass, method, arg);
199 putImpl(key, value);
200 }
201 catch (Exception e)
202 {
203 log.error("Problem putting object into cache", e);
204 }
205 }
206
207
208
209
210
211
212
213
214 public void removeAll(Serializable instanceOrClass, String method)
215 {
216 if (jcsCache != null)
217 {
218 try
219 {
220 MethodCacheKey key = new MethodCacheKey(instanceOrClass, method);
221 String groupName = key.getGroupKey();
222 jcsCache.invalidateGroup(groupName);
223 groups.remove(groupName);
224 }
225 catch (Exception e)
226 {
227 log.error("Problem removing all", e);
228 }
229 }
230 }
231
232
233
234
235
236
237
238
239
240
241
242 @SuppressWarnings("unchecked")
243 public <T> T remove(Serializable instanceOrClass, String method, Serializable ... arg)
244 {
245 T result = null;
246 if (jcsCache != null)
247 {
248 try
249 {
250 MethodCacheKey key = new MethodCacheKey(instanceOrClass, method, arg);
251 result = (T) removeImpl(key);
252 }
253 catch (Exception e)
254 {
255 log.error("Problem removing object from cache", e);
256 }
257 }
258
259 return result;
260 }
261 }