• 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");
  • 相关阅读:
    我正在使用的 Gnome 扩展列表(不定期更新)
    PHP中 <<< A ··· A 是什么意思
    Java 中 this 的作用
    Java 类成员变量中 public, private, protected, default 权限区别
    虫师github
    在线学习的网站
    软件测试博客
    乙醇公开课视频
    教你如何写框架------元素管理篇
    自动化测试与持续集成方案--接口测试
  • 原文地址:https://www.cnblogs.com/lijianda/p/10894764.html
Copyright © 2020-2023  润新知