冰风2-缓存

缓存

1.CacheUnit
package com.xf.common.unit;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.xf.common.util.StringUtil;

/** 
 *
 * @author 董晓峰
 * @datetime 2017年8月4日 下午6:10:09
 * @editnote 
 *  
 */
public abstract class CacheUnit {

    private String DMZL = "";

    private String fieldID = "id";

    private String fieldMC = "mc";

    private String fieldKey = "key";

    private String fieldValue = "value";

    public List listDM = new ArrayList();

    public String getDMZL() {
        return this.DMZL;
    }

    public void setDMZL(String dMZL) {
        DMZL = dMZL;
    }

    public String getFieldID() {
        return this.fieldID;
    }

    public void setFieldID(String fieldID) {
        this.fieldID = fieldID;
    }

    public String getFieldMC() {
        return this.fieldMC;
    }

    public void setFieldMC(String fieldMC) {
        this.fieldMC = fieldMC;
    }

    public String getFieldKey() {
        return this.fieldKey;
    }

    public void setFieldKey(String fieldKey) {
        this.fieldKey = fieldKey;
    }

    public String getFieldValue() {
        return this.fieldValue;
    }

    public void setFieldValue(String fieldValue) {
        this.fieldValue = fieldValue;
    }

    public List getDMList(String dmlb) {
        return this.listDM;
    }

    public Object getDMByID(String dmlb, String id) {
        try {
            Iterator iter = this.listDM.iterator();
            while (iter.hasNext()) {
                Object obj = iter.next();
                String tid = BeanUtils.getProperty(obj, getFieldID());
                if (id.equals(tid)){
                    return obj;
                }
            }
        } catch (Exception e) {
            return null;
        }
        return null;
    }

