1 17 package org.alfresco.repo.cache; 18 19 import java.io.Serializable ; 20 21 import org.alfresco.error.AlfrescoRuntimeException; 22 import org.jboss.cache.Fqn; 23 import org.jboss.cache.TreeCache; 24 25 30 public class TreeCacheAdapter<K extends Serializable , V extends Serializable > 31 implements SimpleCache<K, V> 32 { 33 private TreeCache cache; 34 private Fqn regionFqn; 35 36 public TreeCacheAdapter() 37 { 38 } 39 40 43 public void setCache(TreeCache cache) 44 { 45 this.cache = cache; 46 } 47 48 53 public void setRegionName(String regionName) 54 { 55 this.regionFqn = new Fqn(regionName); 56 } 57 58 public boolean contains(K key) 59 { 60 try 61 { 62 return cache.exists(regionFqn, key); 63 } 64 catch (Throwable e) 65 { 66 throw new AlfrescoRuntimeException("contains failed", e); 67 } 68 } 69 70 @SuppressWarnings ("unchecked") 71 public V get(K key) 72 { 73 try 74 { 75 Object element = cache.get(regionFqn, key); 76 if (element != null) 77 { 78 return (V) element; 79 } 80 else 81 { 82 return null; 83 } 84 } 85 catch (Throwable e) 86 { 87 throw new AlfrescoRuntimeException("Failed to get from TreeCache: \n" + 88 " key: " + key, 89 e); 90 } 91 } 92 93 public void put(K key, V value) 94 { 95 try 96 { 97 cache.put(regionFqn, key, value); 98 } 99 catch (Throwable e) 100 { 101 throw new AlfrescoRuntimeException("Failed to put into TreeCache: \n" + 102 " key: " + key + "\n" + 103 " value: " + value, 104 e); 105 } 106 } 107 108 public void remove(K key) 109 { 110 try 111 { 112 cache.remove(regionFqn, key); 113 } 114 catch (Throwable e) 115 { 116 throw new AlfrescoRuntimeException("Failed to remove from TreeCache: \n" + 117 " key: " + key, 118 e); 119 } 120 } 121 122 public void clear() 123 { 124 try 125 { 126 cache.remove(regionFqn); 127 } 128 catch (Throwable e) 129 { 130 throw new AlfrescoRuntimeException("Failed to clear cache", e); 131 } 132 } 133 } 134 | Popular Tags |