EhCacheUtil缓存工具类

/**
 * 使用API来动态的添加缓存(将缓存的配置信息通过java代码来实现而非写在配置文件)
 *
 * @author Administrator
 */
public class EhCacheUtil {
    private static Logger logger = LoggerFactory.getLogger(EhCacheUtil.class);
    private static CacheManager singletonManager;
    private static String proName;
    private static int maxElementsInMemory;
    private static Boolean overflowToDisk;
    private static boolean eternal;
    private static long timeToLiveSeconds;
    private static long timeToIdleSeconds;

    static {
        singletonManager = CacheManager.create();
        Properties properties = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = EhCacheUtil.class.getClassLoader().getResourceAsStream("ehcache.properties");
        // 使用properties对象加载输入流
        try {
            properties.load(in);
            proName = Globals.APP_BASE_NAME.replace("/","");
            maxElementsInMemory = Integer.parseInt(properties.getProperty("MAXELEMENTSINMEMORY"));
            overflowToDisk = Boolean.parseBoolean(properties.getProperty("OVERFLOWTODISK"));
            eternal = Boolean.parseBoolean(properties.getProperty("ETERNAL"));
            timeToLiveSeconds = Long.parseLong(properties.getProperty("TIMETOLIVESECONDS"));
            timeToIdleSeconds = Long.parseLong(properties.getProperty("TIMETOIDLESECONDS"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 创建缓存区间
     */
    public static void createCache() {
        Cache memoryOnlyCache = singletonManager.getCache(proName);
        //建立一个缓存实例
        if (memoryOnlyCache == null) {
            //项目名称   缓存最大数量 是否缓存成文件 设定缓存是否过期  对象存活时间   无访问多长时间缓存失效
            memoryOnlyCache = new Cache(proName, maxElementsInMemory, overflowToDisk, eternal, timeToLiveSeconds, timeToIdleSeconds);
            //在内存管理器中添加缓存实例
            singletonManager.addCache(memoryOnlyCache);
        }
        logger.info("缓存空间->"+proName+"库创建成功!");
    }

    /**
     * 查询缓存中的数据
     *
     * @param key
     * @return
     */
    @SuppressWarnings("deprecation")
    public static Object getValue(String key) {
        //在缓存管理器中获取一个缓存实例
        Cache cache = singletonManager.getCache(proName);
        Element element = cache.get(key);
        if (element == null) {
            return null;
        }
        return element.getValue();
    }

    /**
     * 添加缓存
     *
     * @param key
     * @param value
     */
    public static void setValue(String key, Object value) {
        Cache cache = singletonManager.getCache(proName);
        //使用获取到的缓存实例
        Element element = new Element(key, value);
        cache.put(element);//添加缓存值
    }

    /**
     * 删除缓存
     *
     * @param key
     */
    public static void remove(String key) {
        Cache cache = singletonManager.getCache(proName);
        Element element = cache.get(key);
        if (element != null) {
            cache.remove(key);
        }
    }

    /**
     * 是否存在缓存
     *
     * @param key
     */
    public static boolean isEmpty(String key) {
        Cache cache = singletonManager.getCache(proName);
        Element element = cache.get(key);
        if (element != null) {
            return false;
        }
        return true;
    }
}
配置文件ehcache.properties
#缓存最大数量
MAXELEMENTSINMEMORY=1000
#是否缓存成文件
OVERFLOWTODISK=false
#设定缓存是否过期
ETERNAL=false
#对象存活时间单位秒
TIMETOLIVESECONDS=7200
#无访问多长时间缓存失效 单位秒
TIMETOIDLESECONDS=7200
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值