java中使用Ehcache缓存数据

知识点:在java项目中,使用ehcache缓存数据

参考博客:http://www.cnblogs.com/jingmoxukong/p/5975994.html

1)概述

Ehcache是一个纯Java的进程内缓存框架,具有快速‘精干等特点。

本文基于2.10.X以上版本

(2)在pom.xml添加相关包依赖

<!--  ehcache缓存包-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>

<!-- spring-context-support包含有Spring对于缓存功能的抽象封装接口-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>

(3)HelloWorld实例使用Ehcache缓存

1.在classpath下添加ehcache.xml配置文件,添加一个名为helloworld的缓存
--------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<!-- 磁盘缓存位置 -->
<diskStore path="java.io.tmpdir/ehcache"/>

<!-- 默认缓存 -->
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>

<!-- helloworld缓存 -->
<cache name="helloworld"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="5"
timeToLiveSeconds="5"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>

</ehcache>
-------------------------------------------------------------------------------------------------------

ehcache.xml配置参数说明:

  • name:缓存名称。
  • maxElementsInMemory:缓存最大个数。
  • eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
  • timeToIdleSeconds:置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
  • timeToLiveSeconds:缓存数据的生存时间(TTL),也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
  • maxEntriesLocalDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
  • overflowToDisk:内存不足时,是否启用磁盘缓存。
  • diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
  • maxElementsOnDisk:硬盘最大缓存个数。
  • diskPersistent:是否在VM重启时存储硬盘的缓存数据。默认值是false。
  • diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
  • memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
  • clearOnFlush:内存数量最大时是否清除。


-------------------------------------------------------------------------------------------------------

2.EhcacheDemo.java文件
Ehcache会自动加载classpath根目录下名为ehcache.xml文件。
EhcacheDemo的工作步骤如下:
在EhcacheDemo中,我们引用ehcache.xml声明的名为helloworld的缓存来创建Cache对象;
然后我们用一个键值对来实例化Element对象;
Element对象添加到Cache
然后用Cache的get方法获取Element对象。
----------------------------------------------------------------------------------------
package com.agesun.attendance.web.controller;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class EhcacheDemo {

public static void main(String[] args){
       // Create a cache manager 创建缓存管理者
final CacheManager cacheManager=new CacheManager();
// create the cache called "helloworld" 引用ehcache.xml申明的的名为helloworld的缓存创建Cache对象
final Cache cache=cacheManager.getCache("helloworld");
// create a key to map the data to
final String key="greeting";
// Create a data element
final Element putGreeting=new Element(key,"Hello,World!");
// Put the element into the data store //将map对象放到cache缓存里
cache.put(putGreeting);
// Retrieve the data element //从cache对象中获得到元素
final Element getGreeting=cache.get(key);
// Retrieve the data element
System.out.println(getGreeting.getObjectValue());
    }

}
----------------------------------------------------------------------------------

输出:


(4)Ehcache基本操作

Element、Cache、cacheManager是Ehcacle最重要的API

Element:缓存的元素,维护着一个键值对
Cache:是Ehcache的核心类,他有多个Element,并被CacheManager管理,它实现了对缓存的逻辑行为
CacheManager:Cache的容器对象, 并管理着Cache的生命周期。






猜你喜欢

转载自www.cnblogs.com/shuaifing/p/9188436.html