Ehcache2 的配置(不使用配置文件)
来源:互联网 发布:沈阳优化公司 编辑:程序博客网 时间:2024/05/16 19:49
EhCache是一个开放源码的,基于标准的高速缓存系统。
网上关于EhCache的使用配置很多,但是一般是基于配置文件的。但是实际应用中。我们可能需要动态的管理缓存,这时候单纯配置文件就不够用了。
所以我们需要编码形式的配置创建缓存。
其实EhCache是支持硬编码方式创建配置的(配置文件只是一种形式,最终也是需要解析成JAVA类模型的)。
这里可以比较一下两种创建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>
JAVA中使用缓存
URL url = getClass().getResource("ehache.xml");CacheManager manager = new CacheManager(url);Cache cache = manager.getCache("metaCache");//获得缓存
0 0
- Ehcache2 的配置(不使用配置文件)
- hibernate4.1.7+ehcache2.4.3配置二级缓存
- MyBatis3.4.5整合ehcache2.6.11的步骤
- spring使用注解时配置文件的配置
- 分享一个不使用配置文件的log4cpp的例程
- hibernate4.1.2使用ehcache2.4.3实现二级缓存时报错
- ehcache2.5后hibernate多SessionFactory报错的解决
- ehcache2.5后hibernate多SessionFactory报错的解决
- ehcache2.5后hibernate多SessionFactory报错的解决
- ehcache2.5后hibernate多SessionFactory报错的解决
- ehcache2.10+spring4 整合要注意的问题
- mantis不使用邮件系统(Email)的配置
- Servicemix配置文件的配置
- Servicemix配置文件的配置
- RHEL5.1使用默认配置一键安装的配置文件
- 利用配置文件使用一些可配置的资源
- 在struts配置文件中使用spring配置的bean
- hibernate3使用注解,spring配置文件applicationContext.xml的配置
- 上传图片时,使用JS获得图片文件大小
- JS对象序列化为JSON对象的方法
- 程序开发为什么要使用框架
- 为什么有这么多 Python?Python是解释型的还是编译型的?
- 约定优于配置
- Ehcache2 的配置(不使用配置文件)
- Solr学习01:Solr基础知识
- Solr学习02:搭建Solr环境
- Mybatis热加载Mapper.xml
- 【第九章】 Spring的事务 之 9.1 数据库事务概述 ——跟我学spring3
- 修改eclipse字体
- 安装 RabbitMQ (WINDOWS)
- P3P解决cookie跨域
- Webdings 图形字体