码迷,mamicode.com
首页 > 系统相关 > 详细

EhCacheUtils 缓存 ehche (将文件临时保存在磁盘)

时间:2020-05-12 20:50:59      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:pack   获取   shutdown   eal   disk   ble   保留   磁盘   throws   

参考:https://www.jianshu.com/p/154c82073b07

依赖:

    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>2.10.2</version>
    </dependency>

 

配置文件:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

    <!--缓存的路径  当前配置为系统的临时目录  可以任意修改为任意盘符下的文件夹-->
  <!--   <diskStore path="java.io.tmpdir/ehcache"/> -->
    <diskStore path="/tmp/sea/"/>
    <!--
    maxElementsInMemory:内存存储数据的个数
    eternal:缓存数据是否永久有效  建议false
    timeToIdleSeconds:最大空闲时间 (s)  空闲时间超出配置,清理内存数据
    timeToLiveSeconds:存活时间(s)
    overflowToDisk: 溢出到磁盘(磁盘最多存储多少个对象) 如果内存超过maxElementsInMemory配置那么放置到配置的磁盘路径上
    diskPersistent: 服务器重启时是否保留磁盘数据
    diskExpiryThreadIntervalSeconds: 每隔多长时间进行线程扫描
    memoryStoreEvictionPolicy:淘汰策略 LRU(最近最少)  FIFO(先进先出 Frist in Frist out)
    -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            >
    </defaultCache>
    
     <!--  换存10 min   -->
    <cache name="MIN10" eternal="false" maxElementsInMemory="1000"
        overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
        timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />
        
        
    <cache name="HOUR2" eternal="false" maxElementsInMemory="100"
        overflowToDisk="true" diskPersistent="true" timeToIdleSeconds="0"
        timeToLiveSeconds="3600" memoryStoreEvictionPolicy="LRU" />


    <!-- 系统缓存  会一直保存,掉电也会存在 需要 System.setProperty("net.sf.ehcache.enableShutdownHook","true");-->
    <cache name="sysCache" maxEntriesLocalHeap="100" 
    eternal="true"  diskPersistent="true" overflowToDisk="true" memoryStoreEvictionPolicy="LRU" />  
</ehcache>

 

EhCacheUtils:

package com.icil.collect.common;

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

public class EhCacheUtils {
    
        private static CacheManager cacheManager=null;

        public  static final String SYS_CACHE = "sysCache";
        public static final String MIN10 = "MIN10";
        public static final String HOUR2 = "HOUR2";

        public final static Byte[] locks = new Byte[0];

        /**
         * 获取SYS_CACHE缓存
         * @param key
         * @return
         */
        public static Object get(String key) {
            return get(SYS_CACHE, key);
        }

        /**
         * 写入SYS_CACHE缓存
         * @param key
         * @return
         */
        public static void put(String key, Object value) {
            put(SYS_CACHE, key, value);
        }

        /**
         * @param key
         * @return
         */
        public static void remove(String key) {
            remove(SYS_CACHE, key);
        }

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

        /**
         * 写入缓存
         * @param cacheName
         * @param key
         * @param value
         */
        public static void put(String cacheName, String key, Object value) {
            Element element = new Element(key, value);
            getCache(cacheName).put(element);
        }

        /**
         * 从缓存中移除
s         * @param cacheName
         * @param key
         */
        public static void remove(String cacheName, String key) {
            getCache(cacheName).remove(key);
        }

        public static void removeAll(String cacheName) {
            getCache(cacheName).removeAll();
        }

        /**
         * @param cacheName
         * @return
         */
        public static Cache getCache(String cacheName) {
            if (cacheManager == null) {
                synchronized (locks) {
                    if (cacheManager == null) {                       
  
               System.setProperty("net.sf.ehcache.enableShutdownHook","true");
cacheManager = CacheManager.create(); } } } Cache cache = cacheManager.getCache(cacheName); if (cache == null) { cacheManager.addCache(cacheName); cache = cacheManager.getCache(cacheName); //sava all the day cache.getCacheConfiguration().setEternal(true); } return cache; } public static CacheManager getCacheManager() { return cacheManager; } }

 

Test:

@Test
    public void testCache() throws Exception {
        System.setProperty("net.sf.ehcache.enableShutdownHook","true");
        EhCacheUtils ehCacheUtils = new  EhCacheUtils();
        Cache cache = ehCacheUtils.getCache(EhCacheUtils.SYS_CACHE);
        CacheManager cacheManager = ehCacheUtils.getCacheManager();
        ehCacheUtils.put("sea", "sea test ");
        Object object = EhCacheUtils.get("sea");
        System.err.println(object);
    }

 

EhCacheUtils 缓存 ehche (将文件临时保存在磁盘)

标签:pack   获取   shutdown   eal   disk   ble   保留   磁盘   throws   

原文地址:https://www.cnblogs.com/lshan/p/12878116.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!