1 41 package com.mvnforum.db; 42 43 import java.util.*; 44 45 import net.myvietnam.mvncore.exception.DatabaseException; 46 import net.myvietnam.mvncore.exception.ObjectNotFoundException; 47 import net.myvietnam.mvncore.util.DateUtil; 48 49 import org.apache.commons.logging.Log; 50 import org.apache.commons.logging.LogFactory; 51 52 import com.mvnforum.MVNForumConfig; 53 import com.whirlycott.cache.*; 54 55 public class CategoryCache { 56 57 public static final long TIME_OUT = DateUtil.HOUR; 58 59 private static Log log = LogFactory.getLog(ForumCache.class); 60 61 static private CategoryCache instance = new CategoryCache(); 63 64 private Cache cache; 66 67 public CategoryCache() { 68 try { 70 if (MVNForumConfig.getEnableCacheCategory()) { 71 cache = CacheManager.getInstance().getCache("category"); 72 } 73 } catch (CacheException ex) { 74 log.error("Cannot get the WhirlyCache. Category caching is disabled.", ex); 75 } catch (LinkageError e) { 76 log.error("Cannot get the WhirlyCache caused by Package Conflict. Category caching is disabled.", e); 78 } 79 } 80 81 87 static public CategoryCache getInstance() { 88 return instance; 89 } 90 91 public String getEfficiencyReport() { 92 String result = "No report"; 93 if (cache == null) { 94 if (MVNForumConfig.getEnableCacheCategory() == false) { 95 result = "Cache is disabled."; 96 } else { 97 result = "Cache cannot be inited"; 98 } 99 } else if (cache instanceof CacheDecorator) { 100 result = ((CacheDecorator) cache).getEfficiencyReport(); 101 } 102 return result; 103 } 104 105 public void clear() { 106 if (cache != null) { 107 cache.clear(); 108 } 109 } 110 111 114 117 122 123 127 public List getBeans() throws DatabaseException { 128 List result = null; 129 if (cache != null) { 130 StringBuffer buffer = new StringBuffer (128); 131 buffer.append("getBeans"); 132 String key = buffer.toString(); 133 result = (List) cache.retrieve(key); 134 if (result == null) { 135 result = (List) DAOFactory.getCategoryDAO().getCategories(); 136 cache.store(key, result, TIME_OUT); 137 } 138 } else { 139 result = (List) DAOFactory.getCategoryDAO().getCategories(); 140 } 141 return Collections.unmodifiableList(result); 142 } 143 144 public CategoryBean getBean(int categoryID) throws DatabaseException, ObjectNotFoundException { 145 146 List beans = getBeans(); 149 int size = beans.size(); 150 for (int i = 0; i < size; i++) { 151 CategoryBean bean = (CategoryBean) beans.get(i); 152 if (bean.getCategoryID() == categoryID) { 153 return bean; 154 } 155 } 156 throw new ObjectNotFoundException("Cannot find the row in table Category " + "where primary key = (" + categoryID + ")."); 158 } 159 160 public CategoryBean getBean(String categoryName) throws DatabaseException, ObjectNotFoundException { 161 162 List beans = getBeans(); 165 for (Iterator it = beans.iterator(); it.hasNext();) { 166 CategoryBean bean = (CategoryBean) it.next(); 167 if (bean.getCategoryName().equals(categoryName)) { 168 return bean; 169 } 170 } 171 throw new ObjectNotFoundException("Cannot find a category with the given name: " + categoryName); 173 } 174 175 } 176 | Popular Tags |