1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.util;
17
18 import java.io.Serializable;
19
20 import net.sf.ehcache.Cache;
21 import net.sf.ehcache.CacheManager;
22 import net.sf.ehcache.Element;
23
24
25
26
27
28
29 public class EhCacheHelper {
30
31 private CacheManager cacheManager;
32
33
34
35 public EhCacheHelper() {
36 initCacheManager();
37 }
38
39 public EhCacheHelper(String configurationFileName) {
40 initCacheManager(configurationFileName);
41 }
42
43 private void initCacheManager(String configurationFileName) {
44 if(cacheManager == null) {
45 cacheManager = CacheManager.create(configurationFileName);
46 }
47 }
48
49
50
51
52
53
54
55
56
57 private void initCacheManager() {
58
59 if(cacheManager == null) {
60 cacheManager = CacheManager.create();
61 }
62 }
63
64
65
66
67
68
69 public void createCache(String cacheName) {
70 if(!cacheManager.cacheExists(cacheName)) {
71 cacheManager.addCache(cacheName);
72 }
73 }
74
75
76
77
78
79
80
81
82 public void saveOrUpdateCacheElement(String cacheName, String key, Object value) {
83 Cache cache = cacheManager.getCache(cacheName);
84 Element element = new Element(key, value);
85 cache.put(element);
86 }
87
88
89
90
91
92
93
94 public Serializable getCacheElementValue(String cacheName, String key) {
95 Element element = getCacheElement(cacheName, key);
96 if(element == null) {
97 return null;
98 }
99 return element.getValue();
100 }
101
102
103
104
105
106
107
108
109 public Element getCacheElement(String cacheName, String key) {
110 Cache cache = cacheManager.getCache(cacheName);
111 return cache.get(key);
112 }
113
114
115
116
117
118
119 public void evictCacheElement(String cacheName, String key) {
120 Cache cache = cacheManager.getCache(cacheName);
121 cache.remove(key);
122 }
123
124
125
126
127
128 public void evictAllCacheElements(String cacheName) {
129 Cache cache = cacheManager.getCache(cacheName);
130 cache.removeAll();
131 }
132
133
134
135
136
137
138 public long getCacheElementHitCount(Element element) {
139 if(element == null) {
140 return 0;
141 }
142 return element.getHitCount();
143 }
144
145
146
147
148 public String[] getCacheNames() {
149 return cacheManager.getCacheNames();
150 }
151 }