001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/PostCache.java,v 1.20 2007/10/18 04:38:54 tbtrung Exp $
003: * $Author: tbtrung $
004: * $Revision: 1.20 $
005: * $Date: 2007/10/18 04:38:54 $
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: import java.sql.Timestamp;
045:
046: import net.myvietnam.mvncore.exception.DatabaseException;
047: import net.myvietnam.mvncore.exception.ObjectNotFoundException;
048: import net.myvietnam.mvncore.util.AssertionUtil;
049: import net.myvietnam.mvncore.util.DateUtil;
050:
051: import org.apache.commons.logging.Log;
052: import org.apache.commons.logging.LogFactory;
053:
054: import com.mvnforum.MVNForumConfig;
055: import com.whirlycott.cache.*;
056:
057: public class PostCache {
058:
059: private static Log log = LogFactory.getLog(PostCache.class);
060:
061: public static final long TIME_OUT = DateUtil.MINUTE * 10;
062:
063: // static singleton variable
064: static private PostCache instance = new PostCache();
065:
066: // instance variable
067: private Cache cache;
068:
069: public PostCache() {
070: //Use the cache manager to create the default cache
071: try {
072: if (MVNForumConfig.getEnableCachePost()) {
073: cache = CacheManager.getInstance().getCache("post");
074: }
075: } catch (CacheException ex) {
076: log
077: .error(
078: "Cannot get the WhirlyCache. Post caching is disabled.",
079: ex);
080: } catch (LinkageError e) {
081: // @todo: Should be never throw
082: log
083: .error(
084: "Cannot get the WhirlyCache caused by Package Conflict. Post caching is disabled.",
085: e);
086: }
087: }
088:
089: /**
090: * Returns the single instance
091: * @return PostCache : the singleton instance.
092: *
093: * NOTE: if use normal singleton pattern, this method should be synchronized
094: */
095: static public PostCache getInstance() {
096: return instance;
097: }
098:
099: public String getEfficiencyReport() {
100: String result = "No report";
101: if (cache == null) {
102: if (MVNForumConfig.getEnableCachePost() == false) {
103: result = "Cache is disabled.";
104: } else {
105: result = "Cache cannot be inited";
106: }
107: } else if (cache instanceof CacheDecorator) {
108: result = ((CacheDecorator) cache).getEfficiencyReport();
109: }
110: return result;
111: }
112:
113: public void clear() {
114: if (cache != null) {
115: cache.clear();
116: }
117: }
118:
119: public PostBean getLastEnablePost_inThread(int threadID)
120: throws DatabaseException {
121:
122: PostBean postBean = null;
123: if (cache != null) {
124: String key = new String("getLastEnablePost_inThread"
125: + threadID);
126: postBean = (PostBean) cache.retrieve(key);
127: if (postBean == null) {
128: //log.debug("PostCache: about to call getLastEnablePost_inThread with id = " + threadID);
129:
130: Collection cPostTemp = DAOFactory.getPostDAO()
131: .getLastEnablePosts_inThread_limit(threadID, 1);
132: AssertionUtil.doAssert(cPostTemp.size() == 1,
133: "Assertion: PostBeans size == 1 (but the value = "
134: + cPostTemp.size() + ")");
135: postBean = (PostBean) (cPostTemp.iterator().next());
136:
137: cache.store(key, postBean, TIME_OUT);
138: }
139: } else {
140: Collection cPostTemp = DAOFactory.getPostDAO()
141: .getLastEnablePosts_inThread_limit(threadID, 1);
142: AssertionUtil.doAssert(cPostTemp.size() == 1,
143: "Assertion: PostBeans size == 1 (but the value = "
144: + cPostTemp.size() + ")");
145: postBean = (PostBean) (cPostTemp.iterator().next());
146: }
147: AssertionUtil
148: .doAssert(
149: postBean != null,
150: "Assertion cannot find the row in table Post where getLastEnablePosts_inThread_limit = ("
151: + threadID + ").");
152: return postBean;
153: }
154:
155: public Collection getEnablePosts_inThread_limit(int threadID,
156: int offset, int rowsToReturn) throws DatabaseException {
157:
158: Collection result = null;
159: if (cache != null) {
160: StringBuffer buffer = new StringBuffer(128);
161: buffer.append("getEnablePosts_inThread_limit");
162: buffer.append(threadID).append("_");
163: buffer.append(offset).append("_");
164: buffer.append(rowsToReturn).append("_");
165: String key = buffer.toString();
166: result = (Collection) cache.retrieve(key);
167: if (result == null) {
168: result = DAOFactory.getPostDAO()
169: .getEnablePosts_inThread_limit(threadID,
170: offset, rowsToReturn);
171:
172: cache.store(key, result, TIME_OUT);
173: }
174: } else {
175: result = DAOFactory.getPostDAO()
176: .getEnablePosts_inThread_limit(threadID, offset,
177: rowsToReturn);
178: }
179:
180: return result;
181: }
182:
183: public Collection getMostActiveMembers(Timestamp since,
184: int rowsToReturn) throws DatabaseException {
185:
186: Collection result = null;
187: if (cache != null) {
188: StringBuffer buffer = new StringBuffer(128);
189: buffer.append("getMostActiveMembers");
190: //buffer.append(since.getTime()).append("_");
191: buffer.append(rowsToReturn).append("_");
192: String key = buffer.toString();
193: result = (Collection) cache.retrieve(key);
194: if (result == null) {
195: result = DAOFactory.getPostDAO().getMostActiveMembers(
196: since, rowsToReturn);
197:
198: cache.store(key, result, TIME_OUT);
199: }
200: } else {
201: result = DAOFactory.getPostDAO().getMostActiveMembers(
202: since, rowsToReturn);
203: }
204:
205: return result;
206: }
207:
208: public Collection getMostActiveThreads(Timestamp since,
209: int rowsToReturn) throws DatabaseException {
210:
211: Collection result = null;
212: if (cache != null) {
213: StringBuffer buffer = new StringBuffer(128);
214: buffer.append("getMostActiveThreads");
215: //buffer.append(since.getTime()).append("_");
216: buffer.append(rowsToReturn).append("_");
217: String key = buffer.toString();
218: result = (Collection) cache.retrieve(key);
219: if (result == null) {
220: result = DAOFactory.getPostDAO().getMostActiveThreads(
221: since, rowsToReturn);
222:
223: cache.store(key, result, TIME_OUT);
224: }
225: } else {
226: result = DAOFactory.getPostDAO().getMostActiveThreads(
227: since, rowsToReturn);
228: }
229:
230: return result;
231: }
232:
233: public PostBean getPost(int postID) throws DatabaseException,
234: ObjectNotFoundException {
235:
236: PostBean result = null;
237: if (cache != null) {
238: StringBuffer buffer = new StringBuffer(128);
239: buffer.append("getPost_").append(postID);
240: String key = buffer.toString();
241: result = (PostBean) cache.retrieve(key);
242: if (result == null) {
243: result = DAOFactory.getPostDAO().getPost(postID);
244:
245: cache.store(key, result, TIME_OUT);
246: }
247: } else {
248: result = DAOFactory.getPostDAO().getPost(postID);
249: }
250:
251: return result;
252: }
253:
254: }
|