001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/MemberCache.java,v 1.13 2007/10/16 06:49:37 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.13 $
005: * $Date: 2007/10/16 06:49:37 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Minh Nguyen
039: * @author: Mai Nguyen
040: */
041: package com.mvnforum.db;
042:
043: import net.myvietnam.mvncore.exception.DatabaseException;
044: import net.myvietnam.mvncore.exception.ObjectNotFoundException;
045: import net.myvietnam.mvncore.util.DateUtil;
046:
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049:
050: import com.mvnforum.MVNForumConfig;
051: import com.whirlycott.cache.*;
052:
053: public class MemberCache {
054:
055: private static Log log = LogFactory.getLog(MemberCache.class);
056:
057: public static final long TIME_OUT = DateUtil.HOUR;
058:
059: // static singleton variable
060: static private MemberCache instance = new MemberCache();
061:
062: // instance variable
063: private Cache cache;
064:
065: public MemberCache() {
066: //Use the cache manager to create the default cache
067: try {
068: if (MVNForumConfig.getEnableCacheMember()) {
069: cache = CacheManager.getInstance().getCache("member");
070: }
071: } catch (CacheException ex) {
072: log
073: .error(
074: "Cannot get the WhirlyCache. Member caching is disabled.",
075: ex);
076: } catch (LinkageError e) {
077: // @todo: Should be never throw
078: log
079: .error(
080: "Cannot get the WhirlyCache caused by Package Conflict. Member caching is disabled.",
081: e);
082: }
083: }
084:
085: /**
086: * Returns the single instance
087: * @return MemberCache : the singleton instance.
088: *
089: * NOTE: if use normal singleton pattern, this method should be synchronized
090: */
091: static public MemberCache getInstance() {
092: return instance;
093: }
094:
095: public String getEfficiencyReport() {
096: String result = "No report";
097: if (cache == null) {
098: if (MVNForumConfig.getEnableCacheMember() == false) {
099: result = "Cache is disabled.";
100: } else {
101: result = "Cache cannot be inited";
102: }
103: } else if (cache instanceof CacheDecorator) {
104: result = ((CacheDecorator) cache).getEfficiencyReport();
105: }
106: return result;
107: }
108:
109: public void clear() {
110: if (cache != null) {
111: cache.clear();
112: }
113: }
114:
115: public MemberBean getMember(int memberID) throws DatabaseException,
116: ObjectNotFoundException {
117:
118: MemberBean memberBean = null;
119: if (cache != null) {
120: String key = new String("getMember" + memberID);
121: memberBean = (MemberBean) cache.retrieve(key);
122: if (memberBean == null) {
123: //log.debug("MemberCache: about to call getMember with id = " + memberID);
124: memberBean = DAOFactory.getMemberDAO().getMember(
125: memberID);
126: cache.store(key, memberBean, TIME_OUT);
127: }
128: } else {
129: memberBean = DAOFactory.getMemberDAO().getMember(memberID);
130: }
131:
132: if (memberBean == null) {
133: throw new ObjectNotFoundException(
134: "Cannot find the row in table Member "
135: + "where primary key = (" + memberID + ").");
136: }
137: return memberBean;
138: }
139:
140: public int getMemberIDFromMemberName(String memberName)
141: throws DatabaseException, ObjectNotFoundException {
142:
143: int memberID = -1;
144: if (cache != null) {
145: String key = new String("getMemberIDFromMemberName_"
146: + memberName);
147: Integer memberIDInterger = (Integer) cache.retrieve(key);
148: if (memberIDInterger == null) {
149: //log.debug("MemberCache: about to call getMemberIDFromMemberName with id = " + memberID);
150: memberID = DAOFactory.getMemberDAO()
151: .getMemberIDFromMemberName(memberName);
152: cache.store(key, new Integer(memberID), TIME_OUT);
153: } else {
154: memberID = memberIDInterger.intValue();
155: }
156: } else {
157: memberID = DAOFactory.getMemberDAO()
158: .getMemberIDFromMemberName(memberName);
159: }
160:
161: if (memberID == -1) {
162: throw new ObjectNotFoundException(
163: "Cannot find the row in table Member "
164: + "where alternate key = (" + memberName
165: + ").");
166: }
167: return memberID;
168: }
169:
170: }
|