java缓存Ehcache的使用

一、简介
  EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点。Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。

     主要的特性有:

 快速
 简单
 多种缓存策略
缓存数据有两级:内存和磁盘,因此无需担心容量问题
 缓存数据会在虚拟机重启的过程中写入磁盘
可以通过RMI、可插入API等方式进行分布式缓存
 具有缓存和缓存管理器的侦听接口
支持多缓存管理器实例,以及一个实例的多个缓存区域
提供Hibernate的缓存实现
二、详细配置
EhCache 给我们提供了丰富的配置来配置缓存的设置。

cache元素的属性:  

    name:缓存名称  

    maxElementsInMemory:内存中最大缓存对象数  

    maxElementsOnDisk:硬盘中最大缓存对象数,若是0表示无穷大  

    eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false  

    overflowToDisk:true表示当内存缓存的对象数目达到了maxElementsInMemory界限后,会把溢出的对象写到硬盘缓存中。注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行。

  diskSpoolBufferSizeMB:磁盘缓存区大小,默认为30MB。每个Cache都应该有自己的一个缓存区。  

    diskPersistent:是否缓存虚拟机重启期数据  

    diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认为120秒  

    timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态  

    timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义  

    memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。

三、持久化到硬盘
在一些系统中,使用EhCache来缓存数据的时候,有时是不需要持久化到硬盘的,但是在爬虫系统,就需要将数据 持久化到硬盘,用来判断URL是否重复,也就是对爬取过去的URL进行过滤,避免重复爬取。

  参考配置:

<?xml version="1.0" encoding="UTF-8"?>
 
<ehcache>
   <!-- 
         磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
          path:指定在硬盘上存储对象的路径
   -->
   <diskStore path="C:\ehcache" />
    
   <!-- 
        defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
        maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
        eternal:代表对象是否永不过期
        overflowToDisk:当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中
   -->
   <defaultCache
      maxElementsInMemory="100"
      eternal="true"
      overflowToDisk="true"/>
 
    <!-- 
        maxElementsInMemory设置成1,overflowToDisk设置成true,只要有一个缓存元素,就直接存到硬盘上去
        eternal设置成true,代表对象永久有效
        maxElementsOnDisk设置成0 表示硬盘中最大缓存对象数无限大
        diskPersistent设置成true表示缓存虚拟机重启期数据 
     -->
    <cache 
      name="a"
      maxElementsInMemory="1" 
      eternal="true"
      overflowToDisk="true" 
      maxElementsOnDisk="0"
      diskPersistent="true"/>
 
</ehcache>
四、实例
  此处使用的EhCache版本为2.X,此版本比较稳定。

1.pom.xml

<dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>2.10.4</version>
    </dependency>
2.代码实现

public static void main(String[] args) {
        String path = System.getProperty("user.dir");
        // 创建Cache管理器
        CacheManager manager = CacheManager.create(path + "\\src\\main\\resources\\ehcache.xml");
        // 获取指定Cache
        Cache cache = manager.getCache("a");
        // 把一个元素添加到Cache中
        cache.put(new Element("name", "Joe"));
        // 根据Key获取缓存元素
        Element ele = cache.get("name");
        System.out.println("name==" + ele.getObjectValue());
        
        cache.flush(); // 刷新缓存
        manager.shutdown();  // 关闭缓存管理器
    }
3.查看C盘中的ehcache文件夹,有如下文件,说明缓存到磁盘中了。
原文链接:https://blog.csdn.net/qq_42969074/article/details/85642873

使用代码编写的方式使用ehcache:

准备一个可用的maven项目:并加入依赖;

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

首先您得准备一些ehcache的配置文件:

 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="java.io.tmpdir/Tmp_EhCache" />
    
    <!-- defaultCache,是默认的缓存策略 -->
    <!-- 如果你指定的缓存策略没有找到,那么就用这个默认的缓存策略 -->
    <!-- external:缓存对象是否一直存在,如果设置为true的话,那么timeout就没有效果,缓存就会一直存在,一般默认就是false -->
    <!-- maxElementsInMemory:内存中可以缓存多少个缓存条目 -->
    <!-- overflowToDisk:如果内存不够的时候,是否溢出到磁盘 -->
    <!-- diskPersistent:是否启用磁盘持久化的机制,在jvm崩溃的时候和重启之间 -->
    <!-- timeToIdleSeconds:对象最大的闲置的时间,如果超出闲置的时间,可能就会过期  单位:秒 当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大-->
    <!-- timeToLiveSeconds:对象最多存活的时间  单位:秒 当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是存活时间无穷大-->
    <!-- memoryStoreEvictionPolicy:当缓存数量达到了最大的指定条目数的时候,需要采用一定的算法,从缓存中清除一批数据,LRU,最近最少使用算法,最近一段时间内,最少使用的那些数据,就被干掉了 -->
    <defaultCache
        eternal="false"
        maxElementsInMemory="1000"
        overflowToDisk="false"
        diskPersistent="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="0"
        memoryStoreEvictionPolicy="LRU" />
 
     <!-- 手动指定的缓存策略 -->
     <!-- 对不同的数据,缓存策略可以在这里配置多种 -->
    <cache
        name="local"  
        eternal="false"
        maxElementsInMemory="1000"
        overflowToDisk="false"
        diskPersistent="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="0"
        memoryStoreEvictionPolicy="LRU" />    
