1 17 package org.alfresco.repo.cache; 18 19 import java.io.IOException ; 20 import java.io.Serializable ; 21 22 import net.sf.ehcache.Cache; 23 import net.sf.ehcache.CacheException; 24 import net.sf.ehcache.Element; 25 26 import org.alfresco.error.AlfrescoRuntimeException; 27 28 39 public class EhCacheAdapter<K extends Serializable , V extends Serializable > 40 implements SimpleCache<K, V> 41 { 42 private net.sf.ehcache.Cache cache; 43 44 public EhCacheAdapter() 45 { 46 } 47 48 51 public void setCache(Cache cache) 52 { 53 this.cache = cache; 54 } 55 56 public boolean contains(K key) 57 { 58 try 59 { 60 return (cache.get(key) != null); 61 } 62 catch (CacheException e) 63 { 64 throw new AlfrescoRuntimeException("contains failed", e); 65 } 66 } 67 68 @SuppressWarnings ("unchecked") 69 public V get(K key) 70 { 71 try 72 { 73 Element element = cache.get(key); 74 if (element != null) 75 { 76 return (V) element.getValue(); 77 } 78 else 79 { 80 return null; 81 } 82 } 83 catch (CacheException e) 84 { 85 throw new AlfrescoRuntimeException("Failed to get from EhCache: \n" + 86 " key: " + key); 87 } 88 } 89 90 public void put(K key, V value) 91 { 92 Element element = new Element(key, value); 93 cache.put(element); 94 } 95 96 public void remove(K key) 97 { 98 cache.remove(key); 99 } 100 101 public void clear() 102 { 103 try 104 { 105 cache.removeAll(); 106 } 107 catch (IOException e) 108 { 109 throw new AlfrescoRuntimeException("Failed to clear cache", e); 110 } 111 } 112 } 113 | Popular Tags |