View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
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   * Simplified api for enabling Service and DAO classes with EhCache.  
26   * @author garystruthers
27   *
28   */
29  public class EhCacheHelper {
30  	
31  	private CacheManager cacheManager; // singleton uses default ehcache.xml configuration in ehcache.jar
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  	 * EhCache has a default configuration file within ehcache.jar which is used if there is no ehcache.xml in the classpath.
51  	 * It is a singleton, so it's only created once.
52  	 * @see http://ehcache.sourceforge.net/EhcacheUserGuide.html#id.s6.2   for default configuration 
53  	 * @see http://ehcache.sourceforge.net/EhcacheUserGuide.html#usingthecachemanager
54  	 * To customize configuration, add an ehcache.xml file to the classpath.
55  	 * 
56  	 */
57  	private void initCacheManager() {
58  
59  		if(cacheManager == null) {
60  			cacheManager = CacheManager.create(); 			
61  		}
62  	}
63  	
64  	/**
65  	 * Creates a cache and adds it to the cache manager
66  	 * @param cacheName you could name the cache with the classname that uses it.
67  	 * 
68  	 */
69  	public void createCache(String cacheName) {
70  		if(!cacheManager.cacheExists(cacheName)) {
71  			cacheManager.addCache(cacheName);
72  		}
73  	}
74  	
75  	/**
76  	 * Like Hibernate's saveOrUpdate, this is used to create a new cache element and update existing element
77  	 * The cache knows if cache.put if creating or updating an element.
78  	 * @param cacheName
79  	 * @param key	must be unique, should be descriptive
80  	 * @param value the thing cached
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  	 * Get the thing cached
90  	 * @param cacheName
91  	 * @param key
92  	 * @return a Serializable object Note, EhCache can support non-serializable object in memory only cache
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 	 * Gets Element, useful for calling Element methods like getHitCount()
105 	 * @param cacheName
106 	 * @param key
107 	 * @return
108 	 */
109 	public Element getCacheElement(String cacheName, String key) {
110 		Cache cache = cacheManager.getCache(cacheName);
111 		return cache.get(key);		
112 	}
113 
114 	/**
115 	 * Removes 1 element from cache
116 	 * @param cacheName
117 	 * @param key
118 	 */
119 	public void evictCacheElement(String cacheName, String key) {
120 		Cache cache = cacheManager.getCache(cacheName);
121 		cache.remove(key);
122 	}
123 	
124 	/**
125 	 * Remove all elements from cache
126 	 * @param cacheName
127 	 */
128 	public void evictAllCacheElements(String cacheName) {
129 		Cache cache = cacheManager.getCache(cacheName);
130 		cache.removeAll();
131 	}
132 	
133 	/**
134 	 * Gets how many times element has been accessed. Useful for testing and optimization
135 	 * @param element
136 	 * @return
137 	 */
138 	public long getCacheElementHitCount(Element element) {
139 		if(element == null) {
140 			return 0;
141 		}
142 		return element.getHitCount();
143 	}
144 	
145 	/**
146 	 * @return
147 	 */
148 	public String[] getCacheNames() {
149 		return cacheManager.getCacheNames();
150 	}
151 }