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

📄 resultsetcachestorage.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.ConfigureParam;
import org.speedframework.utilities.KeyGenTools;

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

import java.util.*;

/**
 *  ��������Ϣ�����������Ҫְ����ô���
 *
 *
 * @version    $LastChangedRevision: 1945 $, 2007.09.29 at 02:14:28 CST
 * @author     <a href="mailto:falcon8848@gmail.com">piginzoo </a>
 */
public class ResultSetCacheStorage
 {

    /** ����������Ϣ */
    private static final Logger log = Logger.getLogger(ResultSetCacheStorage.class);

    /**
     * ����������Ϣ��
     * ���������ʲô�ģ�
     * ��ε��ã���ø����ô���ʾ��
     *
     * @param key
     *
     * @throws Exception
     */
    public static synchronized void remove(Object key) throws Exception {

        // TODO ????????????
        Map  sqlCollection   = (Map) CacheStorage.get(ConfigureParam.SQL_CACHE_NAME);
        List paramCollection = (List) CacheStorage.get(ConfigureParam.PARAM_CACHE_NAME);
        Map  remove_         = new HashMap();

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

                if (cbtemp.getSql().toLowerCase().indexOf(key.toString().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 < paramCollection.size(); j++) {
                    CacheParamBean paramBean = (CacheParamBean) paramCollection.get(j);

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

                sqlCollection.remove(sqlBean.getCodeid());
                CacheStorage.put(ConfigureParam.SQL_CACHE_NAME, sqlCollection);
                CacheStorage.put(ConfigureParam.PARAM_CACHE_NAME, paramCollection);
                CacheStorage.remove(key + "_" + paramid);
                log.debug("Remove result in cache : id = " + key + "_" + paramid);
            }
        }
    }

    /**
     * ����������Ϣ��
     * ���������ʲô�ģ�
     * ��ε��ã���ø����ô���ʾ��
     *
     * @param key
     * @param params
     * @param collection
     *
     * @throws Exception
     */
    public static synchronized void save(Object key, Object[] params, Object collection) throws Exception {

        // TODO ????????????
        Map     sqlCollection = (Map) CacheStorage.get(ConfigureParam.SQL_CACHE_NAME);
        boolean found         = false;
        int     paramid       = 0;
        String  sqlKey        = KeyGenTools.MD5Encode((String) key);

        if (sqlCollection == null) {
            sqlCollection = new LinkedHashMap();
        } else {
            if (sqlCollection.containsKey(sqlKey)) {
                log.debug("found the repeat sql in SQlBean : id = " + key);
                found = true;
            }
        }

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

            sqlBean.setCodeid(sqlKey);
            sqlBean.setSql((String) key);
            sqlCollection.put(sqlKey, sqlBean);
            log.debug("Put SQLBean in Cache: id = " + key);
            CacheStorage.put(ConfigureParam.SQL_CACHE_NAME, sqlCollection);
        }

        CacheParamBean paramBean = new CacheParamBean();

        paramid = paramBean.hashCode();
        paramBean.setParmvalue(params);
        paramBean.setCodeid(sqlKey);
        paramBean.setParamid(paramid);

        List paramCollection = (List) CacheStorage.get(ConfigureParam.PARAM_CACHE_NAME);

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

        paramCollection.add(paramBean);
        log.debug("Put ParamBean in Cache: id = " + paramid);
        CacheStorage.put(ConfigureParam.PARAM_CACHE_NAME, paramCollection);        
        CacheStorage.put(sqlKey + "_" + paramid, collection);
        log.debug("Put result in cache: id = " + sqlKey + "_" + paramid);
        
    }

    /**
     * ����������Ϣ��
     * ���������ʲô�ģ�
     * ��ε��ã���ø����ô���ʾ��
     *
     * @param key
     * @param params
     *
     * @return
     *
     * @throws Exception
     */
    public static synchronized Object get(Object key, Object[] params) throws Exception {

        // TODO ????????????
        Object collection      = null;
        Map    sqlCollection   = (Map) CacheStorage.get(ConfigureParam.SQL_CACHE_NAME);
        List   paramCollection = (List) CacheStorage.get(ConfigureParam.PARAM_CACHE_NAME);
        String sqlKey          = KeyGenTools.MD5Encode((String) key);

        if ((sqlCollection != null) && sqlCollection.containsKey(sqlKey)) {
            CacheSQLBean sqlBean = (CacheSQLBean) sqlCollection.get(sqlKey);

            if (sqlBean.getSql().equals(key)) {
                log.debug("SQL found : id = " + sqlKey);

                if ((paramCollection != null) && (paramCollection.size() > 0)) {
                    Iterator paramIt = paramCollection.iterator();

                    while (paramIt.hasNext()) {
                        boolean        found     = true;
                        CacheParamBean paramBean = (CacheParamBean) paramIt.next();

                        if (paramBean.getCodeid().equals(sqlKey)) {
                            log.debug("found the same SQL in ParamBean : " + paramBean.getParamid());

                            Object[] sqlParams = paramBean.getParmvalue();

                            if (sqlParams.length == params.length) {
                                for (int i = 0; i < params.length; i++) {
                                    if (!sqlParams[i].equals(params[i])) {
                                        log.debug("param's value not match");
                                        found = false;

                                        break;
                                    }
                                }
                            } else {
                                continue;
                            }

                            if (found) {
                                collection = CacheStorage.get(sqlKey + "_" + paramBean.getParamid());
                                log.debug("Found the ResultList : id = " + sqlKey + "_" + paramBean.getParamid());

                                break;
                            }
                        }
                    }
                }
            }
        }

        return collection;
    }
}

⌨️ 快捷键说明

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