KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/ThreadCache.java,v 1.7 2006/04/14 17:05:26 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.7 $
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
53 public class ThreadCache {
54
55     public static final long TIME_OUT = DateUtil.MINUTE * 10;
56
57     private static Log log = LogFactory.getLog(ThreadCache.class);
58
59     // static singleton variable
60
static private ThreadCache instance = new ThreadCache();
61
62     // instance variable
63
private Cache cache;
64
65     public ThreadCache() {
66         //Use the cache manager to create the default cache
67
try {
68             if (MVNForumConfig.getEnableCacheThread()) {
69                 cache = CacheManager.getInstance().getCache("thread");
70             }
71         } catch (CacheException ex) {
72             log.error("Cannot get the WhirlyCache. Thread caching is disabled.", ex);
73         } catch (LinkageError JavaDoc e) {
74             // @todo: Should be never throw
75
log.error("Cannot get the WhirlyCache caused by Package Conflict. Thread caching is disabled.", e);
76         }
77     }
78
79     /**
80      * Returns the single instance
81      * @return ThreadCache : the singleton instance.
82      *
83      * NOTE: if use normal singleton pattern, this method should be synchronized
84      */

85     static public ThreadCache getInstance() {
86         return instance;
87     }
88
89     public String JavaDoc getEfficiencyReport() {
90         String JavaDoc result = "No report";
91         if (cache == null) {
92             if (MVNForumConfig.getEnableCacheThread() == false) {
93                 result = "Cache is disabled.";
94             } else {
95                 result = "Cache cannot be inited";
96             }
97         } else if (cache instanceof CacheDecorator) {
98             result = ((CacheDecorator)cache).getEfficiencyReport();
99         }
100         return result;
101     }
102
103     public void clear() {
104         if (cache != null) {
105             cache.clear();
106         }
107     }
108
109     public int getPreviousEnableThread(int forumID, int threadID)
110         throws DatabaseException, AssertionException {
111
112         Integer JavaDoc previousThreadID = null;
113         if (cache != null) {
114             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
115             buffer.append("getPreviousEnableThread").append("_").append(forumID).append("_").append(threadID);
116             String JavaDoc key = buffer.toString();
117             previousThreadID = (Integer JavaDoc)cache.retrieve(key);
118             if (previousThreadID == null) {
119                 int previousTopic = DAOFactory.getThreadDAO().getPreviousEnableThread(forumID, threadID);// can throw AssertionException
120
previousThreadID = new Integer JavaDoc(previousTopic);
121
122                 cache.store(key, previousThreadID, TIME_OUT);
123             }
124         } else {
125             int previousTopic = DAOFactory.getThreadDAO().getPreviousEnableThread(forumID, threadID);// can throw AssertionException
126
previousThreadID = new Integer JavaDoc(previousTopic);
127         }
128
129         return previousThreadID.intValue();
130     }
131
132     public int getNextEnableThread(int forumID, int threadID)
133         throws DatabaseException, AssertionException {
134
135         Integer JavaDoc nextThreadID = null;
136         if (cache != null) {
137             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
138             buffer.append("getNextEnableThread").append("_").append(forumID).append("_").append(threadID);
139             String JavaDoc key = buffer.toString();
140             nextThreadID = (Integer JavaDoc)cache.retrieve(key);
141             if (nextThreadID == null) {
142                 int previousTopic = DAOFactory.getThreadDAO().getNextEnableThread(forumID, threadID);// can throw AssertionException
143
nextThreadID = new Integer JavaDoc(previousTopic);
144
145                 cache.store(key, nextThreadID, TIME_OUT);
146             }
147         } else {
148             int previousTopic = DAOFactory.getThreadDAO().getNextEnableThread(forumID, threadID);// can throw AssertionException
149
nextThreadID = new Integer JavaDoc(previousTopic);
150         }
151
152         return nextThreadID.intValue();
153     }
154
155     public Collection JavaDoc getEnableGlobalAnnouncements()
156         throws DatabaseException {
157
158         Collection JavaDoc result = null;
159         if (cache != null) {
160             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
161             buffer.append("getEnableGlobalAnnouncements");
162             String JavaDoc key = buffer.toString();
163             result = (Collection JavaDoc)cache.retrieve(key);
164             if (result == null) {
165                 result = DAOFactory.getThreadDAO().getEnableGlobalAnnouncements();
166
167                 cache.store(key, result, TIME_OUT);
168             }
169         } else {
170             result = DAOFactory.getThreadDAO().getEnableGlobalAnnouncements();
171         }
172
173         return result;
174     }
175
176     public Collection JavaDoc getEnableForumAnnouncements_inForum(int forumID)
177         throws DatabaseException {
178
179         Collection JavaDoc result = null;
180         if (cache != null) {
181             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
182             buffer.append("getEnableForumAnnouncements_inForum").append(forumID);
183             String JavaDoc key = buffer.toString();
184             result = (Collection JavaDoc)cache.retrieve(key);
185             if (result == null) {
186                 result = DAOFactory.getThreadDAO().getEnableForumAnnouncements_inForum(forumID);
187
188                 cache.store(key, result, TIME_OUT);
189             }
190         } else {
191             result = DAOFactory.getThreadDAO().getEnableForumAnnouncements_inForum(forumID);
192         }
193
194         return result;
195     }
196
197     public Collection JavaDoc getEnableStickies_inForum(int forumID)
198         throws DatabaseException {
199
200         Collection JavaDoc result = null;
201         if (cache != null) {
202             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
203             buffer.append("getEnableStickies_inForum").append(forumID);
204             String JavaDoc key = buffer.toString();
205             result = (Collection JavaDoc)cache.retrieve(key);
206             if (result == null) {
207                 result = DAOFactory.getThreadDAO().getEnableStickies_inForum(forumID);
208
209                 cache.store(key, result, TIME_OUT);
210             }
211         } else {
212             result = DAOFactory.getThreadDAO().getEnableStickies_inForum(forumID);
213         }
214
215         return result;
216     }
217
218     public Collection JavaDoc getNormalEnableThreads_inForum_withSortSupport_limit(int forumID, int offset, int rowsToReturn, String JavaDoc sort, String JavaDoc order)
219         throws DatabaseException {
220
221         Collection JavaDoc result = null;
222         if (cache != null) {
223             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
224             buffer.append("getNormalEnableThreads_inForum_withSortSupport_limit");
225             buffer.append(forumID).append("_");
226             buffer.append(offset).append("_");
227             buffer.append(rowsToReturn).append("_");
228             buffer.append(sort).append("_");
229             buffer.append(order).append("_");
230             String JavaDoc key = buffer.toString();
231             result = (Collection JavaDoc)cache.retrieve(key);
232             if (result == null) {
233                 result = DAOFactory.getThreadDAO().getNormalEnableThreads_inForum_withSortSupport_limit(forumID, offset, rowsToReturn, sort, order);
234
235                 cache.store(key, result, TIME_OUT);
236             }
237         } else {
238             result = DAOFactory.getThreadDAO().getNormalEnableThreads_inForum_withSortSupport_limit(forumID, offset, rowsToReturn, sort, order);
239         }
240
241         return result;
242     }
243
244     public int getNumberOfEnableThreads_inForum(int forumID)
245         throws DatabaseException, AssertionException {
246
247         Integer JavaDoc result = null;
248         if (cache != null) {
249             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
250             buffer.append("getNumberOfEnableThreads_inForum").append("_").append(forumID);
251             String JavaDoc key = buffer.toString();
252             result = (Integer JavaDoc)cache.retrieve(key);
253             if (result == null) {
254                 int i = DAOFactory.getThreadDAO().getNumberOfEnableThreads_inForum(forumID);
255                 result = new Integer JavaDoc(i);
256
257                 cache.store(key, result, TIME_OUT);
258             }
259         } else {
260             int i = DAOFactory.getThreadDAO().getNumberOfEnableThreads_inForum(forumID);
261             result = new Integer JavaDoc(i);
262         }
263
264         return result.intValue();
265     }
266
267     public int getNumberOfNormalEnableThreads_inForum(int forumID)
268         throws DatabaseException, AssertionException {
269
270         Integer JavaDoc result = null;
271         if (cache != null) {
272             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
273             buffer.append("getNumberOfNormalEnableThreads_inForum").append("_").append(forumID);
274             String JavaDoc key = buffer.toString();
275             result = (Integer JavaDoc)cache.retrieve(key);
276             if (result == null) {
277                 int i = DAOFactory.getThreadDAO().getNumberOfNormalEnableThreads_inForum(forumID);
278                 result = new Integer JavaDoc(i);
279
280                 cache.store(key, result, TIME_OUT);
281             }
282         } else {
283             int i = DAOFactory.getThreadDAO().getNumberOfNormalEnableThreads_inForum(forumID);
284             result = new Integer JavaDoc(i);
285         }
286
287         return result.intValue();
288     }
289
290 }
291
Popular Tags