1 31 32 package org.opencms.cache; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.CmsResource; 36 import org.opencms.main.CmsEvent; 37 import org.opencms.main.CmsLog; 38 import org.opencms.main.I_CmsEventListener; 39 import org.opencms.main.OpenCms; 40 import org.opencms.xml.Messages; 41 42 import java.util.HashMap ; 43 import java.util.List ; 44 import java.util.Map ; 45 46 import org.apache.commons.logging.Log; 47 48 59 public final class CmsVfsMemoryObjectCache implements I_CmsEventListener { 60 61 62 private static final Log LOG = CmsLog.getLog(CmsVfsMemoryObjectCache.class); 63 64 65 private static CmsVfsMemoryObjectCache m_vfsMemoryObjectCache; 66 67 68 private Map m_cache; 69 70 73 private CmsVfsMemoryObjectCache() { 74 75 m_cache = new HashMap (); 76 OpenCms.getMemoryMonitor().register(CmsVfsMemoryObjectCache.class.getName() + ".m_cache", m_cache); 78 registerEventListener(); 80 } 81 82 87 public static CmsVfsMemoryObjectCache getVfsMemoryObjectCache() { 88 89 if (m_vfsMemoryObjectCache == null) { 90 m_vfsMemoryObjectCache = new CmsVfsMemoryObjectCache(); 91 } 92 return m_vfsMemoryObjectCache; 93 } 94 95 98 public void cmsEvent(CmsEvent event) { 99 100 CmsResource resource; 101 switch (event.getType()) { 102 case I_CmsEventListener.EVENT_PUBLISH_PROJECT: 103 case I_CmsEventListener.EVENT_CLEAR_CACHES: 104 m_cache.clear(); 106 if (LOG.isDebugEnabled()) { 107 LOG.debug(Messages.get().getBundle().key(Messages.LOG_ER_FLUSHED_CACHES_0)); 108 } 109 break; 110 case I_CmsEventListener.EVENT_RESOURCE_MODIFIED: 111 resource = (CmsResource)event.getData().get("resource"); 112 uncacheSystemId(resource.getRootPath()); 113 break; 114 case I_CmsEventListener.EVENT_RESOURCE_DELETED: 115 List resources = (List )event.getData().get("resources"); 116 for (int i = 0; i < resources.size(); i++) { 117 resource = (CmsResource)resources.get(i); 118 uncacheSystemId(resource.getRootPath()); 119 } 120 break; 121 default: 122 } 124 } 125 126 133 public Object getCachedObject(CmsObject cms, String rootPath) { 134 135 String key = getCacheKeyForCurrentProject(cms, rootPath); 136 return m_cache.get(key); 137 } 138 139 146 public void putCachedObject(CmsObject cms, String rootPath, Object value) { 147 148 String key = getCacheKeyForCurrentProject(cms, rootPath); 149 m_cache.put(key, value); 150 } 151 152 155 protected void registerEventListener() { 156 157 OpenCms.addCmsEventListener(this, new int[] { 159 I_CmsEventListener.EVENT_CLEAR_CACHES, 160 I_CmsEventListener.EVENT_PUBLISH_PROJECT, 161 I_CmsEventListener.EVENT_RESOURCE_MODIFIED, 162 I_CmsEventListener.EVENT_RESOURCE_DELETED}); 163 } 164 165 174 private String getCacheKey(String systemId, boolean online) { 175 176 if (online) { 177 return "online_".concat(systemId); 178 } 179 return "offline_".concat(systemId); 180 } 181 182 190 private String getCacheKeyForCurrentProject(CmsObject cms, String rootPath) { 191 192 boolean project = (cms != null) ? cms.getRequestContext().currentProject().isOnlineProject() : false; 194 195 return getCacheKey(rootPath, project); 196 } 197 198 205 private void uncacheSystemId(String systemId) { 206 207 Object o; 208 o = m_cache.remove(getCacheKey(systemId, false)); 209 if ((null != o) && LOG.isDebugEnabled()) { 210 LOG.debug(Messages.get().getBundle().key(Messages.LOG_ER_UNCACHED_SYS_ID_1, getCacheKey(systemId, false))); 211 } 212 } 213 } | Popular Tags |