querycacheupdatetask.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 135 行

JAVA
135
字号
/** * $RCSfile: QueryCacheUpdateTask.java,v $ * $Revision: 1.2 $ * $Date: 2002/07/12 00:35:59 $ * * Copyright 1999-2002 CoolServlets, Inc. All Rights Reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.jivesoftware.forum.database;import com.jivesoftware.forum.*;import com.jivesoftware.util.*;import java.sql.*;/** * A task to reload a cache entry from the database and then move it from the * short-term query cache and into the long-term cache. */public class QueryCacheUpdateTask implements Runnable {    private QueryCacheKey key;    /**     * Creates a new cache entry update task.     *     * @param key the key for the cache entry to be updated.     */    public QueryCacheUpdateTask(QueryCacheKey key) {        this.key = key;    }    public void run() {        DbForumFactory factory = DbForumFactory.getInstance();        try {            int blockID = key.getBlockID();            // Determine the block size.            int blockSize = 0;            switch (key.getObjectType()) {                case JiveGlobals.FORUM_CATEGORY:                    blockSize = DbForumCategory.BLOCK_SIZE;                    break;                case JiveGlobals.FORUM:                    blockSize = DbForum.BLOCK_SIZE;                    break;                case JiveGlobals.THREAD:                    blockSize = DbForum.BLOCK_SIZE;            }            Object value = null;            Connection con = null;            Statement stmt = null;            try {                con = ConnectionManager.getConnection();                // If this is not a count query, load a block of results from the db.                if (key.blockID != -1) {                    stmt = ConnectionManager.createScrollableStatement(con);                    LongList objectList = new LongList(blockSize);                    // Set the maximum number of rows to end at the end of this block.                    ConnectionManager.setMaxRows(stmt, blockSize * (blockID+1));                    ResultSet rs = stmt.executeQuery(key.getSql());                    // Grab BLOCK_SIZE rows at a time.                    ConnectionManager.setFetchSize(rs, blockSize);                    // Position the cursor right before the first row that we're insterested in.                    int blockStart = blockSize * blockID;                    ConnectionManager.scrollResultSet(rs, blockStart);                    // Keep reading results until the result set is exhausted or                    // we come to the end of the block.                    int count = 0;                    while (rs.next() && count < blockSize) {                        objectList.add(rs.getLong(1));                        count++;                    }                    value = objectList.toArray();                }                // Otherwise, this is a count query.                else {                    stmt = con.createStatement();                    ResultSet rs = stmt.executeQuery(key.getSql());                    rs.next();                    value = new Integer(rs.getInt(1));                }            }            catch( SQLException sqle ) {                sqle.printStackTrace();            }            finally {                try {  stmt.close(); }                catch (Exception e) { e.printStackTrace(); }                try {  con.close();   }                catch (Exception e) { e.printStackTrace(); }            }            if (value != null) {                // Add the value to long-term cache.                factory.cacheManager.queryCache.put(key, value);                // Add the query key to the object.                switch (key.getObjectType()) {                    case JiveGlobals.FORUM_CATEGORY:                        DbForumCategory category = null;                        try {                            category = factory.cacheManager.getForumCategory(key.getObjectID());                            category.addQueryKey(key);                        }                        catch (Exception e) { }                        break;                    case JiveGlobals.FORUM:                         DbForum forum = null;                         try {                             forum = factory.cacheManager.getForum(key.getObjectID());                             forum.addQueryKey(key);                         }                         catch (Exception e) { }                         break;                    case JiveGlobals.THREAD:                        DbForumThread thread = null;                        try {                            thread = factory.cacheManager.getForumThread(key.getObjectID());                            thread.addQueryKey(key);                        }                        catch (Exception e) { }                }            }        }        finally {            // Always remove the cache entry from short-term cache.            factory.cacheManager.shortTermQueryCache.remove(key);        }    }}

⌨️ 快捷键说明

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