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