java-Ehcache缓存

springmvc配置文件:

<beans
....
xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="....
        http://www.springframework.org/schema/cache 
        http://www.springframework.org/schema/cache/spring-cache.xsd">
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
         <property name="configLocation" value="classpath:ehcache.xml"></property>
     </bean>
     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
         <property name="cacheManager" ref="cacheManagerFactory"></property>
     </bean>
     <!-- 开启spring缓存 -->
     <cache:annotation-driven cache-manager="cacheManager" />

ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
    updateCheck="false">
    <!-- 缓存到磁盘路径 -->
    <diskStore path="d:/cache" />
     <defaultCache
           maxEntriesLocalHeap="0"
           eternal="false"
           timeToIdleSeconds="30"
           timeToLiveSeconds="30">
    </defaultCache>
    <!-- 自定义缓存 -->
    <cache 
        name="loginSession" 
        maxElementsInMemory="200"
        maxElementsOnDisk="1000" 
        eternal="false" 
        overflowToDisk="true"
        diskSpoolBufferSizeMB="20" 
        timeToIdleSeconds="300" 
        timeToLiveSeconds="600"
        memoryStoreEvictionPolicy="LFU" />
    <cache 
        name="loginIpList" 
        maxElementsInMemory="200"
        maxElementsOnDisk="1000" 
        eternal="false" 
        overflowToDisk="true"
        diskSpoolBufferSizeMB="10" 
        timeToIdleSeconds="300" 
        timeToLiveSeconds="600"
        memoryStoreEvictionPolicy="LFU" />
</ehcache>

自定义Ehcache类:

package net.nblh.utils.common;

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

public class EhcacheUtils {
    private CacheManager cacheManager;
    private static final String path = "/ehcache.xml";//ehcache的配置文件的路径
    private static EhcacheUtils ehcache;
    
    /**
     * 创建缓存对象
     * @param path
     */
    private EhcacheUtils(String path) {
        cacheManager = CacheManager.create(getClass().getResource(path));
    }
    
    public static EhcacheUtils getInstance() {
        if (ehcache == null) {
            ehcache = new EhcacheUtils(path);
        }
        return ehcache;
    }
    
    /**
     * 缓存一个对象
     * @param cacheName
     * @param key
     * @param value
     */
    public void putCache(String cacheName,Object key,Object value) {
        Cache cache = cacheManager.getCache(cacheName);
        Element element = new Element(key, value);
        cache.put(element);
    }
    
    /**
     * 获取一个缓存对象
     * @param cacheName
     * @param key
     * @return
     */
    public Object getCache(String cacheName,String key) {
        Cache cache = cacheManager.getCache(cacheName);
        Element element = cache.get(key);
        return element == null?null:element.getObjectValue();
    }
    
    public Cache getCache(String cacheName) {
        return cacheManager.getCache(cacheName);
    }
    
    /**
     * 移除一个缓存对象
     * @param cacheName
     * @param key
     */
    public void removeCache(String cacheName,String key) {
        Cache cache = cacheManager.getCache(cacheName);
        cache.remove(key);
    }
    
}

调用:

EhcacheUtils.getInstance().putCache("loginSession", session, user.getLoginName());
Cache cache = EhcacheUtils.getInstance().getCache("loginSession");
EhcacheUtils.getInstance().putCache("loginIpList", user.getId(), loginIp);
Cache cache = EhcacheUtils.getInstance().getCache("loginIpList");
原文地址:https://www.cnblogs.com/lijianda/p/10894764.html