1 43 package net.jforum.repository; 44 45 import java.util.ArrayList ; 46 import java.util.HashMap ; 47 import java.util.Iterator ; 48 import java.util.LinkedList ; 49 import java.util.List ; 50 import java.util.Map ; 51 52 import net.jforum.cache.CacheEngine; 53 import net.jforum.cache.Cacheable; 54 import net.jforum.dao.DataAccessDriver; 55 import net.jforum.dao.TopicDAO; 56 import net.jforum.entities.Topic; 57 import net.jforum.util.preferences.ConfigKeys; 58 import net.jforum.util.preferences.SystemGlobals; 59 60 67 public class TopicRepository implements Cacheable 68 { 69 private static int maxItems = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE); 70 71 private static final String FQN = "topics"; 72 private static final String RECENT = "recent"; 73 private static final String FQN_FORUM = FQN + "/byforum"; 74 private static final String RELATION = "relation"; 75 private static final String FQN_LOADED = FQN + "/loaded"; 76 77 private static CacheEngine cache; 78 79 82 public void setCacheEngine(CacheEngine engine) 83 { 84 cache = engine; 85 } 86 87 public static boolean isLoaded(int forumId) 88 { 89 return "1".equals(cache.get(FQN_LOADED, Integer.toString(forumId))); 90 } 91 92 97 public synchronized static void pushTopic(Topic topic) throws Exception 98 { 99 if (SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) { 100 int limit = SystemGlobals.getIntValue(ConfigKeys.RECENT_TOPICS); 101 102 LinkedList l = (LinkedList )cache.get(FQN, RECENT); 103 if (l == null || l.size() == 0) { 104 l = new LinkedList (loadMostRecentTopics()); 105 } 106 107 l.remove(topic); 108 l.addFirst(topic); 109 110 while (l.size() > limit) { 111 l.removeLast(); 112 } 113 114 cache.add(FQN, RECENT, l); 115 } 116 } 117 118 123 public synchronized static void popTopic(Topic topic) throws Exception 124 { 125 if (SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) { 126 List l = (List )cache.get(FQN, RECENT); 127 128 if (l == null || l.size() == 0) { 129 l = new LinkedList (loadMostRecentTopics()); 130 } 131 132 l.remove(topic); 133 cache.add(FQN, RECENT, l); 134 } 135 } 136 137 141 public static List getRecentTopics() throws Exception 142 { 143 List l = (List )cache.get(FQN, RECENT); 144 145 if (l == null || l.size() == 0 146 || !SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) { 147 l = loadMostRecentTopics(); 148 } 149 150 return new ArrayList (l); 151 } 152 153 156 public static List loadMostRecentTopics() throws Exception 157 { 158 TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO(); 159 int limit = SystemGlobals.getIntValue(ConfigKeys.RECENT_TOPICS); 160 161 List l = tm.selectRecentTopics(limit); 162 cache.add(FQN, RECENT, new LinkedList (l)); 163 164 return l; 165 } 166 172 public static void addAll(int forumId, List topics) 173 { 174 synchronized (FQN_FORUM) { 175 cache.add(FQN_FORUM, Integer.toString(forumId), new LinkedList (topics)); 176 177 Map m = (Map )cache.get(FQN, RELATION); 178 179 if (m == null) { 180 m = new HashMap (); 181 } 182 183 Integer fId = new Integer (forumId); 184 185 for (Iterator iter = topics.iterator(); iter.hasNext(); ) { 186 Topic t = (Topic)iter.next(); 187 188 m.put(new Integer (t.getId()), fId); 189 } 190 191 cache.add(FQN, RELATION, m); 192 cache.add(FQN_LOADED, Integer.toString(forumId), "1"); 193 } 194 } 195 196 201 public static void clearCache(int forumId) throws Exception 202 { 203 synchronized (FQN_FORUM) { 204 cache.add(FQN_FORUM, Integer.toString(forumId), new LinkedList ()); 205 cache.remove(FQN, RELATION); 206 } 207 } 208 209 214 public static void addTopic(Topic topic) 215 { 216 if (!SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) { 217 return; 218 } 219 220 synchronized (FQN_FORUM) { 221 String forumId = Integer.toString(topic.getForumId()); 222 LinkedList list = (LinkedList )cache.get(FQN_FORUM, forumId); 223 224 if (list == null) { 225 list = new LinkedList (); 226 list.add(topic); 227 } 228 else { 229 boolean contains = list.contains(topic); 230 231 if (!contains && list.size() + 1 > maxItems) { 232 list.removeLast(); 233 } 234 else if (contains) { 235 list.remove(topic); 236 } 237 238 int index = 0; 239 240 for (Iterator iter = list.iterator(); iter.hasNext(); index++) { 241 Topic current = (Topic)iter.next(); 242 243 if (current.getType() == Topic.TYPE_ANNOUNCE) { 244 if (topic.getType() == Topic.TYPE_ANNOUNCE) { 245 list.add(index, topic); 246 break; 247 } 248 249 continue; 250 } 251 252 if (current.getType() == Topic.TYPE_STICKY) { 253 if (topic.getType() == Topic.TYPE_STICKY) { 254 list.add(index, topic); 255 break; 256 } 257 258 continue; 259 } 260 261 list.add(index, topic); 262 break; 263 } 264 } 265 266 cache.add(FQN_FORUM, forumId, list); 267 268 Map m = (Map )cache.get(FQN, RELATION); 269 270 if (m == null) { 271 m = new HashMap (); 272 } 273 274 m.put(new Integer (topic.getId()), new Integer (forumId)); 275 276 cache.add(FQN, RELATION, m); 277 } 278 } 279 280 285 public static void updateTopic(Topic topic) 286 { 287 synchronized (FQN_FORUM) { 288 String forumId = Integer.toString(topic.getForumId()); 289 List l = (List )cache.get(FQN_FORUM, forumId); 290 291 if (l != null) { 292 int index = l.indexOf(topic); 293 294 if (index > -1) { 295 l.set(index, topic); 296 cache.add(FQN_FORUM, forumId, l); 297 } 298 } 299 } 300 } 301 302 307 public static void remove(Topic topic) 308 { 309 synchronized (FQN_FORUM) { 310 if (topic.getForumId() == 0) { 311 throw new IllegalArgumentException ("Forum id cannot be empty"); 312 } 313 314 String forumId = Integer.toString(topic.getForumId()); 315 List l = (List )cache.get(FQN_FORUM, forumId); 316 317 if (l != null) { 318 l.remove(topic); 319 cache.add(FQN_FORUM, forumId, l); 320 321 Map m = (Map )cache.get(FQN, RELATION); 323 324 if (m != null) { 325 m.remove(new Integer (topic.getId())); 326 cache.add(FQN, RELATION, m); 327 } 328 } 329 } 330 } 331 332 339 public static Topic getTopic(Topic t) 340 { 341 if (t.getForumId() == 0) { 342 Map m = (Map )cache.get(FQN, RELATION); 343 344 if (m != null) { 345 Integer forumId = (Integer )m.get(new Integer (t.getId())); 346 347 if (forumId != null) { 348 t.setForumId(forumId.intValue()); 349 } 350 } 351 352 if (t.getForumId() == 0) { 353 return null; 354 } 355 } 356 357 List l = (List )cache.get(FQN_FORUM, Integer.toString(t.getForumId())); 358 359 int index = -1; 360 361 if (l != null) { 362 index = l.indexOf(t); 363 } 364 365 return (index == -1 ? null : (Topic)l.get(index)); 366 } 367 368 374 public static boolean isTopicCached(Topic topic) 375 { 376 return ((List )cache.get(FQN_FORUM, Integer.toString(topic.getForumId()))).contains(topic); 377 } 378 379 385 public static List getTopics(int forumid) 386 { 387 if (SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) { 388 synchronized (FQN_FORUM) { 389 List returnList = (List )cache.get(FQN_FORUM, Integer.toString(forumid)); 390 391 if (returnList == null) { 392 return new ArrayList (); 393 } 394 395 return new ArrayList (returnList); 396 } 397 } 398 399 return new ArrayList (); 400 } 401 } 402 | Popular Tags |