java ehcache使用_java中使用Ehcache缓存数据

本文介绍了在Java项目中如何使用Ehcache进行数据缓存,包括在pom.xml中添加依赖,配置ehcache.xml文件,以及创建并使用Cache对象的示例代码。
摘要由CSDN通过智能技术生成

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

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

(1)概述

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

本文基于2.10.X以上版本

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

net.sf.ehcache

ehcache

org.springframework

spring-context-support

(3)HelloWorld实例使用Ehcache缓存

1.在classpath下添加ehcache.xml配置文件,添加一个名为helloworld的缓存

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

xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

maxEntriesLocalHeap="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

maxEntriesLocalDisk="10000000"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU"/>

maxElementsInMemory="1000"

eternal="false"

timeToIdleSeconds="5"

timeToLiveSeconds="5"

overflowToDisk="false"

memoryStoreEvictionPolicy="LRU"/>

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

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());

}

}

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

输出:

55697e6a94d6cd3f4c70db9ae6a6c515.png

(4)Ehcache基本操作Element、Cache、cacheManager是Ehcacle最重要的API

Element:缓存的元素,维护着一个键值对

Cache:是Ehcache的核心类,他有多个Element,并被CacheManager管理,它实现了对缓存的逻辑行为

CacheManager:Cache的容器对象, 并管理着Cache的生命周期。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值