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

📄 cachemanage.java

📁 羽量级数据持久层开发框架
💻 JAVA
字号:
package org.speedframework.cache;

//~--- non-JDK imports --------------------------------------------------------

import org.apache.log4j.Logger;

import org.speedframework.entity.CacheParamBean;
import org.speedframework.entity.CacheSQLBean;
import org.speedframework.utilities.KeyGenTools;

//~--- JDK imports ------------------------------------------------------------

import java.util.*;

/**
 * <p/>
 * Title: SpeedFrameworkWork
 * </p>
 * <p/>
 * <p/>
 * Description: Cache Control Object
 * </p>
 * <p/>
 * <p/>
 * Copyright: Copyright (c) 2006
 * </p>
 * <p/>
 * <p/>
 * Company: SpeedFrameworkWork team
 * </p>
 *
 * @author
 * @version 1.0
 */
public class CacheManage
 {

    /** 属性描述信息 */
    private static final String ParamCacheName = "paramCache";

    // private static String CachePoolName = "speedCache";

    /** 属性描述信息 */
    private static final String SQLCacheName = "sqlCache";

    /** 属性描述信息 */
    private static final Logger log = Logger.getLogger(CacheManage.class);

    /** 属性描述信息 */
    private static final CachePool cp = CachePoolFactory.getProviderCache();

    /**
     * Constructs ...
     *
     */
    public CacheManage() {}

    /**
     * Find Cache Store Object
     *
     * @param parmvalue Object[]
     * @param SQL       String
     * @return List
     * @throws Exception
     */
    public static synchronized List FindCache(Object[] parmvalue, String SQL) throws Exception {
        List relist = null;

        // cp.newInstance(CachePoolName);
        CacheSQLBean sqlBean = new CacheSQLBean();

        sqlBean.setSql(SQL);
        sqlBean.setCodeid(KeyGenTools.MD5Encode(SQL));

        // cbone.setParmvalue(parmvalue);
        // cbone.setSql(SQL);
        Map  sqlList   = (Map) cp.get(SQLCacheName);
        List paramList = (List) cp.get(ParamCacheName);

        if ((sqlList != null) && (sqlList.size() > 0)) {
            for (Iterator it = sqlList.keySet().iterator(); it.hasNext(); ) {
                boolean found = true;

                // CacheBean cbtemp = (CacheBean) it.next();
                CacheSQLBean tempBean = (CacheSQLBean) sqlList.get((String) it.next());
                String       codeid   = tempBean.getCodeid();
                int          paramid  = 0;

                if (tempBean.getSql().equals(SQL)) {    // check sql

                    // is match
                    log.debug("SQL found : id = " + codeid);

                    // String[] cache_paramvalue = cbtemp.getParmvalue();
                    if ((paramList != null) && (paramList.size() > 0)) {
                        Iterator paramIt = paramList.iterator();

                        while (paramIt.hasNext()) {
                            CacheParamBean paramTemp = (CacheParamBean) paramIt.next();

                            if (paramTemp.getCodeid().equals(codeid)) {
                                paramid = paramTemp.getParamid();
                                log.debug("found the param object: id = " + paramTemp.getParamid());

                                Object[] cache_paramvalue = paramTemp.getParmvalue();

                                if (cache_paramvalue.length == parmvalue.length) {    // check

                                    // param
                                    // value
                                    // is
                                    // match
                                    log.debug("param's length match");

                                    for (int param_length = 0; param_length < cache_paramvalue.length; param_length++) {
                                        if (!cache_paramvalue[param_length].toString().equals(
                                                parmvalue[param_length].toString())) {
                                            log.debug("param'value not match");
                                            found = false;

                                            break;
                                        }
                                    }

                                    if (found) {
                                        relist = (List) cp.get(codeid + "_" + paramid);
                                        log.debug("Found the ResultList : id = " + codeid + "_" + paramid);

                                        break;
                                    }
                                } else {
                                    log.debug("param's length not match ");

                                    // found = false;
                                    break;
                                }
                            } else {
                                continue;
                            }
                        }
                    }
                } else {
                    continue;
                }
            }
        }

        return relist;
    }

    /**
     * Store Cache Object
     *
     * @param parmvalue Object[]
     * @param SQL       String
     * @param putlist   List
     * @throws Exception
     */
    public static synchronized void put(Object[] parmvalue, String SQL, List putlist) throws Exception {

        // cp.newInstance(CachePoolName);
        boolean found     = false;
        String  key       = KeyGenTools.MD5Encode(SQL);
        ;
        int     paramid   = 0;
        Map     sqlList   = (Map) cp.get(SQLCacheName);
        List    paramList = (List) cp.get(ParamCacheName);

        if (sqlList == null) {
            sqlList = new LinkedHashMap();

            // CacheSQLBean sqlTemp = new CacheSQLBean();
            // sqlTemp.setCodeid(key);
            // sqlTemp.setSql(SQL);
            //
            // sqlList.put(key,sqlTemp);
            // log.debug("Put SQLBean in Cache: id = " + key);
            // cp.put(SQLCacheName, sqlList);
        } else {

            // while (it.hasNext()) {
            //
            // CacheSQLBean sqlBean = (CacheSQLBean) it.next();
            // if (sqlBean.getSql().equals(SQL)) {
            // codeid = sqlBean.getCodeid();
            // log.debug("found the repeat sql in SQlBean : id = "
            // + codeid);
            // found = true;
            // break;
            // }
            if (sqlList.containsKey(key)) {
                log.debug("found the repeat sql in SQlBean : id = " + key);
                found = true;
            }
        }

        if (!found) {
            CacheSQLBean sqlBean = new CacheSQLBean();

            sqlBean.setCodeid(key);
            sqlBean.setSql(SQL);
            sqlList.put(key, sqlBean);
            log.debug("Put SQLBean in Cache: id = " + key);
            cp.put(SQLCacheName, sqlList);
        }

        CacheParamBean cb = new CacheParamBean();

        paramid = cb.hashCode();
        cb.setParmvalue(parmvalue);
        cb.setCodeid(key);
        cb.setParamid(paramid);

        if (paramList == null) {
            paramList = new ArrayList();
        }

        paramList.add(cb);
        log.debug("Put ParamBean in Cache: id = " + paramid);
        cp.put(ParamCacheName, paramList);

        // log.debug("Load in Cache Object::" + sqlTemp.hashCode());
        cp.put(key + "_" + paramid, putlist);
        log.debug("Put result in cache: id = " + key + "_" + paramid);
    }

    /**
     * Delete Cache Object
     *
     * @param TableName String
     * @throws Exception
     */
    public static synchronized void RemoveCache(String TableName) throws Exception {

        // cp.newInstance(CachePoolName);
        Map  sqlList   = (Map) cp.get(SQLCacheName);
        List paramList = (List) cp.get(ParamCacheName);
        Map  remove_   = new HashMap();

        if ((sqlList != null) && (sqlList.size() > 0)) {
            for (Iterator it = sqlList.keySet().iterator(); it.hasNext(); ) {
                CacheSQLBean cbtemp = (CacheSQLBean) sqlList.get((String) it.next());

                if (cbtemp.getSql().toLowerCase().indexOf(TableName.toLowerCase()) != -1) {
                    log.debug("Update/Delete Cache Object::" + cbtemp.getCodeid());

                    // list.remove(cbtemp);
                    remove_.put(cbtemp.getCodeid(), cbtemp);

                    // CachePool.remove(Integer.toString(cbtemp.hashCode()));
                }
            }
        }

        if (remove_.size() > 0) {
            Iterator it = remove_.keySet().iterator();

            while (it.hasNext()) {
                String key     = it.next().toString();
                int    paramid = 0;

                log.debug("Delete SQlBean : id = " + key);

                CacheSQLBean sqlBean = (CacheSQLBean) remove_.get(key);
                String       codeid  = sqlBean.getCodeid();

                for (int j = 0; j < paramList.size(); j++) {
                    CacheParamBean paramBean = (CacheParamBean) paramList.get(j);

                    if (paramBean.getCodeid().equals(codeid)) {
                        paramid = paramBean.getParamid();
                        log.debug("Delete ParamBean : id = " + paramid);
                        paramList.remove(paramBean);
                    }
                }

                sqlList.remove(sqlBean.getCodeid());
                cp.remove(key + "_" + paramid);
                log.debug("Remove result in cache : id = " + key + "_" + paramid);
            }
        }

        // log.debug(CachePool.get("SQLBean"));
    }
}

⌨️ 快捷键说明

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