</ehcache>
b.在编码中读取ehcache配置文件,并得到EhCacheCacheManager操作:

import net.sf.ehcache.Cache;
     import net.sf.ehcache.CacheManager;
     import net.sf.ehcache.Element;
@org.junit.Test  
        public void testEhcache (){  
            CacheManager create = CacheManager.create(this.getClass().getResourceAsStream("/ehcache.xml"));
            Cache cache = create.getCache("local");
            cache.put(new Element("key1", "value1"));
            Element element = cache.get("key1");
            System.out.println(element.getObjectKey()+" : "+element.getObjectValue());
            
        }

如此,可以自己封装到单例中作为工具提供。

2.使用springboot注解方法:

 准备工作:一个可以正常使用的springboot项目:也要添加上面的ehcache依赖和ehcache配置 。

a.读取ehcache配置 生成EhCacheCacheManager:

EhcacheConfiguration.java:


import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
 
@Configuration
@EnableCaching
public class EhcacheConfiguration {
    @Bean
    public EhCacheManagerFactoryBean cacheManagerFactoryBean(){
        EhCacheManagerFactoryBean bean = new EhCacheManagerFactoryBean();
        bean.setConfigLocation(new ClassPathResource("ehcache.xml"));
        bean.setShared(true);
        return bean;
    }
    @Bean
    public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
      return new EhCacheCacheManager(bean.getObject());
    }    
}
在service中使用ehcache:
EhcahceServiceImpl.java:


@Service
public class EhcahceServiceImpl implements EhcahceService {
 
    private Log log = LogFactory.getLog(EhcahceServiceImpl.class);
//    @Resource
//    private JedisCluster cluster;
    @Resource
    private Jedis cluster;
    private static final String CACHE_STRATEGY = "local";
    
    @CachePut(value=CACHE_STRATEGY,key="'key_'+#info.getProduct_id()")
    @Override
    public ProductInfo saveProductInfo(ProductInfo info) throws Exception {
        return info;
    }
    //@CacheEvict(value="myCache", key="'get'+#userNo")  
    allEntries为true表示清除value中的全部缓存,默认为false  
   // @CacheEvict(value="myCache", allEntries=true)  
    @Cacheable(value=CACHE_STRATEGY,key="'key_'+#id")
    @Override
    public ProductInfo getProductInfoById(Long id) throws Exception {
        return null;
    }}
到此我们就可以简单的使用ehcache到我们的项目中了。ehcache被更多的作为本地的缓存机制,但是在业务复杂频频的系统中,单靠ehcache貌似有些吃力,这时我们可以加上Redis,
让Redis做为分布式缓存,ehcache做本地的缓存,形成拥有2种缓存的系统。在一个请求被路由到某台服务上时,系统先去Redis中找,Reids中有

对应的缓存就返回,没有的话再从本地的Ehcache中找,找到则返回,找不到就从数据库中查询了,查询了之后我们可以保持在Redis

和本地的Ehcache中。
原文链接:https://blog.csdn.net/u014104286/article/details/79125141

如下也可直接注入CacheManager对象缓存

@Component
public class EhcacheUtil {
    /**
     * 管理员菜单缓存
     */
    public final static String ADMINMENUEHCACHENAME = "adminMenuCache";

    @Resource
    public CacheManager manager;

    /**
     * 获得一个Cache
     *
     * @param cacheName 缓存名
     * @param key       缓存key
     * @return Object
     */
    public Object get(String cacheName, Object key) {
        Cache cache = manager.getCache(cacheName);
        if (cache != null) {
            Element element = cache.get(key);
            if (element != null) {
                return element.getObjectValue();
            }
        }
        return null;
    }

    /**
     * 添加一个Cache
     *
     * @param cacheName 缓存名
     * @param key       缓存key
     * @param value     缓存value
     */
    public void put(String cacheName, Object key, Object value) {
        Cache cache = manager.getCache(cacheName);
        if (cache != null) {
            cache.put(new Element(key, value));
        }
    }

    /**
     * 移除一个Cache
     *
     * @param cacheName 缓存名
     * @param key       缓存key
     * @return boolean
     */
    public boolean remove(String cacheName, Object key) {
        Cache cache = manager.getCache(cacheName);
        if (cache != null) {
            return cache.remove(key);
        }
        return false;
    }

}

具体使用见:https://www.cnblogs.com/sunxucool/p/3159076.html

猜你喜欢

转载自blog.csdn.net/zpflwy1314/article/details/106913279