001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/ThreadCache.java,v 1.11 2007/10/16 06:49:37 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.11 $
005: * $Date: 2007/10/16 06:49:37 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Minh Nguyen
039: * @author: Mai Nguyen
040: */
041: package com.mvnforum.db;
042:
043: import java.util.Collection;
044:
045: import net.myvietnam.mvncore.exception.DatabaseException;
046: import net.myvietnam.mvncore.util.DateUtil;
047:
048: import org.apache.commons.logging.Log;
049: import org.apache.commons.logging.LogFactory;
050:
051: import com.mvnforum.MVNForumConfig;
052: import com.whirlycott.cache.*;
053:
054: public class ThreadCache {
055:
056: private static Log log = LogFactory.getLog(ThreadCache.class);
057:
058: public static final long TIME_OUT = DateUtil.MINUTE * 10;
059:
060: // static singleton variable
061: static private ThreadCache instance = new ThreadCache();
062:
063: // instance variable
064: private Cache cache;
065:
066: public ThreadCache() {
067: //Use the cache manager to create the default cache
068: try {
069: if (MVNForumConfig.getEnableCacheThread()) {
070: cache = CacheManager.getInstance().getCache("thread");
071: }
072: } catch (CacheException ex) {
073: log
074: .error(
075: "Cannot get the WhirlyCache. Thread caching is disabled.",
076: ex);
077: } catch (LinkageError e) {
078: // @todo: Should be never throw
079: log
080: .error(
081: "Cannot get the WhirlyCache caused by Package Conflict. Thread caching is disabled.",
082: e);
083: }
084: }
085:
086: /**
087: * Returns the single instance
088: * @return ThreadCache : the singleton instance.
089: *
090: * NOTE: if use normal singleton pattern, this method should be synchronized
091: */
092: static public ThreadCache getInstance() {
093: return instance;
094: }
095:
096: public String getEfficiencyReport() {
097: String result = "No report";
098: if (cache == null) {
099: if (MVNForumConfig.getEnableCacheThread() == false) {
100: result = "Cache is disabled.";
101: } else {
102: result = "Cache cannot be inited";
103: }
104: } else if (cache instanceof CacheDecorator) {
105: result = ((CacheDecorator) cache).getEfficiencyReport();
106: }
107: return result;
108: }
109:
110: public void clear() {
111: if (cache != null) {
112: cache.clear();
113: }
114: }
115:
116: public int getPreviousEnableThread(int forumID, int threadID)
117: throws DatabaseException {
118:
119: Integer previousThreadID = null;
120: if (cache != null) {
121: StringBuffer buffer = new StringBuffer(128);
122: buffer.append("getPreviousEnableThread").append("_")
123: .append(forumID).append("_").append(threadID);
124: String key = buffer.toString();
125: previousThreadID = (Integer) cache.retrieve(key);
126: if (previousThreadID == null) {
127: int previousTopic = DAOFactory.getThreadDAO()
128: .getPreviousEnableThread(forumID, threadID);// can throw AssertionError
129: previousThreadID = new Integer(previousTopic);
130:
131: cache.store(key, previousThreadID, TIME_OUT);
132: }
133: } else {
134: int previousTopic = DAOFactory.getThreadDAO()
135: .getPreviousEnableThread(forumID, threadID);// can throw AssertionError
136: previousThreadID = new Integer(previousTopic);
137: }
138:
139: return previousThreadID.intValue();
140: }
141:
142: public int getNextEnableThread(int forumID, int threadID)
143: throws DatabaseException {
144:
145: Integer nextThreadID = null;
146: if (cache != null) {
147: StringBuffer buffer = new StringBuffer(128);
148: buffer.append("getNextEnableThread").append("_").append(
149: forumID).append("_").append(threadID);
150: String key = buffer.toString();
151: nextThreadID = (Integer) cache.retrieve(key);
152: if (nextThreadID == null) {
153: int previousTopic = DAOFactory.getThreadDAO()
154: .getNextEnableThread(forumID, threadID);// can throw AssertionError
155: nextThreadID = new Integer(previousTopic);
156:
157: cache.store(key, nextThreadID, TIME_OUT);
158: }
159: } else {
160: int previousTopic = DAOFactory.getThreadDAO()
161: .getNextEnableThread(forumID, threadID);// can throw AssertionError
162: nextThreadID = new Integer(previousTopic);
163: }
164:
165: return nextThreadID.intValue();
166: }
167:
168: public Collection getEnableGlobalAnnouncements()
169: throws DatabaseException {
170:
171: Collection result = null;
172: if (cache != null) {
173: StringBuffer buffer = new StringBuffer(128);
174: buffer.append("getEnableGlobalAnnouncements");
175: String key = buffer.toString();
176: result = (Collection) cache.retrieve(key);
177: if (result == null) {
178: result = DAOFactory.getThreadDAO()
179: .getEnableGlobalAnnouncements();
180:
181: cache.store(key, result, TIME_OUT);
182: }
183: } else {
184: result = DAOFactory.getThreadDAO()
185: .getEnableGlobalAnnouncements();
186: }
187:
188: return result;
189: }
190:
191: public Collection getEnableForumAnnouncements_inForum(int forumID)
192: throws DatabaseException {
193:
194: Collection result = null;
195: if (cache != null) {
196: StringBuffer buffer = new StringBuffer(128);
197: buffer.append("getEnableForumAnnouncements_inForum")
198: .append(forumID);
199: String key = buffer.toString();
200: result = (Collection) cache.retrieve(key);
201: if (result == null) {
202: result = DAOFactory.getThreadDAO()
203: .getEnableForumAnnouncements_inForum(forumID);
204:
205: cache.store(key, result, TIME_OUT);
206: }
207: } else {
208: result = DAOFactory.getThreadDAO()
209: .getEnableForumAnnouncements_inForum(forumID);
210: }
211:
212: return result;
213: }
214:
215: public Collection getEnableStickies_inForum(int forumID)
216: throws DatabaseException {
217:
218: Collection result = null;
219: if (cache != null) {
220: StringBuffer buffer = new StringBuffer(128);
221: buffer.append("getEnableStickies_inForum").append(forumID);
222: String key = buffer.toString();
223: result = (Collection) cache.retrieve(key);
224: if (result == null) {
225: result = DAOFactory.getThreadDAO()
226: .getEnableStickies_inForum(forumID);
227:
228: cache.store(key, result, TIME_OUT);
229: }
230: } else {
231: result = DAOFactory.getThreadDAO()
232: .getEnableStickies_inForum(forumID);
233: }
234:
235: return result;
236: }
237:
238: public Collection getNormalEnableThreads_inForum_withSortSupport_limit(
239: int forumID, int offset, int rowsToReturn, String sort,
240: String order) throws DatabaseException {
241:
242: Collection result = null;
243: if (cache != null) {
244: StringBuffer buffer = new StringBuffer(128);
245: buffer
246: .append("getNormalEnableThreads_inForum_withSortSupport_limit");
247: buffer.append(forumID).append("_");
248: buffer.append(offset).append("_");
249: buffer.append(rowsToReturn).append("_");
250: buffer.append(sort).append("_");
251: buffer.append(order).append("_");
252: String key = buffer.toString();
253: result = (Collection) cache.retrieve(key);
254: if (result == null) {
255: result = DAOFactory
256: .getThreadDAO()
257: .getNormalEnableThreads_inForum_withSortSupport_limit(
258: forumID, offset, rowsToReturn, sort,
259: order);
260:
261: cache.store(key, result, TIME_OUT);
262: }
263: } else {
264: result = DAOFactory
265: .getThreadDAO()
266: .getNormalEnableThreads_inForum_withSortSupport_limit(
267: forumID, offset, rowsToReturn, sort, order);
268: }
269:
270: return result;
271: }
272:
273: public int getNumberOfEnableThreads_inForum(int forumID)
274: throws DatabaseException {
275:
276: Integer result = null;
277: if (cache != null) {
278: StringBuffer buffer = new StringBuffer(128);
279: buffer.append("getNumberOfEnableThreads_inForum").append(
280: "_").append(forumID);
281: String key = buffer.toString();
282: result = (Integer) cache.retrieve(key);
283: if (result == null) {
284: int i = DAOFactory.getThreadDAO()
285: .getNumberOfEnableThreads_inForum(forumID);
286: result = new Integer(i);
287:
288: cache.store(key, result, TIME_OUT);
289: }
290: } else {
291: int i = DAOFactory.getThreadDAO()
292: .getNumberOfEnableThreads_inForum(forumID);
293: result = new Integer(i);
294: }
295:
296: return result.intValue();
297: }
298:
299: public int getNumberOfNormalEnableThreads_inForum(int forumID)
300: throws DatabaseException {
301:
302: Integer result = null;
303: if (cache != null) {
304: StringBuffer buffer = new StringBuffer(128);
305: buffer.append("getNumberOfNormalEnableThreads_inForum")
306: .append("_").append(forumID);
307: String key = buffer.toString();
308: result = (Integer) cache.retrieve(key);
309: if (result == null) {
310: int i = DAOFactory
311: .getThreadDAO()
312: .getNumberOfNormalEnableThreads_inForum(forumID);
313: result = new Integer(i);
314:
315: cache.store(key, result, TIME_OUT);
316: }
317: } else {
318: int i = DAOFactory.getThreadDAO()
319: .getNumberOfNormalEnableThreads_inForum(forumID);
320: result = new Integer(i);
321: }
322:
323: return result.intValue();
324: }
325:
326: }
|