📄 msgcache.java
字号:
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;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_"; public MsgCache() { init(); } public void init() { connname = Global.defaultDB; if (connname.equals("")) Logger.getLogger(getClass()).info("MsgCache:conname is empty."); } public MsgDb getMsgDb(long id) { MsgDb msg = (MsgDb) rmCache.get(cachePrix + id); if (msg == null) { msg = new MsgDb(id); try { if (msg.isLoaded()) rmCache.put(cachePrix + id, msg); } catch (Exception e) { Logger.getLogger(getClass()).error("getMsgDb:" + e.getMessage()); } } else { } return msg; } protected long[] getThreadsBlock(String query, String groupname, long startIndex) { long blockID = startIndex / THREAD_BLOCK_SIZE; long blockStart = blockID * THREAD_BLOCK_SIZE; String key = query + blockID; long[] longArray = null; try { longArray = (long[]) rmCache.getFromGroup(key, THREADBLOCKCACHEPRIX + groupname); } catch (Exception e) { Logger.getLogger(getClass()).error(e.getMessage()); } if (longArray != null) { long[] docs = longArray; if (startIndex >= blockStart + docs.length) { return EMPTY_BLOCK; } else { return docs; } } else { Vector DocBlock = new Vector(); Conn conn = new Conn(connname); ResultSet rs = null; try { conn.setMaxRows((int)(THREAD_BLOCK_SIZE * (blockID + 1))); rs = conn.executeQuery(query); conn.setFetchSize(THREAD_BLOCK_SIZE); for (int i = 0; i < blockStart; i++) { rs.next(); } 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(); } try { rmCache.putInGroup(key, THREADBLOCKCACHEPRIX + groupname, docs); } catch (Exception e) { Logger.getLogger(getClass()).error(e.getMessage()); } if (startIndex >= blockStart + docs.length) { return EMPTY_BLOCK; } else { return docs; } } } protected long[] getMsgBlock(String query, String groupname, long startIndex) { long blockID = startIndex / MSG_BLOCK_SIZE; long blockStart = blockID * MSG_BLOCK_SIZE; String key = query + blockID; long[] longArray = null; try { longArray = (long[]) rmCache.getFromGroup(key, MSGBLOCKCACHEPRIX + groupname); } catch (Exception e) { Logger.getLogger(getClass()).error("getMsgBlock:" + e.getMessage()); } if (longArray != null) { long[] docs = longArray; if (startIndex >= blockStart + docs.length) { return EMPTY_BLOCK; } else { return docs; } } else { Vector DocBlock = new Vector(); Conn conn = new Conn(connname); ResultSet rs = null; try { conn.setMaxRows((int)(MSG_BLOCK_SIZE * (blockID + 1))); rs = conn.executeQuery(query); conn.setFetchSize(MSG_BLOCK_SIZE); for (int i = 0; i < blockStart; i++) { rs.next(); } 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(); } try { rmCache.putInGroup(key, MSGBLOCKCACHEPRIX + groupname, docs); } catch (Exception e) { Logger.getLogger(getClass()).error(e.getMessage()); } if (startIndex >= blockStart + docs.length) { return EMPTY_BLOCK; } else { return docs; } } } public int getThreadsCount(String sql, String boardcode) { String query = SQLFilter.getCountSql(sql); if (!SecurityUtil.isValidSql(query)) return -1; Integer count = null; try { count = (Integer) rmCache.getFromGroup(query, COUNT_GROUP_NAME + boardcode); } catch (Exception e) { Logger.getLogger(getClass()).error(e.getMessage()); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -