subjectlistcache.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 175 行

JAVA
175
字号
package cn.js.fan.module.cms;import com.cloudwebsoft.framework.base.*;import cn.js.fan.db.Conn;import cn.js.fan.db.SQLFilter;import java.util.Vector;import java.sql.ResultSet;import java.sql.SQLException;import cn.js.fan.security.SecurityUtil;import com.cloudwebsoft.framework.db.Connection;public class SubjectListCache extends ObjectCache {    public static final int DOC_BLOCK_SIZE = 100;    private static final long[] EMPTY_BLOCK = new long[0];    String DOCBLOCKCACHEPRIX = "SUBJECT_BLOCK_";    public SubjectListCache(SubjectListDb sld) {        super(sld);    }        public void refreshList(String subjectCode) {                try {            rmCache.invalidateGroup(DOCBLOCKCACHEPRIX + subjectCode);            rmCache.invalidateGroup(COUNT_GROUP_NAME);        } catch (Exception e) {            logger.error(e.getMessage());        }    }    protected long[] getDocBlock(String query, String groupKey, int startIndex) {                int blockID = startIndex / DOC_BLOCK_SIZE;        int blockStart = blockID * DOC_BLOCK_SIZE;                        String key = query + blockID;        long[] longArray = null;        try {                        longArray = (long[]) rmCache.getFromGroup(key,                    DOCBLOCKCACHEPRIX + groupKey);        } catch (Exception e) {            logger.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();            Connection conn = new Connection(connname);            ResultSet rs = null;            try {                                conn.setMaxRows(DOC_BLOCK_SIZE * (blockID + 1));                rs = conn.executeQuery(query);                                                conn.setFetchSize(DOC_BLOCK_SIZE);                                                                for (int i = 0; i < blockStart; i++) {                    rs.next();                }                                                int count = 0;                while (rs.next() && count < DOC_BLOCK_SIZE) {                    DocBlock.addElement(new Long(rs.getLong(1)));                    count++;                }            } catch (SQLException sqle) {                logger.error("getDocBlock: " + sqle.getMessage());                            } 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, DOCBLOCKCACHEPRIX + groupKey, docs);            } catch (Exception e) {                logger.error("getDocBlock1:" + e.getMessage());            }                        if (startIndex >= blockStart + docs.length) {                                return EMPTY_BLOCK;            } else {                return docs;            }        }    }        public int getDocCount(String sql) {                String query = SQLFilter.getCountSql(sql);        if (!SecurityUtil.isValidSql(query))            return -1;        Integer count = null;        try {            count = (Integer) rmCache.getFromGroup(query, COUNT_GROUP_NAME);        } catch (Exception e) {            logger.error(e.getMessage());        }                if (count != null) {            return count.intValue();        }                else {            int docCount = 0;            Conn conn = new Conn(connname);            ResultSet rs = null;            try {                rs = conn.executeQuery(query);                if (rs.next())                    docCount = rs.getInt(1);            } catch (SQLException sqle) {                sqle.printStackTrace();            } finally {                if (rs != null) {                    try {                        rs.close();                    } catch (Exception e) {                        e.printStackTrace();                    }                    rs = null;                }                if (conn != null) {                    conn.close();                    conn = null;                }            }                        try {                rmCache.putInGroup(query, COUNT_GROUP_NAME,                                   new Integer(docCount));            } catch (Exception e) {                logger.error(e.getMessage());            }            return docCount;        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?