1 41 package com.mvnforum.db; 42 43 import java.util.Collection ; 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 ; 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 private PostCache instance = new PostCache(); 62 63 private Cache cache; 65 66 public PostCache() { 67 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 e) { 75 log.error("Cannot get the WhirlyCache caused by Package Conflict. Post caching is disabled.", e); 77 } 78 } 79 80 86 static public PostCache getInstance() { 87 return instance; 88 } 89 90 public String getEfficiencyReport() { 91 String 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 key = new String ("getLastEnablePost_inThread" + threadID); 116 postBean = (PostBean)cache.retrieve(key); 117 if (postBean == null) { 118 120 Collection cPostTemp = DAOFactory.getPostDAO().getLastEnablePosts_inThread_limit(threadID, 1); 121 if (cPostTemp.size() != 1) { 122 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 cPostTemp = DAOFactory.getPostDAO().getLastEnablePosts_inThread_limit(threadID, 1); 132 if (cPostTemp.size() != 1) { 133 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 getEnablePosts_inThread_limit(int threadID, int offset, int rowsToReturn) 148 throws DatabaseException { 149 150 Collection result = null; 151 if (cache != null) { 152 StringBuffer buffer = new StringBuffer (128); 153 buffer.append("getEnablePosts_inThread_limit"); 154 buffer.append(threadID).append("_"); 155 buffer.append(offset).append("_"); 156 buffer.append(rowsToReturn).append("_"); 157 String key = buffer.toString(); 158 result = (Collection )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 getMostActiveMembers(Timestamp since, int rowsToReturn) 172 throws DatabaseException { 173 174 Collection result = null; 175 if (cache != null) { 176 StringBuffer buffer = new StringBuffer (128); 177 buffer.append("getMostActiveMembers"); 178 buffer.append(rowsToReturn).append("_"); 180 String key = buffer.toString(); 181 result = (Collection )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 getMostActiveThreads(Timestamp since, int rowsToReturn) 195 throws DatabaseException { 196 197 Collection result = null; 198 if (cache != null) { 199 StringBuffer buffer = new StringBuffer (128); 200 buffer.append("getMostActiveThreads"); 201 buffer.append(rowsToReturn).append("_"); 203 String key = buffer.toString(); 204 result = (Collection )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 |