private static Hashtable<String, ControlMemory> hashManager = new Hashtable<String, ControlMemory>(); private static Hashtable<String, String> hashSTBManager = new Hashtable<String, String>(); private static Hashtable<String, String> hashMappingManager = new Hashtable<String, String>();
Basic Caching Problem
- Bad Code Readablity - Hashtable to Ehcache Method problem. [Deffernce] Hashtable<K, V> get(K) put(K,V) getKeys() - return Enumeration - hasMoreElements(), nextElement() containsKey(String), containsValue(String) Ehcache<K, V> get(K) - return Object. So needgetObjectValue() or getObjectKey() put(new Elemnet(K,V)) getKeys() - return List - iterator().hasNext(), iterator().next() isKeyInCache(String), isValueInCache(String) - Proto type of EhcacheManager class : Occuered NullPointerException when used [getCache, get, put, etc .. ] in other class
import net.sf.ehcache.CacheManager; import net.sf.ehcache.Ehcache; import net.sf.ehcache.Element; public class EhcacheWrapper<K, V> implements CacheWrapper<K, V> { private final String cacheName; private final CacheManager cacheManager;
public EhcacheWrapper(final String cacheName, final CacheManager cacheManager) { this.cacheName = cacheName; this.cacheManager = cacheManager; }
public Ehcache getCache() { return cacheManager.getEhcache(cacheName); }
public void put(K key, V value) { // TODO Auto-generated method stub getCache().put(new Element(key, value)); }
public V get(K key) { // TODO Auto-generated method stub Element element = getCache().get(key); if(element != null) { return (V) element.getObjectValue(); } return null; }
public void remove(K Key) { // TODO Auto-generated method stub getCache().remove(Key); }
public List<String> keys() { // TODO Auto-generated method stub List<String> keys = getCache().getKeys(); return keys; }
public boolean containsKey(String strPairingID) { // TODO Auto-generated method stub return getCache().isKeyInCache(strPairingID); }
public boolean containsValue(String strCharID) { // TODO Auto-generated method stub return getCache().isValueInCache(strCharID); }
public int size() { // TODO Auto-generated method stub return getCache().getSize(); }
}
How to use in other class
CacheManager cacheManager = CacheManager.newInstance(); EhcacheWrapper<String, ControlMemory> hashManager = new EhcacheWrapper<String, ControlMemory>("hashManager", cacheManager); EhcacheWrapper<String, String> hashSTBManager = new EhcacheWrapper<String, String>("hashSTBManager", cacheManager); EhcacheWrapper<String, String> hashMappingManager = new EhcacheWrapper<String, String>("hashMappingManager", cacheManager); ......
Use Same to Hashtable. Likes hashManager.get() / put / containsKey, Value / keys / remove / etc....