EhCache是一个开放源码的,基于标准的高速缓存系统。
网上关于EhCache的使用配置很多,但是一般是基于配置文件的。
但是实际应用中。我们可能需要动态的管理缓存,这时候可能需要通过程序来动态添加配置创建缓存。
这里可以比较一下两种创建EhCache缓存方式的差异。
第一种方式,不使用配置文件,使用JAVA代码创建配置。
Configuration configuration = new Configuration()// .diskStore(new DiskStoreConfiguration().path("java.io.tmpdir"))//临时文件目录 //指定除自身之外的网络群体中其他提供同步的主机列表,用“|”分开不同的主机 .cacheManagerPeerProviderFactory(new FactoryConfiguration<FactoryConfiguration<?>>()// .className(RMICacheManagerPeerProviderFactory.class.getName())// .properties("peerDiscovery=manual,rmiUrls=//localhost:40004/metaCache|//localhost:40005/metaCache")// )// //配宿主主机配置监听程序 .cacheManagerPeerListenerFactory(new FactoryConfiguration<FactoryConfiguration<?>>()// .className(RMICacheManagerPeerListenerFactory.class.getName())// .properties("port=40004,socketTimeoutMillis=2000")// )// .cache(new CacheConfiguration("metaCache", 10000)//缓存名称(必须唯一),maxElements内存最多可以存放的元素的数量 .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)//清理机制:LRU最近最少使用 FIFO先进先出 LFU较少使用 .timeToIdleSeconds(1000)//元素最大闲置时间 .timeToLiveSeconds(2000)//元素最大生存时间 .eternal(false)//元素是否永久缓存 .diskExpiryThreadIntervalSeconds(120)//缓存清理时间(默认120秒) //LOCALTEMPSWAP当缓存容量达到上限时,将缓存对象(包含堆和非堆中的)交换到磁盘中 //NONE当缓存容量达到上限时,将缓存对象(包含堆和非堆中的)交换到磁盘中 //DISTRIBUTED按照_terracotta标签配置的持久化方式执行。非分布式部署时,此选项不可用 .persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.NONE)).maxEntriesLocalDisk(0)//磁盘中最大缓存对象数0表示无穷大) .cacheEventListenerFactory(new CacheConfiguration.CacheEventListenerFactoryConfiguration().className(RMICacheReplicatorFactory.class.getName()))// ); CacheManager manager = CacheManager.create(configuration); Cache cache = manager.getCache("metaCache");//获得缓存
第二种方式,使用配置文件
配置文件ehache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true"> <diskStore path="java.io.tmpdir" /> <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=manual,rmiUrls=//localhost:40004/metaCache|//localhost:40005/metaCache" /> <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties="port=40004,socketTimeoutMillis=2000" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> </defaultCache> <cache name="metaCache" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="2000" timeToLiveSeconds="1000" overflowToDisk="false"> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" /> </cache> </ehcache>
使用
URL url = getClass().getResource("ehache.xml"); CacheManager manager = new CacheManager(url); Cache cache = manager.getCache("metaCache");//获得缓存
相关推荐
ehcache配置文件里面有大量注解方便解读和理解湖北卫视 2018-10-26 2018-10-26 黄鹤楼酒违法 一般违法 50
ehcache二级缓存配置文件
ehcache缓存的jar包和配置文件。
SpringMVC+Mybatis+Spring+Shiro+ehcache整合配置文件
NULL 博文链接:https://jlwangjinshuang-163-com.iteye.com/blog/1058617
EhCache缓存的配置文件
mybatis 第三方二级缓存,ehcache 配置文件的 约束文件 ehcache.xsd !!
EHCAHCE基于JGROUP的集群配置方案,内含相关配置文件,及配置说明
框架整合配置文件:Spring整合JBPM、日志、数据库、ehcache的配置文件
mybatis整合ehcache的jar包+配置文件
1、springboot 整合ehcache+redis 通过配置文件application.yml切换缓存类型 2、ehcache 、redis 通过缓存管理器管理 3、可分别设置缓存的过期时间 ehcache :添加依赖 pom.xml 2、添加配置文件ehcache.xml 3、添加...
介绍 ehcache的导入过程。使用CacheManager 创建并管理Cache,默认的配置文件为ehcache.xml文件,也可以使用不同的配置。
默认的配置文件为ehcache.xml文件,也可以使用不同的配置: CacheManager manager = new CacheManager("src/config/other.xml"); 缓存的创建,采用自动的方式 CacheManager singletonManager = CacheManager....
NULL 博文链接:https://shikonglaike.iteye.com/blog/1544803
将以下配置copy的ehcache.xml文件的ehcache标签中,注:上述链接中说的配置少写了个probe包名。 , monitorPor t=9889" /> 3.在\ehcache-monitor-kit-1.0.0\etc\ehcache-monitor.conf中可以配置监控的ip和端口号。 4....
详细描述了Ehcache缓存配置,本文将会对ehcache.xml配置文件进行详细的阐述。
ehcache的缓存配置文件
从sebService获得数据,分页显示,如果不用缓存刚每显示都很慢。配置文件自动清空缓存。
本例子主要讲解ehcache的配置使用。采用了java配置和xml配置两种方式。主要用于学习。 使用java配置时将SpringTestCase.java 文件中的@ContextConfiguration(locations = { "classpath:applicationContext.xml" }) ...