⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cachemanager.java

📁 java版源代码,里面包含很多源代码,大家可以看看.
💻 JAVA
字号:
package com.trulytech.mantis.system;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.HashMap;
import com.trulytech.mantis.result.DBResult;
import java.util.Enumeration;

/**
 *
 * <p>Title: Mantis</p>
 *
 * <p>Description: Cache管理</p>
 *
 * <p>Copyright: Copyright (c) 2002</p>
 *
 * <p>Company: </p>
 *
 * @author Wang Xian
 * @version 1.0
 */
public class CacheManager {

  //是否初始化
  public static boolean isInited = false;

  /**
   * 设置Cache到application中的CacheTable对象中
   * @throws Exception
   */
  public static void init() throws Exception {
    if (isInited)
      return;
    Statement stmt = null;
    Connection conn = null;
    logWriter.Info("读取缓存表...");
    try {
      conn = ConnManager.getConnection();
      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                                  ResultSet.CONCUR_READ_ONLY);
      stmt.setFetchSize(Properties.Fetch_Size);
      stmt.setMaxRows(Properties.MaxRows);
      stmt.setQueryTimeout(Properties.QueryTimeout);

      SQLParser Parser = new SQLParser(stmt, null);

      for (Enumeration enumeration = com.trulytech.mantis.system.Properties.
           Cachekeys.keys();
           enumeration.hasMoreElements(); ) {
        String key = (String) enumeration.nextElement();

        DBResult Result = Parser.QueryExecute(com.trulytech.mantis.system.
                                              Properties.Cachekeys.getProperty(
                                                  key));
        logWriter.Debug("读取" + key + "对象成功 (共" + Result.ResultBuffer.size() +
                        "条记录)");
        com.trulytech.mantis.system.Properties.cacheTable.put(key.toLowerCase(),
            Result);

      }
      Parser = null;
      logWriter.Info("读取缓存表成功");
      isInited = true;
    }
    catch (Exception e) {
      logWriter.Error("读取缓存表失败 " + e.toString());

    }
    finally {
      try {
        if (stmt != null) {
          stmt.close();
          stmt = null;
        }
      }
      catch (Exception ex) {
        logWriter.Error("读取缓存表失败 " + ex.toString());
      }
      try {
        ConnManager.closeConnection(conn);
      }
      catch (Exception ex) {
        logWriter.Error("读取缓存表失败 " + ex.toString());
      }

    }

  }

  /**
   * 根据对象名获得对象
   * @param Key String 对象名
   * @return DBResult
   */
  public static DBResult getCache(String Key) {

    logWriter.Debug("获得缓存表" + Key);

    if (isInited) {
      HashMap map = com.trulytech.mantis.system.Properties.cacheTable;
      if (map == null)
        return new DBResult();
      else {
        DBResult Result = (DBResult) map.get(Key.toLowerCase());
        if (Result == null)
          return new DBResult();
        else
          return Result;
      }
    }
    else
      return new DBResult();
  }

  /**
   * 刷新缓存表
   * @param Key String 对象名
   * @throws Exception
   */
  public synchronized static void refresh(String Key) throws Exception {
    if (Key == null)
      return;
    else if (com.trulytech.mantis.system.Properties.Cachekeys == null)
      return;
    else if (com.trulytech.mantis.system.Properties.Cachekeys.get(Key) == null)
      return;
    //如果数据合法
    Statement stmt = null;
    Connection conn = null;
    logWriter.Info("刷新缓存表..." + Key);
    try {
      conn = ConnManager.getConnection();

      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                                  ResultSet.CONCUR_READ_ONLY);
      stmt.setFetchSize(Properties.Fetch_Size);
      stmt.setMaxRows(Properties.MaxRows);
      stmt.setQueryTimeout(Properties.QueryTimeout);

      SQLParser Parser = new SQLParser(stmt, null);
      DBResult Result = Parser.QueryExecute(com.trulytech.mantis.system.
                                            Properties.Cachekeys.getProperty(
                                                Key));

      com.trulytech.mantis.system.Properties.cacheTable.put(Key.toLowerCase(),
          Result);

      logWriter.Info("刷新缓存表成功(" + Key + ", 共" + Result.ResultBuffer.size() +
                     "条记录) ");
    }
    catch (Exception e) {
      logWriter.Error("刷新缓存表失败 (" + Key + ") " + e.toString());

    }
    finally {
      try {
        if (stmt != null) {
          stmt.close();
          stmt = null;
        }
      }
      catch (Exception ex) {
        logWriter.Error("刷新缓存表失败 (" + Key + ") " + ex.toString());
      }
      try {
        ConnManager.closeConnection(conn);
      }
      catch (Exception ex) {
        logWriter.Error("刷新缓存表失败 (" + Key + ") " + ex.toString());
      }

    }

  }

  /**
   * 刷新所有缓存表
   * @throws Exception
   */
  public synchronized static void refreshAll() throws Exception {
    if (com.trulytech.mantis.system.Properties.Cachekeys == null)
      return;
    //如果数据合法
    Statement stmt = null;
    Connection conn = null;
    logWriter.Info("刷新所有缓存表...");
    try {
      conn = ConnManager.getConnection();

      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                                  ResultSet.CONCUR_READ_ONLY);
      stmt.setFetchSize(Properties.Fetch_Size);
      stmt.setMaxRows(Properties.MaxRows);
      stmt.setQueryTimeout(Properties.QueryTimeout);

      SQLParser Parser = new SQLParser(stmt, null);
      for (Enumeration enumeration = com.trulytech.mantis.system.Properties.
           Cachekeys.keys();
           enumeration.hasMoreElements(); ) {
        String key = (String) enumeration.nextElement();

        DBResult Result = Parser.QueryExecute(com.trulytech.mantis.system.
                                              Properties.Cachekeys.getProperty(
                                                  key));

        com.trulytech.mantis.system.Properties.cacheTable.put(key.toLowerCase(),
            Result);
        logWriter.Debug("读取" + key + "对象成功 (共" + Result.ResultBuffer.size() +
                        "条记录)");
      }
      Parser = null;
      logWriter.Info("刷新所有缓存表成功");
    }
    catch (Exception e) {
      logWriter.Error("刷新所有缓存表失败" + e.toString());

    }
    finally {
      try {
        if (stmt != null) {
          stmt.close();
          stmt = null;
        }
      }
      catch (Exception ex) {
        logWriter.Error("刷新所有缓存表失败" + ex.toString());
      }
      try {
        ConnManager.closeConnection(conn);
      }
      catch (Exception ex) {
        logWriter.Error("刷新所有缓存表失败" + ex.toString());
      }

    }

  }

}

⌨️ 快捷键说明

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