1 43 package net.jforum.repository; 44 45 import java.util.ArrayList ; 46 import java.util.Collection ; 47 import java.util.Iterator ; 48 import java.util.LinkedHashMap ; 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.PostDAO; 56 import net.jforum.entities.Post; 57 import net.jforum.util.preferences.ConfigKeys; 58 import net.jforum.util.preferences.SystemGlobals; 59 import net.jforum.view.forum.common.PostCommon; 60 61 68 public class PostRepository implements Cacheable 69 { 70 private static final int CACHE_SIZE = SystemGlobals.getIntValue(ConfigKeys.POSTS_CACHE_SIZE); 71 private static final String FQN = "posts"; 72 private static CacheEngine cache; 73 74 77 public void setCacheEngine(CacheEngine engine) 78 { 79 cache = engine; 80 } 81 82 public static int size() 83 { 84 Map m = (Map )cache.get(FQN); 85 return (m != null ? m.size() : 0); 86 } 87 88 public static int size(int topicId) 89 { 90 List posts = (List )cache.get(FQN, Integer.toString(topicId)); 91 return (posts == null ? 0 : posts.size()); 92 } 93 94 public static Collection cachedTopics() 95 { 96 Map m = (Map )cache.get(FQN); 97 if (m == null) { 98 return new ArrayList (); 99 } 100 101 return m.keySet(); 102 } 103 104 public static List selectAllByTopicByLimit(int topicId, int start, int count) throws Exception 105 { 106 String tid = Integer.toString(topicId); 107 108 List posts = (List )cache.get(FQN, tid); 109 if (posts == null || posts.size() == 0) { 110 PostDAO pm = DataAccessDriver.getInstance().newPostDAO(); 111 posts = pm.selectAllByTopic(topicId); 112 113 for (Iterator iter = posts.iterator(); iter.hasNext(); ) { 114 PostCommon.preparePostForDisplay((Post)iter.next()); 115 } 116 117 Map topics = (Map )cache.get(FQN); 118 if (topics == null || topics.size() == 0 || topics.size() < CACHE_SIZE) { 119 cache.add(FQN, tid, posts); 120 } 121 else { 122 if (!(topics instanceof LinkedHashMap )) { 123 topics = new LinkedHashMap (topics) { 124 protected boolean removeEldestEntry(java.util.Map.Entry eldest) { 125 return this.size() > CACHE_SIZE; 126 } 127 }; 128 } 129 130 topics.put(tid, posts); 131 cache.add(FQN, topics); 132 } 133 } 134 135 int size = posts.size(); 136 return posts.subList(start, (size < start + count) ? size : start + count); 137 } 138 139 public static void remove(int topicId, int postId) 140 { 141 synchronized (FQN) { 142 String tid = Integer.toString(topicId); 143 144 List posts = (List )cache.get(FQN, tid); 145 146 if (posts != null) { 147 Post p = new Post(); 148 p.setId(postId); 149 posts.remove(p); 150 151 cache.add(FQN, tid, posts); 152 } 153 } 154 } 155 156 public static void update(int topicId, Post p) 157 { 158 String tid = Integer.toString(topicId); 159 List posts = (List )cache.get(FQN, tid); 160 if (posts != null && posts.contains(p)) { 161 posts.set(posts.indexOf(p), p); 162 cache.add(FQN, tid, posts); 163 } 164 } 165 166 public static void append(int topicId, Post p) 167 { 168 String tid = Integer.toString(topicId); 169 List posts = (List )cache.get(FQN, tid); 170 if (posts != null && !posts.contains(p)) { 171 posts.add(p); 172 cache.add(FQN, tid, posts); 173 } 174 } 175 176 public static void clearCache(int topicId) 177 { 178 cache.remove(FQN, Integer.toString(topicId)); 179 } 180 } 181 182 | Popular Tags |