📄 sqlcache.java
字号:
/* * PoolMan Java Object Pooling and Caching Library * Copyright (C) 1999-2001 The Code Studio * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * The full license is located at the root of this distribution * in the LICENSE file. * */package com.codestudio.util;import com.codestudio.PoolManConstants;import com.codestudio.sql.PoolManResultSet;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Enumeration;import java.util.Hashtable;public class SQLCache implements Runnable { private Hashtable cache; private Hashtable cacheArgs; private JDBCPool mypool; protected long sleeptime; protected int max_size; protected Thread cachechecker; public SQLCache(JDBCPool pool) { this(pool, PoolManConstants.DEFAULT_CACHE_SIZE, PoolManConstants.DEFAULT_CACHE_REFRESH); } public SQLCache(JDBCPool pool, int size, int sleeptimeSecs) { this.cache = new Hashtable(1); this.cacheArgs = new Hashtable(1); this.mypool = pool; this.max_size = size; this.sleeptime = (sleeptimeSecs * 1000); this.cachechecker = new Thread(this); this.cachechecker.setDaemon(true); cachechecker.start(); } /** Return the current cache as a Hashtable. */ public synchronized Hashtable getRawCache() { return this.cache; } /** Retrieve the JDBCPool to which this SQLCache corresponds. */ public JDBCPool getPool() { return this.mypool; } /** Establish the JDBCPool to which this SQLCache corresponds. */ public void setPool(JDBCPool pool) { this.mypool = pool; } /** Get a cached SQL Result. */ public synchronized ResultSet getResult(String sql) { sql = sql.toLowerCase(); if (this.cache.containsKey(sql)) { try { this.mypool.debug("Returning a cached ResultSet for query: " + sql); return ((PoolManResultSet) cache.get(sql)).cloneSet(); } catch (Exception e) { this.mypool.log("Exception while attempting to return a cached " + "ResultSet for query: " + sql + " [query removed from cache]", e); removeResult(sql); } } return null; } /** * Remove a cached SQLResult. Doesn't need to be synchronized because it is * always invoked from within a synchronized block. * @return boolean Whether or not the operation succeeded. */ public boolean removeResult(String sql) { try { this.cache.remove(sql.toLowerCase()); this.cacheArgs.remove(sql.toLowerCase()); return true; } catch (Exception e) { } return false; } /** Cache a result. */ public synchronized boolean cacheResult(String origsql, ResultSet results) { String sql = origsql.toLowerCase(); if ((results instanceof PoolManResultSet) && (sql.startsWith("select"))) { if (cache.size() < this.max_size) { try { cache.put(sql, ((PoolManResultSet) results).cloneSet()); cacheArgs.put(sql, origsql); mypool.debug("Cached ResultSet for query: " + sql); return true; } catch (ClassCastException ce) { mypool.log("Only PoolMan ResultSets (no native sets) can be cached: " + ce.getMessage()); } } } return false; } /** Return the size of the cache as an integer. */ public int size() { return this.cache.size(); } /** Return the maximum possible size of the cache. */ public int getMaxSize() { return this.max_size; } /** Refresh the cache explicitly. */ public synchronized void forceRefresh() { for (Enumeration enum = cache.keys(); enum.hasMoreElements();) { String sql = (String) enum.nextElement(); String origsql = (String) cacheArgs.get(sql); try { // remove result to ensure the statement doesn't merely return the same result removeResult(sql); // execute sql again SQLUtil.getInstance().doJDBC(mypool.getPoolname(), origsql, false); mypool.debug("Refreshed cache for pool " + mypool.getPoolname() + ", query: " + origsql); } catch (SQLException sqle) { mypool.log("Exception while refreshing cache for pool " + mypool.getPoolname(), sqle); removeResult(sql); } } } public void run() { for (; ;) { try { this.cachechecker.sleep(this.sleeptime); forceRefresh(); } catch (InterruptedException ie) { mypool.log("ERROR: Cache Checker Died:"); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -