Blame view

EhCacheUtil.java 1.28 KB
涂亚平 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
package com.subsidy.util;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.util.ClassUtils;

/**
 * <p>
 *  Ehcache
 * </p>
 *
 * @author DengMin
 * @since 2022/8/1
 */
public class EhCacheUtil {

    static CacheManager cacheManager = CacheManager.newInstance(ClassUtils.getDefaultClassLoader().getResource("").getPath()+"ehcache.xml");

    /**
     * 添加缓存
     * @param cacheName
     * @param key
     * @param value
     */
    public static void putEhCache(String cacheName, String key, Object value) {
        Cache cache = cacheManager.getCache(cacheName);
        Element element = new Element(key, value);
        cache.put(element);
    }

    /**
     * 获取缓存数据
     * @param cacheName
     * @param key
     * @return
     */
    public static Object getEhCache(String cacheName, String key) {
        Cache cache = cacheManager.getCache(cacheName);
        Element element = cache.get(key);
        return element == null ? null : element.getObjectValue();
    }

    /**
     * 删除缓存
     * @param cacheName
     * @param key
     */
    public static void deleteEhCache(String cacheName, String key) {
        Cache cache = cacheManager.getCache(cacheName);
        cache.remove(key);
    }
}