    public Object getDMByKey(String dmlb, String strKey) {
        try {

            Iterator iter = this.listDM.iterator();
            while (iter.hasNext()) {
                Object obj = iter.next();
                String key = BeanUtils.getProperty(obj, getFieldKey());
                if (key == null) {
                    continue;
                }
                if (key.equals(strKey)) {
                    return obj;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
        return null;
    }

    public String getMCByID(String dmlb, String id) {
        Object obj = getDMByID(dmlb, id);
        if (obj == null || StringUtil.isEmpty(id))
            return null;
        try {
            return BeanUtils.getProperty(obj, getFieldMC());
        } catch (Exception ex) {

        }
        return null;
    }

    /**
     * 通过key值得到名称
     *
     * @param dmlb(缓存的类比)
     * @param strKey key值
     * @return 名称mc
     * @datetime 2017年9月26日 上午11:04:11
     * @editnote 
     *
     */
    public String getMCByKey(String dmlb, String strKey) {
        Object obj = getDMByKey(dmlb, strKey);
        if (obj == null || StringUtil.isEmpty(strKey))
            return null;
        try {
            return BeanUtils.getProperty(obj, getFieldMC());
        } catch (Exception ex) {

        }
        return null;
    }
}

注:

  1. 这里面的StringUtil类为我自己写的工具类,详细查看文章中的Util模块。
  2. 这的StrinUitl.isEmpty方法功能是判断字符串是否为空
  3. BeanUtis为引入的jar包。可在冰风1里面查看
2.CacheUnitUSER
package com.xf.common.unit;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xf.common.util.SpringUtil;
import com.xf.dao.base.PublicDao;
import com.xf.entity.table.GG_USER;
import com.xf.exception.MisException;


/** 
 *
 * @author 董晓峰
 * @datetime 2017年8月4日 下午9:49:41
 * @editnote 
 *  
 */
public class CacheUnitUSER extends CacheUnit {
    private static final Log log = LogFactory.getLog(CacheUnitUSER.class);

    public CacheUnitUSER(){
        setDMZL("USER");
        setFieldID("id");
        setFieldMC("name");
        setFieldKey("username");

        PublicDao publicDao = (PublicDao) SpringUtil.getBean("publicDao");

        try {
            List<GG_USER> listUser = publicDao.listAllUser();
            this.listDM = listUser;
        } catch (Exception e) {
            log.error(e);
            throw new MisException(e);
        }
    }
}

注:
想加入那张表 就再写一个类继承CacheUnit 就好了

3.最后写一个工厂类
package com.xf.common.unit;

import java.util.HashMap;

import com.xf.common.util.StringUtil;

/** 
 *
 * @author 董晓峰
 * @datetime 2017年8月4日 下午6:12:01
 * @editnote 
 *  
 */
public class CacheFactory {
    private static CacheFactory dmFactory = new CacheFactory();

    private static HashMap<String, CacheUnit> hmDMZL = new HashMap<String, CacheUnit>();

    public static CacheFactory getInstance() {
        return dmFactory;
    }
    public static CacheUnit factory(String dmlb) {
        if(hmDMZL.containsKey(dmlb)){
            return hmDMZL.get(dmlb);
        }
        return createDm(dmlb);
    }
    private static CacheUnit createDm(String dmlb){
        CacheUnit dm = null;
        if(StringUtil.isEmpty(dmlb)){
            return null;
        }
        if(dmlb.equals("USER")){
            dm = new CacheUnitUSER();
        }
        hmDMZL.put(dmlb, dm);
        return dm;
    }

    /**
     * 清理全部缓存
     * @datetime 2017年8月4日 下午10:01:17
     * @editnote 
     *
     */
    public static void clear() {
        for (String key : hmDMZL.keySet()) {
            CacheUnit unit = hmDMZL.get(key);
            unit.listDM = null;
            unit = null;
        }
        hmDMZL.clear();
    }

    /**
     * 清理指定代码表缓存
     * @param dmlb
     * @datetime 2017年8月4日 下午10:01:44
     * @editnote 
     *
     */
    public static void clear(String dmlb) {
        if (hmDMZL.containsKey(dmlb)) {
            CacheUnit unit = hmDMZL.get(dmlb);
            hmDMZL.remove(dmlb);
            unit.listDM = null;
            unit = null;
        }
    }

}

到这里缓存基本上已经写完了。

4.还可以用单例模式写一个类来调用缓存
package com.xf.common;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xf.common.unit.CacheFactory;
import com.xf.common.unit.CacheUnit;

/**
 * 缓存 
 *
 * @author 董晓峰
 * @datetime 2017年8月4日 下午6:07:23
 * @editnote 缓存的表有 GG_USER GG_DMNR
 *
 */
public class DMLoader {
    private static final Log log = LogFactory.getLog(DMLoader.class);

    private static DMLoader dmLoader = null;

    private DMLoader(){
    }
    public static DMLoader getInstance(){
        if(dmLoader == null){
            dmLoader = new DMLoader();
        }
        return dmLoader;
    }

    /**获取全部代码*/
    public static List<?> getDMList(String dmlb) {
        CacheUnit flyweight = CacheFactory.factory(dmlb);
        return flyweight.getDMList(dmlb);
    }

    /**根据ID获得这个对象*/
    public static Object getDMByID(String dmlb, String id) {
        CacheUnit flyweight = CacheFactory.factory(dmlb);
        return flyweight.getDMByID(dmlb, id);
    }
    /**根据主键获取这个对象*/
    public static Object getDMByKey(String dmlb, String key) {
        CacheUnit flyweight = CacheFactory.factory(dmlb);
        return flyweight.getDMByKey(dmlb, key);
    }
    /**根据ID获得名称*/
    public static String getMCByID(String dmlb, String id) {
        CacheUnit flyweight = CacheFactory.factory(dmlb);
        String strMC = flyweight.getMCByID(dmlb, id);
        return strMC;
    }
    /**根据Key获得这个名称*/
    public static String getMCByKey(String dmlb, String key) {
        CacheUnit flyweight = CacheFactory.factory(dmlb);
        String strMC = flyweight.getMCByKey(dmlb, key);
        return strMC;
    }
    /**
     * 代码类别清理:dmlb
     *
     * @param dmlb
     * @author 董晓峰
     * @datetime 2017年8月5日 上午12:04:21
     * @editnote 
     *
     */
    public static void clear(String dmlb) {
        CacheFactory.clear(dmlb);
        log.info("代码类别清理:" + dmlb);
    }
    /**
     * 代码类别清理:全部
     *
     * @author 董晓峰
     * @datetime 2017年8月5日 上午12:04:07
     * @editnote 
     *
     */
    public static void clear() {
        CacheFactory.clear();
        log.info("代码类别清理:全部");
    }

}

5.把DMLoader里的方法加入自定义标签中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值