1 41 package com.mvnforum.db; 42 43 import net.myvietnam.mvncore.exception.DatabaseException; 44 import net.myvietnam.mvncore.exception.ObjectNotFoundException; 45 import net.myvietnam.mvncore.util.DateUtil; 46 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 50 import com.mvnforum.MVNForumConfig; 51 import com.whirlycott.cache.*; 52 53 public class MemberCache { 54 55 public static final long TIME_OUT = DateUtil.HOUR; 56 57 private static Log log = LogFactory.getLog(MemberCache.class); 58 59 static private MemberCache instance = new MemberCache(); 61 62 private Cache cache; 64 65 public MemberCache() { 66 try { 68 if (MVNForumConfig.getEnableCacheMember()) { 69 cache = CacheManager.getInstance().getCache("member"); 70 } 71 } catch (CacheException ex) { 72 log.error("Cannot get the WhirlyCache. Member caching is disabled.", ex); 73 } catch (LinkageError e) { 74 log.error("Cannot get the WhirlyCache caused by Package Conflict. Member caching is disabled.", e); 76 } 77 } 78 79 85 static public MemberCache getInstance() { 86 return instance; 87 } 88 89 public String getEfficiencyReport() { 90 String result = "No report"; 91 if (cache == null) { 92 if (MVNForumConfig.getEnableCacheMember() == 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 MemberBean getMember_forPublic(int memberID) 110 throws DatabaseException, ObjectNotFoundException { 111 112 MemberBean memberBean = null; 113 if (cache != null) { 114 String key = new String ("getMember_forPublic" + memberID); 115 memberBean = (MemberBean)cache.retrieve(key); 116 if (memberBean == null) { 117 memberBean = DAOFactory.getMemberDAO().getMember_forPublic(memberID); 119 cache.store(key, memberBean, TIME_OUT); 120 } 121 } else { 122 memberBean = DAOFactory.getMemberDAO().getMember_forPublic(memberID); 123 } 124 125 if (memberBean == null) { 126 throw new ObjectNotFoundException("Cannot find the row in table Member " 127 + "where primary key = (" + memberID + ")."); 128 } 129 return memberBean; 130 } 131 132 } 133 | Popular Tags |