1 39 40 package org.jahia.services.cache.simplecache; 41 42 import java.util.Hashtable ; 43 44 import org.jahia.exceptions.JahiaInitializationException; 45 import org.jahia.exceptions.JahiaPersistenceException; 46 import org.jahia.services.cache.CacheEntry; 47 import org.jahia.services.cache.JMSHub; 48 import org.jahia.services.cache.PersistenceCacheable; 49 50 67 public class PersistenceCache extends SimpleCache { 68 69 72 final private static Integer NULL_OBJECT = new Integer (0); 73 74 75 final private static org.apache.log4j.Logger logger = 76 org.apache.log4j.Logger.getLogger (PersistenceCache.class); 77 78 79 private PersistenceCacheable persistenceCacheable; 80 81 82 final private Hashtable blackList = new Hashtable (); 83 84 85 97 public PersistenceCache (String name, JMSHub hub, 98 PersistenceCacheable persistenceCacheable) 99 throws JahiaInitializationException 100 { 101 super (name, hub); 102 if (persistenceCacheable == null) { 103 throw new JahiaInitializationException ( 104 "In order to use the PersistenceCacheAdapter, you must pass an object that implements the PersistenceCacheable interface"); 105 } 106 this.persistenceCacheable = persistenceCacheable; 107 } 108 109 125 public Object get (Object entryKey) { 126 127 if (entryKey == null) { 128 logger.debug ("Cannot fetch with an null entry key!!!"); 129 return null; 130 } 131 132 Object obj = super.get (entryKey); 134 if (obj != null) { 135 blackList.remove (entryKey); 136 return obj; 137 } 138 139 if (blackList.containsKey (entryKey)) 142 return null; 143 144 try { 146 Object loadedObject = persistenceCacheable.getFromPersistence (entryKey); 147 if (loadedObject != null) { 148 super.put (entryKey, loadedObject); 150 return loadedObject; 151 } 152 153 } catch (JahiaPersistenceException ex) { 154 logger.warn ("Could not get the object ["+ entryKey + 155 "] from the persistence layer!", ex); 156 return null; 157 } 158 159 blackList.put (entryKey, NULL_OBJECT); 163 logger.debug ("could not find the associated CacheEntry instance"); 164 return null; 165 } 166 167 180 public void put (Object entryKey, Object entryObj) { 181 if (entryKey == null) { 182 logger.debug ("Cannot add an object with a null key!!"); 183 return; 184 } 185 186 if (entryObj == null) { 187 logger.debug ("Cannot add a null object!!"); 188 return; 189 } 190 191 boolean newKey = !containsKey (entryKey); 193 try { 194 persistenceCacheable.setToPersistence (entryKey, entryObj, newKey); 195 196 } catch (JahiaPersistenceException ex) { 197 logger.warn ("Could not add the cached object ["+ entryKey + 198 "] into the persistence layer!", ex); 199 return; 200 201 } 202 203 super.put (entryKey, entryObj); 205 206 blackList.remove (entryKey); 208 } 209 210 211 219 public void invalidateKey (final Object entryKey) { 220 if (entryKey == null) { 221 return; 222 } 223 224 blackList.remove (entryKey); 225 } 226 227 } 228 | Popular Tags |