1 31 32 package org.opencms.cache; 33 34 import org.opencms.main.CmsEvent; 35 import org.opencms.main.CmsLog; 36 import org.opencms.main.I_CmsEventListener; 37 import org.opencms.main.OpenCms; 38 import org.opencms.xml.Messages; 39 40 import java.util.HashMap ; 41 import java.util.Map ; 42 43 import org.apache.commons.logging.Log; 44 45 56 public final class CmsMemoryObjectCache implements I_CmsEventListener { 57 58 59 private static final Log LOG = CmsLog.getLog(CmsMemoryObjectCache.class); 60 61 62 private static CmsMemoryObjectCache m_instance; 63 64 65 private Map m_cache; 66 67 70 private CmsMemoryObjectCache() { 71 72 m_cache = new HashMap (); 73 OpenCms.getMemoryMonitor().register(CmsMemoryObjectCache.class.getName() + ".m_cache", m_cache); 75 registerEventListener(); 77 } 78 79 84 public static CmsMemoryObjectCache getInstance() { 85 86 if (m_instance == null) { 87 m_instance = new CmsMemoryObjectCache(); 88 } 89 return m_instance; 90 } 91 92 95 public void cmsEvent(CmsEvent event) { 96 97 switch (event.getType()) { 98 case I_CmsEventListener.EVENT_CLEAR_CACHES: 99 m_cache.clear(); 101 if (LOG.isDebugEnabled()) { 102 LOG.debug(Messages.get().getBundle().key(Messages.LOG_ER_FLUSHED_CACHES_0)); 103 } 104 break; 105 default: 106 } 108 } 109 110 118 public Object getCachedObject(Class owner, String key) { 119 120 key = owner.getName().concat(key); 121 return m_cache.get(key); 122 } 123 124 131 public void putCachedObject(Class owner, String key, Object value) { 132 133 key = owner.getName().concat(key); 134 m_cache.put(key, value); 135 } 136 137 140 protected void registerEventListener() { 141 142 OpenCms.addCmsEventListener(this, new int[] {I_CmsEventListener.EVENT_CLEAR_CACHES}); 144 } 145 } | Popular Tags |