ehcache的实际使用

一、定义一个工具类

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;

/**
 * ehcache 缓存工具类
 */
public class EHCacheUtils {

    public static final String MAP_CACHE_NAME = "mapCache";
    public static final String ENTITY_CACHE_NAME = "entityCache";
    public static final String COMMON_CACHE_NAME = "commonCache";
    public static final String DICTIONARY_CACHE_NAME = "dictionaryCache";

    public static final CacheManager manager = CacheManager.create();
    public static Map<String, Cache> cacheMap = new HashMap<>();

    /**
     * 根据缓存库名称和key获取value
     *
     * @param cacheName 缓存名称
     * @param key       key值 
     * @return 返回缓存的值
     */
    public static Object getCacheValue(String cacheName, Object key) {
        Cache cache = getCache(cacheName);
        if (cache != null) {
            Element element = cache.get(key);
            if (element != null) {
                return element.getObjectValue();
            }
        }
        return null;
    }

    /**
     * 根据缓存名称和key存入一个缓存
     *
     * @param cacheName 缓存名称
     * @param key       key值
     * @param value     缓存的值
     */
    public static void putCacheValue(String cacheName, Object key, Object value) {
        Cache cache = getCache(cacheName);
        if (cache != null) {
            cache.put(new Element(key, value));
        }
    }

    /**
     * 删除一个缓存
     *
     * @param cacheName 缓存名称
     * @param key       key值
     */
    @NotNull
    public static Boolean removeCacheValue(String cacheName, Object key) {
        Cache cache = getCache(cacheName);
        if (cache != null) {
            return cache.remove(key);
        }
        return false;
    }

    public static Cache getCache(String cacheName) {
        if (cacheMap.get(cacheName) == null) {
            cacheMap.put(cacheName, manager.getCache(cacheName));
        }
        return manager.getCache(cacheName);
    }
}

二、进行使用

 /**
     * 查询所有的城市信息
     * 缓冲中有直接返回,没有存入缓冲返回
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<DictionaryVO> selectAllCities() {
        if (EHCacheUtils.getCacheValue(EHCacheUtils.DICTIONARY_CACHE_NAME, DictionaryConstants.OP_TYPE_CITY) == null) {
            EHCacheUtils.putCacheValue(EHCacheUtils.DICTIONARY_CACHE_NAME, DictionaryConstants.OP_TYPE_CITY, commonDao.selectAllCities());
        }
        return (List<DictionaryVO>) EHCacheUtils.getCacheValue(EHCacheUtils.DICTIONARY_CACHE_NAME, DictionaryConstants.OP_TYPE_CITY);
    }

三、ehcache的配置文件
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

    <!-- 磁盘缓存位置 -->
    <diskStore path="D:/Workspace/IdeaWorkSpace/zld_refacting/ehcache"/>

    <!-- 默认缓存 -->
    <defaultCache
            maxEntriesLocalHeap="1000000"
            eternal="true"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxEntriesLocalDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"/>

    <!-- 数据字典缓存,主要用来缓存数据字典的内容以及省份、城市、逻辑链路等一些信息 -->
    <cache name="dictionaryCache"
           maxElementsInMemory="1000000"
           eternal="true"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="3600"
           overflowToDisk="false"
           memoryStoreEvictionPolicy="LRU"/>

    <!-- commonCache缓存,主要用来缓存默认省份代码以及一些公共常量 -->
    <cache name="commonCache"
           maxElementsInMemory="1000000"
           eternal="true"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="3600"
           overflowToDisk="false"
           memoryStoreEvictionPolicy="LRU"/>
</ehcache>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值