1) 概述:

听说公司有tc cache,可是没见着它在那里被应用了。而且他平时同事的编码中,每次要用到字典表的数据,总是QueryManager查询数据库。

昨天花了几个小时写了个简单的TCHelper,用户缓存TC表。

一般的情况下,我们字典表是不会发生变化的,有必要去cache我的字典表。

我的大体思路如下:
一:通过xml配置文件,配置所有的字典表查询sql.这样我们的sql和代码可以不在相干了。
这其中的sql有两种可能:
  1:没有参数,对于这种sql,在初始化的时候就将得到数据,并且缓存起来。
  2:带有参数的,这类sql我们没有办法再初始化的时间就执行,我们在xml中添加了一个简单的attribute

cache字典表
            
    
    
        CacheSQLXMLApachelog4j init="false"

二 :重新加载功能,这有两种可能:
  1:字典表数据放生改变
  2:配置的xml文件放生了变化。

对于字典表数据放生改变的情况,目前这个东西只是简单实现,并没有去检测数据库的数据,而是需要用户主动的方法

cache字典表
            
    
    
        CacheSQLXMLApachelog4j TCHelper.touch();

去修改配置文件最后更新时间。

我们只检测文件是否放生变化,如果放生变化才会去重新加载数据。

2) 代码

cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j /**//*
cache字典表
            
    
    
        CacheSQLXMLApachelog4j  * Copyright (c) 2005 Print Information System Co.,Ltd. All Rights Reserved.
cache字典表
            
    
    
        CacheSQLXMLApachelog4j  
*/

cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
package com.jxlt.adt.util;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
import com.ptf.datastore.QueryManager;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
import com.ptf.util.ClassLoaderUtil;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
import org.apache.commons.lang.builder.ToStringBuilder;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
import org.apache.commons.lang.builder.ToStringStyle;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
import org.apache.log4j.Logger;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
import org.jdom.Document;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
import org.jdom.Element;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
import org.jdom.JDOMException;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
import org.jdom.input.SAXBuilder;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
import java.io.File;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
import java.sql.SQLException;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
import java.util.*;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
/** *//**
cache字典表
            
    
    
        CacheSQLXMLApachelog4j  * 字典表cache
cache字典表
            
    
    
        CacheSQLXMLApachelog4j  *
cache字典表
            
    
    
        CacheSQLXMLApachelog4j  * 
@author <a href="martin.xus@gmail.com">martin xus
cache字典表
            
    
    
        CacheSQLXMLApachelog4j  * 
@version 1.0 ,2005-10-26 11:29:33
cache字典表
            
    
    
        CacheSQLXMLApachelog4j  
*/

cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
public class TCHelper cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
///---------------------------------------------------------------
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
    /// Instancd Data
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
    ///---------------------------------------------------------------
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
    private static final Logger logger = Logger.getLogger(TCHelper.class);
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
private static Map sqlMap = new HashMap();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
private static Map cache = new HashMap();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
private static long lastModefied;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
public static final String CONFIG_FILE = "tc_adt_sql.xml";
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
//---------------------------------------------------------------
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
//  static block
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
//---------------------------------------------------------------
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
    static cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         logger.info(
"初始化TC Cachecache字典表
            
    
    
        CacheSQLXMLApachelog4j ..");
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         init();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         logger.info(
"初始化TC Cache完成");
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     }

cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
//---------------------------------------------------------------
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
//  public method
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
//---------------------------------------------------------------
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 

cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
/** *//**
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 根据指定的id返回缓存中的字典表数据
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 首先检测是否需要重新加载,若需要,则先加载
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      *
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 
@param key 配置在xml中的id
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 
@return key对应的字典表数据,
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      *         若没有对应的key,则返回emptyList
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      
*/

cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
public static List get(String key) cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         logger.info(
"get tc value with key:" + key);
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         
if (StringUtils.isBlank(key))
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             
return null;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j         
if (reload()) cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             logger.info(
"reloadingcache字典表
            
    
    
        CacheSQLXMLApachelog4j ");
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             init();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             logger.info(
"reloaded");
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         }

cache字典表
            
    
    
        CacheSQLXMLApachelog4j         String _key 
= key.toLowerCase();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         
if (cache.containsKey(_key))
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             
return getValue(key);
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         
else
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             
return emptyList();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     }

cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
/** *//**
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 这只针对于在初始化(init)没有初始化的字典表
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      *
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 
@param key    xml配置文件中对应的id
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 
@param params sql参数
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 
@return key对应的字典表数据,
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      *         若没有对应的key,则返回emptyList
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      
*/

cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
public static List get(String key, List params) cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         logger.info(
"PageHelper.getTCValue: key=" + key + " params=" + params);
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         
if (StringUtils.isBlank(key))
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             
return emptyList();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         
if (null == params)
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             
throw new UnsupportedOperationException("不支持params为空的查询!");
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         String _key 
= key.toLowerCase();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j         
if (sqlMap.containsKey(_key)) cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             TCModel model 
= (TCModel) sqlMap.get(_key);
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             
//logger.info("model:" + model);
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
            try cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j                 
//todo:是否cache该变量
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
//                cache.put(_key, _list);
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
                return QueryManager.excuteSql(model.getSql(), params);
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j             }
 catch (SQLException e) cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j                 
return emptyList();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             }

cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j         }
 else cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             logger.debug(
"invalid key!");
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         }

cache字典表
            
    
    
        CacheSQLXMLApachelog4j         
return emptyList();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     }

cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
/** *//**
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 修改文件的最后修改时间
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 这样当用户在查询字典表数据的时候,会重新init加载字典表数据
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 只有在字典表数据发生修改的时候才需要调用该方法。
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      
*/

cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
public static void touch() cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         File file 
= getFile();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         file.setLastModified(System.currentTimeMillis());
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     }

cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
/** *//**
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 清除所有的cache,包括 cache 和 sqlMap
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      
*/

cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
public static void clearAll() cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         cache.clear();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         sqlMap.clear();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     }

cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
/** *//**
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 清除指定key对应的字典表数据
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      *
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 
@param key 配置在xml文件中的id名称
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      
*/

cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
public static void clear(String key) cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         
if (StringUtils.isBlank(key))
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             
return;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         String _key 
= key.toLowerCase();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         
if (cache.containsKey(_key))
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             cache.remove(_key);
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     }

cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
//---------------------------------------------------------------
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
//  private method
cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
//---------------------------------------------------------------
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 

cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
/** *//**
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 读取xml文件,初始化tc cache
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      
*/

cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
private static void init() cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         logger.info(
"TCHelper.init() begin");
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         logger.info(
"Reading config from " + CONFIG_FILE);
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         File file 
= getFile();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         lastModefied 
= file.lastModified();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         logger.debug(
"file loaded.");
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         Element element 
= getRootElement(file);
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         Iterator iterator 
= element.getChildren().iterator();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j         
while (iterator.hasNext()) cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             TCModel model 
= new TCModel();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             Element e 
= (Element) iterator.next();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             String id 
= e.getAttributeValue("id");
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             
if (StringUtils.isBlank(id))
cache字典表
            
    
    
        CacheSQLXMLApachelog4j                 
continue;
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             String key 
= id.toLowerCase();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             
//
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
            model.setId(key);
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             model.setAmount(e.getAttributeValue(
"amount"== null ? 2 : Integer.parseInt(e.getAttributeValue("amount")));
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             model.setInit(e.getAttributeValue(
"init"== null || Boolean.getBoolean(e.getAttributeValue("init")));
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             model.setSql(((Element) e.getChildren().get(
0)) .getText());
cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j             
if (model.isInit()) cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j                 cache.put(key, initTCValues(model));
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             }

cache字典表
            
    
    
        CacheSQLXMLApachelog4j             sqlMap.put(key, model);
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         }

cache字典表
            
    
    
        CacheSQLXMLApachelog4j     }

cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
/** *//**
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 
@param file
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      * 
@return Element
cache字典表
            
    
    
        CacheSQLXMLApachelog4j      
*/

cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
private static Element getRootElement(File file) cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j         
try cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             SAXBuilder saxbuilder 
= new SAXBuilder();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             Document document 
= saxbuilder.build(file);
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             lastModefied 
= file.lastModified();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             
return document.getRootElement();
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j         }
 catch (JDOMException e) cache字典表
            
    
    
        CacheSQLXMLApachelog4j {
cache字典表
            
    
    
        CacheSQLXMLApachelog4j             
throw new RuntimeException("JDOMException:" + e.getMessage());
cache字典表
            
    
    
        CacheSQLXMLApachelog4j         }

cache字典表
            
    
    
        CacheSQLXMLApachelog4j     }

cache字典表
            
    
    
        CacheSQLXMLApachelog4j 
cache字典表
            
    
    
        CacheSQLXMLApachelog4j cache字典表
            
    
    
        CacheSQLXMLApachelog4j     
/** */<