KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > template > CmsTemplateCache


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/template/CmsTemplateCache.java,v $
3 * Date : $Date: 2005/06/13 10:00:03 $
4 * Version: $Revision: 1.4 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * For further information about OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */

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 /**
38  * Implements the OpenCms template cache.<p>
39  *
40  * @author Alexander Lucas
41  * @version $Revision: 1.4 $ $Date: 2005/06/13 10:00:03 $
42  *
43  * @deprecated Will not be supported past the OpenCms 6 release.
44  */

45 public class CmsTemplateCache implements I_CmsTemplateCache {
46     
47     /** Hashtable to store the cached data. */
48     private LRUMap m_templateCache = new LRUMap(1000);
49     
50     /**
51      * Default constructor to create a template cache.<p>
52      */

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     /**
65      * Deletes all documents from the template cache.
66      */

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     /**
75      * Deletes the document with the given key from the
76      * template cache.
77      * @param key Key of the template that should be deleted.
78      */

79     public void clearCache(Object JavaDoc key) {
80         if (key instanceof String JavaDoc) {
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     /**
90      * Gets a previously cached template with the given key.
91      * @param key Key of the requested template.
92      * @return byte array with the cached template content or null if no cached value was found.
93      */

94     public byte[] get(Object JavaDoc key) {
95         if (CmsLog.getLog(this).isInfoEnabled()) {
96             CmsLog.getLog(this).info("Getting " + key + " from cache");
97         }
98         if (key instanceof String JavaDoc) {
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     /**
109      * Checks if there exists a cached template content for
110      * a given key.
111      * @param key Key that should be checked.
112      * @return <EM>true</EM> if a cached content was found, <EM>false</EM> otherwise.
113      */

114     public boolean has(Object JavaDoc key) {
115         if (key instanceof String JavaDoc) {
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     /**
126      * Stores a template content in the cache using the given key.
127      * @param key Key that should be used to store the template
128      * @param content Template content to store.
129      */

130     public void put(Object JavaDoc key, byte[] content) {
131         if (key instanceof String JavaDoc) {
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