001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.cache; 029 030import org.opencms.main.CmsEvent; 031import org.opencms.main.CmsLog; 032import org.opencms.main.I_CmsEventListener; 033import org.opencms.main.OpenCms; 034import org.opencms.monitor.CmsMemoryMonitor; 035 036import org.apache.commons.logging.Log; 037 038/** 039 * A singleton memory cache, that stores objects related with keys.<p> 040 * 041 * This cache listens to the {@link I_CmsEventListener#EVENT_CLEAR_CACHES} event only.<p> 042 * 043 * @since 6.2.3 044 */ 045public final class CmsMemoryObjectCache implements I_CmsEventListener { 046 047 /** The log object for this class. */ 048 private static final Log LOG = CmsLog.getLog(CmsMemoryObjectCache.class); 049 050 /** A cache that maps VFS resource names to Objects. */ 051 private static CmsMemoryObjectCache m_instance; 052 053 /** 054 * Constructor, creates a new CmsVfsMemoryObjectCache.<p> 055 */ 056 private CmsMemoryObjectCache() { 057 058 // register the event listeners 059 registerEventListener(); 060 } 061 062 /** 063 * Returns the singleton memory Object cache instance.<p> 064 * 065 * @return the singleton memory Object cache instance 066 */ 067 public static CmsMemoryObjectCache getInstance() { 068 069 if (m_instance == null) { 070 m_instance = new CmsMemoryObjectCache(); 071 } 072 return m_instance; 073 } 074 075 /** 076 * @see org.opencms.main.I_CmsEventListener#cmsEvent(org.opencms.main.CmsEvent) 077 */ 078 public void cmsEvent(CmsEvent event) { 079 080 switch (event.getType()) { 081 case I_CmsEventListener.EVENT_CLEAR_CACHES: 082 // flush cache 083 OpenCms.getMemoryMonitor().flushCache(CmsMemoryMonitor.CacheType.MEMORY_OBJECT); 084 if (LOG.isDebugEnabled()) { 085 LOG.debug( 086 org.opencms.xml.Messages.get().getBundle().key( 087 org.opencms.xml.Messages.LOG_ERR_FLUSHED_CACHES_0)); 088 } 089 break; 090 default: 091 // no operation 092 } 093 } 094 095 /** 096 * Returns an object from the cache.<p> 097 * 098 * @param owner the owner class of the cached object (used to ensure keys don't overlap) 099 * @param key the key to lookup the object for 100 * 101 * @return an object from the cache, or <code>null</code> if no object matches the given key 102 */ 103 public Object getCachedObject(Class<?> owner, String key) { 104 105 key = owner.getName().concat(key); 106 return OpenCms.getMemoryMonitor().getCachedMemObject(key); 107 } 108 109 /** 110 * Puts an object into the cache.<p> 111 * 112 * @param owner the owner class of the cached object (used to ensure keys don't overlap) 113 * @param key the key to store the object at 114 * @param value the object to store 115 */ 116 public void putCachedObject(Class<?> owner, String key, Object value) { 117 118 key = owner.getName().concat(key); 119 OpenCms.getMemoryMonitor().cacheMemObject(key, value); 120 } 121 122 /** 123 * Registers all required event listeners.<p> 124 */ 125 protected void registerEventListener() { 126 127 // register this object as event listener 128 OpenCms.addCmsEventListener(this, new int[] {I_CmsEventListener.EVENT_CLEAR_CACHES}); 129 } 130}