Ehcache缓存框架如何在Java项目中使用

  介绍

今天就跟大家聊聊有关Ehcache缓存框架如何在Java项目中使用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

<强>前言

Java缓存实现方案有很多,最基本的自己使用地图去构建缓存,或者使用memcached或复述,但是上述两种缓存框架都要搭建服务器,而地图自行构建的缓存可能没有很高的使用效率,那么我们可以尝试一下使用Ehcache缓存框架。

Ehcache主要基于内存缓存,磁盘缓存为辅的,使用起来方便。下面介绍如何在项目中使用Ehcache

<强>入门使用教程

1。maven引用

& lt; dependency>   ,& lt; groupId> net.sf.ehcache   ,& lt; artifactId> ehcache   ,& lt; version> 2.10.4   & lt;/dependency>

2。在类路径下建立一个ehcache。xml

& lt; ? xml  version=?.0“,编码=癠TF-8" ?比;   & lt; ehcache>   & lt; !——timeToIdleSeconds 当缓存闲置n秒后销毁,——祝辞,   & lt; !——timeToLiveSeconds 当缓存存活n秒后销毁,——祝辞,   & lt; !,,   缓存配置,   ,,,的名字:缓存名称只   ,,,maxElementsInMemory:缓存最大个数只   ,,,永恒的:对象是否永久有效,一但设置了,超时将不起作用只   ,,,timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当永恒=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大只   ,,,timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当永恒=false对象不是永久有效时使用,默认是0。,也就是对象存活时间无穷大只   ,,,overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中只   ,,,diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30 mb。每个缓存都应该有自己的一个缓冲区只   ,,,maxElementsOnDisk:硬盘最大缓存个数只   ,,,diskPersistent:是否缓存虚拟机重启期数据,Whether 从而disk  store  persists 结构;restarts  of 从而Virtual 机器只,default  value  is 虚假又是;   ,,,diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒只   ,,,memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)只   ,,,clearOnFlush:内存数量最大时是否清除又是;   ——比;   ,,& lt; !——,磁盘缓存位置,——比;   & lt;才能diskStore 路径=癹ava.io.tmpdir/easylink-mall-web/ehcache"/比;   & lt;才能!——,默认缓存,——比;   & lt; defaultCache才能   ,,,,,maxElementsInMemory=?0000”;   ,,,,,永恒=癴alse"   ,,,,,timeToIdleSeconds=?20”;   ,,,,,timeToLiveSeconds=?20”;   ,,,,,maxElementsOnDisk=?0000000”;   ,,,,,diskExpiryThreadIntervalSeconds=?20”;   ,,,,,memoryStoreEvictionPolicy=癓RU"比;   ,,,& lt; persistence 策略=發ocalTempSwap"/比;   & lt;才能/defaultCache>   & lt;才能!——,商户申请数据缓存,数据缓存40分钟,——比;   & lt;才能缓存   ,,,,,的名字=癿erchant-apply-cache"   ,,,,,永恒=癴alse"   ,,,,,timeToIdleSeconds=?400”;   ,,,,,timeToLiveSeconds=?400”;   ,,,,,maxEntriesLocalHeap=?0000”;   ,,,,,maxEntriesLocalDisk=?0000000”;   ,,,,,diskExpiryThreadIntervalSeconds=?20”;   ,,,,,overflowToDisk=癴alse"   ,,,,,memoryStoreEvictionPolicy=癓RU"比;   & lt;才能/cache>   & lt;/ehcache>

3。与春天的缓存管理器结合使用

& lt; ? xml  version=?.0“,编码=癠TF-8" ?比;   http://www.springframework.org/schema/beans" & lt; beans  xmlns=?;   ,,xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"   xmlns:才能缓存=癶ttp://www.springframework.org/schema/cache"   ,,xsi: schemaLocation=?   ,,,,http://www.springframework.org/schema/beans   ,,,,http://www.springframework.org/schema/beans/spring-beans.xsd   ,,,,http://www.springframework.org/schema/cache   ,,,,http://www.springframework.org/schema/cache/spring-cache.xsd"比;      & lt; !——,才能支持缓存注解,——比;   & lt;才能缓存:annotation-driven 缓存管理器=癱acheManager",/比;      & lt;才能!——,默认是cacheManager ——比;   & lt;才能bean  id=癱acheManager",类=皁rg.springframework.cache.ehcache.EhCacheCacheManager"比;   ,,,& lt; property  name=癱acheManager", ref=癱acheManagerFactory"/比;   & lt;才能/bean>      & lt;才能!——,缓存管理器配置,——比;   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Ehcache缓存框架如何在Java项目中使用