KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > db > PostCache


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/PostCache.java,v 1.8 2006/04/14 17:05:26 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.8 $
5  * $Date: 2006/04/14 17:05:26 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Mai Nguyen
40  */

41 package com.mvnforum.db;
42
43 import java.util.Collection JavaDoc;
44
45 import com.mvnforum.MVNForumConfig;
46 import com.whirlycott.cache.*;
47 import net.myvietnam.mvncore.exception.AssertionException;
48 import net.myvietnam.mvncore.exception.DatabaseException;
49 import net.myvietnam.mvncore.util.DateUtil;
50 import org.apache.commons.logging.Log;
51 import org.apache.commons.logging.LogFactory;
52 import java.sql.Timestamp JavaDoc;
53
54 public class PostCache {
55
56     public static final long TIME_OUT = DateUtil.MINUTE * 10;
57
58     private static Log log = LogFactory.getLog(PostCache.class);
59
60     // static singleton variable
61
static private PostCache instance = new PostCache();
62
63     // instance variable
64
private Cache cache;
65
66     public PostCache() {
67         //Use the cache manager to create the default cache
68
try {
69             if (MVNForumConfig.getEnableCachePost()) {
70                 cache = CacheManager.getInstance().getCache("post");
71             }
72         } catch (CacheException ex) {
73             log.error("Cannot get the WhirlyCache. Post caching is disabled.", ex);
74         } catch (LinkageError JavaDoc e) {
75             // @todo: Should be never throw
76
log.error("Cannot get the WhirlyCache caused by Package Conflict. Post caching is disabled.", e);
77         }
78     }
79
80     /**
81      * Returns the single instance
82      * @return PostCache : the singleton instance.
83      *
84      * NOTE: if use normal singleton pattern, this method should be synchronized
85      */

86     static public PostCache getInstance() {
87         return instance;
88     }
89
90     public String JavaDoc getEfficiencyReport() {
91         String JavaDoc result = "No report";
92         if (cache == null) {
93             if (MVNForumConfig.getEnableCachePost() == false) {
94                 result = "Cache is disabled.";
95             } else {
96                 result = "Cache cannot be inited";
97             }
98         } else if (cache instanceof CacheDecorator) {
99             result = ((CacheDecorator)cache).getEfficiencyReport();
100         }
101         return result;
102     }
103
104     public void clear() {
105         if (cache != null) {
106             cache.clear();
107         }
108     }
109
110     public PostBean getLastEnablePost_inThread(int threadID)
111         throws DatabaseException, AssertionException {
112
113         PostBean postBean = null;
114         if (cache != null) {
115             String JavaDoc key = new String JavaDoc("getLastEnablePost_inThread" + threadID);
116             postBean = (PostBean)cache.retrieve(key);
117             if (postBean == null) {
118                 //log.debug("PostCache: about to call getLastEnablePost_inThread with id = " + threadID);
119

120                 Collection JavaDoc cPostTemp = DAOFactory.getPostDAO().getLastEnablePosts_inThread_limit(threadID, 1);
121                 if (cPostTemp.size() != 1) {
122                     //String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.wrong_size", new Object[] {"PostBeans", "==",new Integer(cPostTemp.size())});
123
//throw new AssertionException(localizedMessage);
124
throw new AssertionException("Assertion: PostBeans size == 1 (but the value = " + cPostTemp.size() + ")");
125                 }
126                 postBean = (PostBean) (cPostTemp.iterator().next());
127
128                 cache.store(key, postBean, TIME_OUT);
129             }
130         } else {
131             Collection JavaDoc cPostTemp = DAOFactory.getPostDAO().getLastEnablePosts_inThread_limit(threadID, 1);
132             if (cPostTemp.size() != 1) {
133                 //String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.wrong_size", new Object[] {"PostBeans", "==",new Integer(cPostTemp.size())});
134
//throw new AssertionException(localizedMessage);
135
throw new AssertionException("Assertion: PostBeans size == 1 (but the value = " + cPostTemp.size() + ")");
136             }
137             postBean = (PostBean) (cPostTemp.iterator().next());
138         }
139
140         if (postBean == null) {
141             throw new AssertionException("Assertion cannot find the row in table Post "
142                                           + "where getLastEnablePost_inThread = (" + threadID + ").");
143         }
144         return postBean;
145     }
146
147     public Collection JavaDoc getEnablePosts_inThread_limit(int threadID, int offset, int rowsToReturn)
148         throws DatabaseException {
149
150         Collection JavaDoc result = null;
151         if (cache != null) {
152             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
153             buffer.append("getEnablePosts_inThread_limit");
154             buffer.append(threadID).append("_");
155             buffer.append(offset).append("_");
156             buffer.append(rowsToReturn).append("_");
157             String JavaDoc key = buffer.toString();
158             result = (Collection JavaDoc)cache.retrieve(key);
159             if (result == null) {
160                 result = DAOFactory.getPostDAO().getEnablePosts_inThread_limit(threadID, offset, rowsToReturn);
161
162                 cache.store(key, result, TIME_OUT);
163             }
164         } else {
165             result = DAOFactory.getPostDAO().getEnablePosts_inThread_limit(threadID, offset, rowsToReturn);
166         }
167
168         return result;
169     }
170
171     public Collection JavaDoc getMostActiveMembers(Timestamp JavaDoc since, int rowsToReturn)
172         throws DatabaseException {
173
174         Collection JavaDoc result = null;
175         if (cache != null) {
176             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
177             buffer.append("getMostActiveMembers");
178             //buffer.append(since.getTime()).append("_");
179
buffer.append(rowsToReturn).append("_");
180             String JavaDoc key = buffer.toString();
181             result = (Collection JavaDoc)cache.retrieve(key);
182             if (result == null) {
183                 result = DAOFactory.getPostDAO().getMostActiveMembers(since, rowsToReturn);
184
185                 cache.store(key, result, TIME_OUT);
186             }
187         } else {
188             result = DAOFactory.getPostDAO().getMostActiveMembers(since, rowsToReturn);
189         }
190
191         return result;
192     }
193
194     public Collection JavaDoc getMostActiveThreads(Timestamp JavaDoc since, int rowsToReturn)
195         throws DatabaseException {
196
197         Collection JavaDoc result = null;
198         if (cache != null) {
199             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
200             buffer.append("getMostActiveThreads");
201             //buffer.append(since.getTime()).append("_");
202
buffer.append(rowsToReturn).append("_");
203             String JavaDoc key = buffer.toString();
204             result = (Collection JavaDoc)cache.retrieve(key);
205             if (result == null) {
206                 result = DAOFactory.getPostDAO().getMostActiveThreads(since, rowsToReturn);
207
208                 cache.store(key, result, TIME_OUT);
209             }
210         } else {
211             result = DAOFactory.getPostDAO().getMostActiveThreads(since, rowsToReturn);
212         }
213
214         return result;
215     }
216
217 }
218
Popular Tags