📄 msgcache.java~3~
字号:
package com.redmoon.forum;
import cn.js.fan.cache.jcs.RMCache;
import org.apache.log4j.Logger;
import cn.js.fan.db.Conn;
import java.util.Vector;
import java.sql.ResultSet;
import java.sql.SQLException;
import cn.js.fan.web.Global;
import cn.js.fan.db.SQLFilter;
import cn.js.fan.security.SecurityUtil;
/**
* <p>Title: 有关MsgDb中用到的缓存都在此类中管理</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class MsgCache {
String connname = "forum";
public static final int MSG_BLOCK_SIZE = 100;
public static final int THREAD_BLOCK_SIZE = 200;
private static final long[] EMPTY_BLOCK = new long[0];
RMCache rmCache = RMCache.getInstance();
String COUNT_GROUP_NAME = "SQL_COUNT_";
String MSGBLOCKCACHEPRIX = "MSGBLOCK_";
String THREADBLOCKCACHEPRIX = "THREADBLOCK_";
String cachePrix = "sq_msg_";
Logger logger = Logger.getLogger(MsgCache.class.getName());
public MsgCache() {
init();
}
public void init() {
connname = Global.defaultDB;
if (connname.equals(""))
logger.info("MsgCache:conname is empty.");
}
public MsgDb getMsgDb(long id) {
MsgDb msg = (MsgDb) rmCache.get(cachePrix + id);
// logger.info("getMsgDb msg=" + msg + " id=" + id);
if (msg == null) {
msg = new MsgDb(id);
// logger.info("getMsgDb: loaded=" + msg.isLoaded());
try {
if (msg.isLoaded())
rmCache.put(cachePrix + id, msg);
} catch (Exception e) {
logger.error("getMsgDb:" + e.getMessage());
}
}
else {
// renew;
}
return msg;
}
protected long[] getThreadsBlock(String query, String groupname, long startIndex) {
// First, discover what block number the results will be in.
long blockID = startIndex / THREAD_BLOCK_SIZE;
long blockStart = blockID * THREAD_BLOCK_SIZE;
// Now, check cache to see if the block is already cached. The key is
// simply the query plus the blockID.
String key = query + blockID;
long[] longArray = null;
try {
longArray = (long[]) rmCache.getFromGroup(key,
THREADBLOCKCACHEPRIX + groupname);
} catch (Exception e) {
logger.error(e.getMessage());
}
//If already in cache, return the block.
if (longArray != null) {
/**
* The actual block may be smaller than THREAD_BLOCK_SIZE. If that's
* the case, it means two things:
* 1) We're at the end boundary of all the results.
* 2) If the start index is greater than the length of the current
* block, than there aren't really any results to return.
*/
long[] docs = longArray;
//当startIndex过大时
if (startIndex >= blockStart + docs.length) {
// Return an empty array
return EMPTY_BLOCK;
} else {
return docs;
}
}
// Otherwise, we have to load up the block from the database.
else {
Vector DocBlock = new Vector();
Conn conn = new Conn(connname);
ResultSet rs = null;
try {
// Set the maxium number of rows to end at the end of this block.
conn.setMaxRows((int)(THREAD_BLOCK_SIZE * (blockID + 1)));
rs = conn.executeQuery(query);
// Grab THREAD_BLOCK_ROWS rows at a time.
conn.setFetchSize(THREAD_BLOCK_SIZE);
// Many JDBC drivers don't implement scrollable cursors the real
// way, but instead load all results into memory. Looping through
// the results ourselves is more efficient.
for (int i = 0; i < blockStart; i++) {
rs.next();
}
// Keep reading results until the result set is exaughsted or
// we come to the end of the block.
int count = 0;
while (rs.next() && count < THREAD_BLOCK_SIZE) {
DocBlock.addElement(new Long(rs.getLong(1)));
count++;
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {}
rs = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
int len = DocBlock.size();
long[] docs = new long[len];
for (int i = 0; i < len; i++) {
docs[i] = ((Long) DocBlock.elementAt(i)).longValue();
}
// Add the thread block to cache
try {
rmCache.putInGroup(key, THREADBLOCKCACHEPRIX + groupname, docs);
} catch (Exception e) {
logger.error(e.getMessage());
}
/**
* The actual block may be smaller than THREAD_BLOCK_SIZE. If that's
* the case, it means two things:
* 1) We're at the end boundary of all the results.
* 2) If the start index is greater than the length of the current
* block, than there aren't really any results to return.
*/
if (startIndex >= blockStart + docs.length) {
// Return an empty array
return EMPTY_BLOCK;
} else {
return docs;
}
}
}
protected long[] getMsgBlock(String query, String groupname, long startIndex) {
// First, discover what block number the results will be in.
long blockID = startIndex / MSG_BLOCK_SIZE;
long blockStart = blockID * MSG_BLOCK_SIZE;
// Now, check cache to see if the block is already cached. The key is
// simply the query plus the blockID.
String key = query + blockID;
long[] longArray = null;
try {
longArray = (long[]) rmCache.getFromGroup(key,
MSGBLOCKCACHEPRIX + groupname);
} catch (Exception e) {
logger.error(e.getMessage());
}
//If already in cache, return the block.
if (longArray != null) {
/**
* The actual block may be smaller than THREAD_BLOCK_SIZE. If that's
* the case, it means two things:
* 1) We're at the end boundary of all the results.
* 2) If the start index is greater than the length of the current
* block, than there aren't really any results to return.
*/
long[] docs = longArray;
//当startIndex过大时
if (startIndex >= blockStart + docs.length) {
// Return an empty array
return EMPTY_BLOCK;
} else {
return docs;
}
}
// Otherwise, we have to load up the block from the database.
else {
Vector DocBlock = new Vector();
Conn conn = new Conn(connname);
ResultSet rs = null;
try {
// Set the maxium number of rows to end at the end of this block.
conn.setMaxRows((int)(MSG_BLOCK_SIZE * (blockID + 1)));
rs = conn.executeQuery(query);
// Grab THREAD_BLOCK_ROWS rows at a time.
conn.setFetchSize(MSG_BLOCK_SIZE);
// Many JDBC drivers don't implement scrollable cursors the real
// way, but instead load all results into memory. Looping through
// the results ourselves is more efficient.
for (int i = 0; i < blockStart; i++) {
rs.next();
}
// Keep reading results until the result set is exaughsted or
// we come to the end of the block.
int count = 0;
while (rs.next() && count < MSG_BLOCK_SIZE) {
DocBlock.addElement(new Long(rs.getLong(1)));
count++;
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {}
rs = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
int len = DocBlock.size();
long[] docs = new long[len];
for (int i = 0; i < len; i++) {
docs[i] = ((Long) DocBlock.elementAt(i)).longValue();
}
// Add the thread block to cache
try {
rmCache.putInGroup(key, MSGBLOCKCACHEPRIX + groupname, docs);
} catch (Exception e) {
logger.error(e.getMessage());
}
/**
* The actual block may be smaller than THREAD_BLOCK_SIZE. If that's
* the case, it means two things:
* 1) We're at the end boundary of all the results.
* 2) If the start index is greater than the length of the current
* block, than there aren't really any results to return.
*/
if (startIndex >= blockStart + docs.length) {
// Return an empty array
return EMPTY_BLOCK;
} else {
return docs;
}
}
}
public int getThreadsCount(String sql, String boardcode) {
// 根据sql语句得出计算总数的sql查询语句
String query = SQLFilter.getCountSql(sql);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -