1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.cache.ehcache;
20
21 import net.sf.ehcache.Element;
22 import org.apache.shiro.cache.Cache;
23 import org.apache.shiro.cache.CacheException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import java.util.*;
28
29
30
31
32
33
34 public class EhCache<K, V> implements Cache<K, V> {
35
36
37
38
39 private static final Logger log = LoggerFactory.getLogger(EhCache.class);
40
41
42
43
44 private net.sf.ehcache.Ehcache cache;
45
46
47
48
49
50
51 public EhCache(net.sf.ehcache.Ehcache cache) {
52 if (cache == null) {
53 throw new IllegalArgumentException("Cache argument cannot be null.");
54 }
55 this.cache = cache;
56 }
57
58
59
60
61
62
63
64 public V get(K key) throws CacheException {
65 try {
66 if (log.isTraceEnabled()) {
67 log.trace("Getting object from cache [" + cache.getName() + "] for key [" + key + "]");
68 }
69 if (key == null) {
70 return null;
71 } else {
72 Element element = cache.get(key);
73 if (element == null) {
74 if (log.isTraceEnabled()) {
75 log.trace("Element for [" + key + "] is null.");
76 }
77 return null;
78 } else {
79
80 return (V) element.getObjectValue();
81 }
82 }
83 } catch (Throwable t) {
84 throw new CacheException(t);
85 }
86 }
87
88
89
90
91
92
93
94 public V put(K key, V value) throws CacheException {
95 if (log.isTraceEnabled()) {
96 log.trace("Putting object in cache [" + cache.getName() + "] for key [" + key + "]");
97 }
98 try {
99 V previous = get(key);
100 Element element = new Element(key, value);
101 cache.put(element);
102 return previous;
103 } catch (Throwable t) {
104 throw new CacheException(t);
105 }
106 }
107
108
109
110
111
112
113
114
115 public V remove(K key) throws CacheException {
116 if (log.isTraceEnabled()) {
117 log.trace("Removing object from cache [" + cache.getName() + "] for key [" + key + "]");
118 }
119 try {
120 V previous = get(key);
121 cache.remove(key);
122 return previous;
123 } catch (Throwable t) {
124 throw new CacheException(t);
125 }
126 }
127
128
129
130
131 public void clear() throws CacheException {
132 if (log.isTraceEnabled()) {
133 log.trace("Clearing all objects from cache [" + cache.getName() + "]");
134 }
135 try {
136 cache.removeAll();
137 } catch (Throwable t) {
138 throw new CacheException(t);
139 }
140 }
141
142 public int size() {
143 try {
144 return cache.getSize();
145 } catch (Throwable t) {
146 throw new CacheException(t);
147 }
148 }
149
150 public Set<K> keys() {
151 try {
152 @SuppressWarnings({"unchecked"})
153 List<K> keys = cache.getKeys();
154 if (!isEmpty(keys)) {
155 return Collections.unmodifiableSet(new LinkedHashSet<K>(keys));
156 } else {
157 return Collections.emptySet();
158 }
159 } catch (Throwable t) {
160 throw new CacheException(t);
161 }
162 }
163
164 public Collection<V> values() {
165 try {
166 @SuppressWarnings({"unchecked"})
167 List<K> keys = cache.getKeys();
168 if (!isEmpty(keys)) {
169 List<V> values = new ArrayList<V>(keys.size());
170 for (K key : keys) {
171 V value = get(key);
172 if (value != null) {
173 values.add(value);
174 }
175 }
176 return Collections.unmodifiableList(values);
177 } else {
178 return Collections.emptyList();
179 }
180 } catch (Throwable t) {
181 throw new CacheException(t);
182 }
183 }
184
185
186
187
188
189
190
191
192 public long getMemoryUsage() {
193 try {
194 return cache.calculateInMemorySize();
195 }
196 catch (Throwable t) {
197 return -1;
198 }
199 }
200
201
202
203
204
205
206
207
208 public long getMemoryStoreSize() {
209 try {
210 return cache.getMemoryStoreSize();
211 }
212 catch (Throwable t) {
213 throw new CacheException(t);
214 }
215 }
216
217
218
219
220
221
222
223
224 public long getDiskStoreSize() {
225 try {
226 return cache.getDiskStoreSize();
227 } catch (Throwable t) {
228 throw new CacheException(t);
229 }
230 }
231
232
233
234
235
236
237 public String toString() {
238 return "EhCache [" + cache.getName() + "]";
239 }
240
241
242
243
244
245
246 private static boolean isEmpty(Collection c) {
247 return c == null || c.isEmpty();
248 }
249 }