Ehcache的一个完整例子

Ehcache的一个完整例子

测试类

测试驱动开发,先写测试类

package com.company.mapp.cache;

import org.junit.Test;

import com.company.component.PageData;

public class CacheTest {

    @Test
    public final void testGetValue() throws Exception {
        Cache cache = CacheFactory.getCache(Constants.MAPP_STATIC_PARAM_CFG);
        cache.setPageData(new PageData());
        cache.getValue("MAPP_AMAP_PARAM");
    }

}

接口

面向接口开发,先有一个接口

package com.company.mapp.cache;

import java.util.Map;

import com.company.component.PageData;

/**
 * 缓存接口
 * 
 * @see CacheTest
 * @author ouyida3
 * @since 2015.4.3
 */
public interface Cache {

    public String getValue(String key) throws Exception;

    public Map<String, String> getMap(String key) throws Exception;

    public Cache setPageData(PageData pd);

}

工厂类

package com.company.mapp.cache;

/**
 * 缓存工厂
 * 生产各张数据表的缓存
 * 
 * @author ouyida3
 * @since 2015.4.4
 */
public class CacheFactory {

    public static Cache getCache(String name){
        if (Constants.MAPP_STATIC_PARAM_CFG.equals(name))
            return new StaticParamCfgCache(name);
        else if (Constants.MAPP_S_STATIC.equals(name))
            return new SStaticCache(name);
        return null;
    }

}

常量

package com.company.mapp.cache;

/**
 * 缓存常量类
 * 
 * @author ouyida3
 * @since 2015.4.4
 */
public class Constants {
    public static final String MAPP_STATIC_PARAM_CFG = "MAPP_STATIC_PARAM_CFG";
    public static final String MAPP_S_STATIC = "MAPP_S_STATIC";
}

抽象类

每个缓存都有共同的方法,所以必须得有抽象类放共同方法

package com.company.mapp.cache;

import java.util.Map;

import com.company.component.PageData;
import com.company.mapp.bean.FSBean;

/**
 * 缓存抽象类
 * 每个数据表的缓存类的重复的方法在这里实现
 * 
 * @author ouyida3
 * @since 2015.4.4
 */
public abstract class AbstractCache implements Cache {
    protected String cacheName;
    protected String eparchyCode;
    protected PageData pd;

    protected Cache setEparchyCode(String eparchyCode) {
        this.eparchyCode = eparchyCode;
        return this;
    }

    protected AbstractCache(String name) {
        this.cacheName = name;
    }

    @Override
    public Cache setPageData(PageData pd) {
        this.pd = pd;
        try {
            setEparchyCode(FSBean.getEparchyCode(pd));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    @Override
    public String getValue(String key) throws Exception {
        return null;
    }

    @Override
    public Map<String, String> getMap(String key) throws Exception {
        return null;
    }
}

其中一张数据表

表里就两个字段最重要,很简单,就是根据key取value

package com.company.mapp.cache;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * 数据表MAPP_STATIC_PARAM_CFG缓存
 * 
 * @author ouyida3
 * @since 2015.4.3
 */
public class StaticParamCfgCache extends AbstractCache {

    protected StaticParamCfgCache(String name) {
        super(name);
    }

    public String getValue(String key) throws Exception {
        net.sf.ehcache.Cache cache = CacheManager.getInstance().getCache(cacheName);
        String cacheKey = key + "_" + eparchyCode;

        if (cache != null) {
            Element element = cache.get(cacheKey);
            if (element != null)
                return (String) element.getValue();
        }

        CacheDao dao = new CacheDao();
        String value = dao.queryStaticParamCfg(cacheName, key, pd);

        if (cache != null) {
            cache.put(new Element(cacheKey, value));
        }

        return value;
    }
}

其中另一张数据表

比刚才复杂一点,有三个重要字段,根据1、2取到3,因此我设计为先根据1取到2和3的map,然后再根据2取3

package com.company.mapp.cache;

import java.util.HashMap;
import java.util.Map;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import com.company.appframework.data.DataMap;
import com.company.appframework.data.IDataset;

/**
 * 数据表MAPP_S_STATIC缓存
 * 
 * @author ouyida3
 * @since 2015.4.4
 */
public class SStaticCache extends AbstractCache {

    public SStaticCache(String name) {
        super(name);
    }

    @SuppressWarnings("unchecked")
    public Map<String, String> getMap(String key) throws Exception {
        net.sf.ehcache.Cache cache = CacheManager.getInstance().getCache(cacheName);
        String cacheKey = key + "_" + eparchyCode;

        if (cache != null) {
            Element element = cache.get(cacheKey);
            if (element != null)
                return (Map<String, String>) element.getValue();
        }

        CacheDao dao = new CacheDao();
        IDataset dataSet = dao.querySStatic(cacheName, key, pd);
        HashMap<String, String> value = new HashMap<String, String>();
        for (Object dataMap : dataSet) {
            value.put((String)((DataMap)dataMap).get("DATA_ID"), (String)((DataMap)dataMap).get("DATA_NAME"));
        }

        if (cache != null) {
            cache.put(new Element(cacheKey, value));
        }

        return value;
    }

}

工具类

最后加的,同事认为这样调用更方便,我试了一下,确实是。

package com.company.mapp.cache;

import java.util.Map;

import com.company.component.PageData;

/**
 * 缓存工具类
 * 外部直接调用该类的方法,隐藏整个Cache框架的实现与调用细节
 * 
 * @author ouyida3
 * @since 2015.4.4
 */
public class CacheUtils {

    public static String getStaticValue(String dataId, String typeId, PageData pd) throws Exception {
        Cache cache = CacheFactory.getCache(Constants.MAPP_S_STATIC);
        cache.setPageData(pd);
        Map<String, String> map = cache.getMap(typeId);
        String dataName = (String)map.get(dataId);
        return dataName;
    }

}

ouyida3的csdn blog
2015.4.4

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。