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");//获得缓存

Ehcache2 的配置(不使用配置文件)的更多相关文章

  1. Web.Config Transformation配置灵活的配置文件

    使用Web.Config Transformation配置灵活的配置文件 发布Asp.net程序的时候,开发环境和发布环境的Web.Config往往不同,比如connectionstring等.如果常 ...

  2. centos yum源配置 与yum配置文件

    参考博客 http://www.cnblogs.com/mchina/archive/2013/01/04/2842275.html 1.centos . yum配置文件在目录 /etc/yum.re ...

  3. JAVAEE——SpringBoot配置篇:配置文件、YAML语法、文件值注入、加载位置与顺序、自动配置原理

    转载 https://www.cnblogs.com/xieyupeng/p/9664104.html @Value获取值和@ConfigurationProperties获取值比较   @Confi ...

  4. PLSQL连接ORACLE配置字符串简介 oracle网络配置 三个配置文件 listener.ora、sqlnet.ora、tnsnames.ora原理解释

    PLSQL连接ORACLE配置字符串简介 oracle网络配置 三个配置文件 listener.ora.sqlnet.ora.tnsnames.ora原理解释 oracle网络配置三个配置文件 lis ...

  5. 详解Apache服务与高级配置,(主配置文件每行都有描述)

    HTTP服务---> http://httpd.apache.org/(官方网站) httpd  service :纯粹的web服务器,同时开源(不是GPL). 特性:1.在进程特性上通常是事先 ...

  6. Spring Boot配置,读取配置文件

    Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...

  7. spring boot自定义类配置绑定在配置文件中自动提示

    在spring boot的日常使用中,我们可能需要使用配置绑定的方式动态配置自定义类的成员变量. 这个时候,我们在配置文件中配置spring默认已有的配置时,只需要输入部分关键字即可自动提示,如下图: ...

  8. SSM框架的配置整合(包含配置文件代码)

    由于SSM框架学习都要去网上或者以前的项目拷贝相同的代码,所以我在此把自己用到的配置文件全放在这里,帮助自己,帮助别人 首先开始前导入依赖和处理静态资源导出问题 <dependencies> ...

  9. Spring配置文件集成Hibernate配置文件

      Spring对hibernate配置文件hibernate.cfg.xml的集成,来取代hibernate.cfg.xml的配置.  spring对hibernate配置文件hibernate.c ...

随机推荐

  1. Java反转单链表

    class Node { private int data; private Node nextNode; public Node(int data) { this.data = data; } pu ...

  2. SQL Server CPU时间和占用时间及优化

    如何测试sql语句执行时间 在MSSQL Server中通过查看SQL语句执行所用的时间,来衡量SQL语句的性能. set statistics profile on set statistics i ...

  3. (转)如何将本地git仓库中的代码上传到github

    1,  在github上新建一个仓库,比如为:CSS3Test,仓库地址为:https://github.com/hyuanyuanlisiwei/CSS3Test 2,本地git仓库中的文件项目为C ...

  4. 页面嵌入QQ功能(点QQ建立一个临时会话,显示在页面的固定位置)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. JList的基本操作

    1.初始化并添加元素DefaultListModel leftListModel=new DefaultListModel();String[] items = Model.getPairs();fo ...

  6. MyEclipse 集成 Gradle开发环境

    一.上Grandle官网下载Gradle,地址:http://www.gradle.org/downloads 如果只是运行只下载gradle-2.6-bin.zip 就可以了,如果为了扩展开发的话就 ...

  7. php抽象与接口的区别[转载]

    来自:http://www.cnblogs.com/k5054/archive/2012/12/26/2834205.html 对于面向对象开发,抽象类与接口这两个东西是比较难理解的! 今天看了一整天 ...

  8. Android AES加密算法及其实现

    找到了AES加密算法.(当然还有MD5,BASE64什么的http://snowolf.iteye.com/blog/379860这篇文章列举了很多,但是基本都是j2se平台的,android平台不一 ...

  9. Android colors.xml 颜色列表

    android 常用项 <?xml version="1.0" encoding="utf-8"?> <resources> <c ...

  10. Python中的关键字的用法

    Python有哪些关键字 -Python常用的关键字 and, del, from, not, while, as, elif, global, or, with, assert, else, if, ...