1 28 29 package com.opencms.template; 30 31 import org.opencms.main.CmsLog; 32 import org.opencms.main.OpenCms; 33 import org.opencms.monitor.CmsMemoryMonitor; 34 35 import org.apache.commons.collections.map.LRUMap; 36 37 45 public class CmsTemplateCache implements I_CmsTemplateCache { 46 47 48 private LRUMap m_templateCache = new LRUMap(1000); 49 50 53 public CmsTemplateCache() { 54 CmsMemoryMonitor monitor = OpenCms.getMemoryMonitor(); 55 if ((monitor != null) && monitor.enabled()) { 56 monitor.register(this.getClass().getName()+"."+"templateCache", m_templateCache); 57 } 58 59 if (CmsLog.INIT.isInfoEnabled()) { 60 CmsLog.INIT.info(". Loader init : XMLTemplate template cache initialized successfully"); 61 } 62 } 63 64 67 public void clearCache() { 68 if (CmsLog.getLog(this).isInfoEnabled()) { 69 CmsLog.getLog(this).info("Clearing template cache"); 70 } 71 m_templateCache.clear(); 72 } 73 74 79 public void clearCache(Object key) { 80 if (key instanceof String ) { 81 m_templateCache.remove(key); 82 } else { 83 if (CmsLog.getLog(this).isInfoEnabled()) { 84 CmsLog.getLog(this).info("Could not clear key from cache: " + key); 85 } 86 } 87 } 88 89 94 public byte[] get(Object key) { 95 if (CmsLog.getLog(this).isInfoEnabled()) { 96 CmsLog.getLog(this).info("Getting " + key + " from cache"); 97 } 98 if (key instanceof String ) { 99 return (byte[])m_templateCache.get(key); 100 } else { 101 if (CmsLog.getLog(this).isInfoEnabled()) { 102 CmsLog.getLog(this).info("Getting " + key + " from cache failed"); 103 } 104 return null; 105 } 106 } 107 108 114 public boolean has(Object key) { 115 if (key instanceof String ) { 116 return m_templateCache.get(key) != null; 117 } else { 118 if (CmsLog.getLog(this).isWarnEnabled()) { 119 CmsLog.getLog(this).warn(key + " is not instanceof String"); 120 } 121 return false; 122 } 123 } 124 125 130 public void put(Object key, byte[] content) { 131 if (key instanceof String ) { 132 m_templateCache.put(key, content); 133 } else { 134 if (CmsLog.getLog(this).isWarnEnabled()) { 135 CmsLog.getLog(this).warn(key + " is not instanceof String"); 136 } 137 } 138 } 139 } 140 | Popular Tags |