ehchache验证缓存过期的api_Ehcache缓存配置

Cache的配置很灵活,官方提供的Cache配置方式有好几种。你可以通过声明配置、在xml中配置、在程序里配置或者调用构造方法时传入不同的参数。

你可以将Cache的配置从代码中剥离出来,也可以在使用运行时配置,所谓的运行时配置无非也就是在代码中配置。以下是运行时配置的好处:

·   在同一个地方配置所有的Cache,这样很容易管理Cache的内存和磁盘消耗。

·   发布时可更改Cache配置。

·   可再安装阶段就检查出配置错误信息,而避免了运行时错误。

本文将会对ehcache.xml配置文件进行详细的阐述。在配置的时可以拷贝一个现有的ehcache.xml.

ehcache.xml片段:

maxElementsInMemory="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="true"maxElementsOnDisk="10000000"diskPersistent="false"diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LRU"/>

ehcache.xml和其他配置文件

在Ehcache-1.6之前的版本,只支持ASCII编码的ehcache.xml配置文件。在Ehcach-1.6之后版本中,支持UTF8编码的ehcache.xml配置文件。因为向后兼容,所有采用ASCII编码的配置文件完全没有必要转换为UTF8。

一个CacheManager必须要有一个XML配置。由于磁盘路径或是监听端口,多个CacheManager使用同一个配置文件时会出现错误。

下面是ehcache.xml具体实例以及配置指南

·   CacheManager配置

DmulticastGroupPort=4446,这样可以配置监听端口。

·   DiskStore配置

如果你使用的DiskStore(磁盘缓存),你必须要配置DiskStore配置项。如果不配置,Ehcache将会使用java.io.tmpdir。

diskStroe的“path”属性是用来配置磁盘缓存使用的物理路径的,Ehcache磁盘缓存使用的文件后缀名是.data和.index。

·   CacheManagerEventListener配置

我们通过CacheManagerEventListenerFactory可以实例化一个CacheManagerPeerProvider,当我们从CacheManager中added和removed

Cache时,将通知CacheManagerPeerProvider,这样一来,我们就可以很方面的对CacheManager中的Cache做一些统计。

注册到CacheManager的事件监听类名有: adding a Cache和removing a Cache

·   CacheManagerPeerProvider配置

在集群中CacheManager配置CacheManagerPeerProviderFactory创建CacheManagerPeerProvider。具体的实例如下:

properties="peerDiscovery=manual,

rmiUrls=//server1:40000/sampleCache1|//server2:40000/sampleCache1|

//server1:40000/sampleCache2|//server2:40000/sampleCache2"

propertySeparator="," />

·   CacheManagerPeerListener配置

CacheManagerPeerListener配置是用来监听集群中缓存消息的分发的。

class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"

properties="hostName=fully_qualified_hostname_or_ip,

port=40001,

socketTimeoutMillis=120000"

propertySeparator="," />

·   Cache配置

·           name:Cache的唯一标识

·           maxElementsInMemory:内存中最大缓存对象数。

·           maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大。

·           eternal:Element是否永久有效,一但设置了,timeout将不起作用。

·           overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中。

·           timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。

·           timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大。

·           diskPersistent:是否缓存虚拟机重启期数据。(这个虚拟机是指什么虚拟机一直没看明白是什么,有高人还希望能指点一二)。

·           diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。

·           diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。

·

memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。这里比较遗憾,Ehcache并没有提供一个用户定制策略的接口,仅仅支持三种指定策略,感觉做的不够理想。

·   Cache Exception Handling配置

class="com.example.ExampleExceptionHandlerFactory"

properties="logLevel=FINE"/>

总结

这里只对通用缓存的配置做了详细的阐述,至于RMI缓存和集群缓存可以参考这里。

下面给出几个配置示例:

·   Ehcache默认Cache配置

·   SampleCache1配置

简单配置,在ehcache.xml文件中有此配置,在使用Ehcache前最好将其删除掉,自己配置。

缓存名sampleCache1,内存中最多可缓存10000个Element,其中的element会在闲置5分钟或是存活10分钟之后失效。

超过10000element时,element将会输出到磁盘中,输出路径是java.io.tmpdir。

·   SampleCache2配置

Cache名为SampleCache2,内存中最多可以缓存1000个element,超出1000不能输出到磁盘中。缓存是永久有效的。

·   SampleCache3配置

Cache名为SampleCache3。可缓存到磁盘。磁盘缓存将会缓存虚拟机重启期的数据。磁盘缓存失效线程运行间隔时间是10分钟。

·   sampleDistributedCache1配置

·   sampleDistributedCache2配置

·   sampleDistributedCache3配置

8f900a89c6347c561fdf2122f13be562.png代码

48304ba5e6f9fe08f3fa1abda7d326ab.png

961ddebeb323a10fe0623af514929fc1.png代码

class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"properties="asynchronousReplicationIntervalMillis=200"/>

48304ba5e6f9fe08f3fa1abda7d326ab.png

48304ba5e6f9fe08f3fa1abda7d326ab.png

961ddebeb323a10fe0623af514929fc1.png代码

48304ba5e6f9fe08f3fa1abda7d326ab.png

48304ba5e6f9fe08f3fa1abda7d326ab.png

961ddebeb323a10fe0623af514929fc1.png代码

Cache名为sampleDistributedCache1。

class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>

class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>

48304ba5e6f9fe08f3fa1abda7d326ab.png

48304ba5e6f9fe08f3fa1abda7d326ab.png

961ddebeb323a10fe0623af514929fc1.png代码

maxElementsInMemory="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="true"diskSpoolBufferSizeMB="30"maxElementsOnDisk="10000000"diskPersistent="false"diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LRU"/>

48304ba5e6f9fe08f3fa1abda7d326ab.png

48304ba5e6f9fe08f3fa1abda7d326ab.png

961ddebeb323a10fe0623af514929fc1.png代码

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442019.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

*【POJ - 3061】 Subsequence (尺取或二分)

题干&#xff1a; A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive eleme…

alert 回调_JavaScript中到底什么时候回调函数Callback

什么是回调函数Callback简单的理解&#xff1a;回调函数是在另一个函数执行完毕后执行的函数 - 因此名称为call back。复杂的理解&#xff1a;在JavaScript中&#xff0c;函数是对象。因此&#xff0c;函数可以将函数作为参数&#xff0c;并且可以由其他函数返回。执行此操作的…

【CF#148B】Escape(模拟)

题干&#xff1a; The princess is going to escape the dragons cave, and she needs to plan it carefully. The princess runs at vp miles per hour, and the dragon flies at vd miles per hour. The dragon will discover the escape after t hours and will chase the…

mysql sql语句分页查询_如何用sql语句 实现分页查询?

我制作了一个数据库其中一张表createtablenews(news_idintprimarykeyidentity(1,1),news_titlevarchar(50)notnull,news_authorvarchar(20),news_summaryvarchar(50),news_contenttext...我制作了一个数据库其中一张表create table news(news_id int primary key identity(1,1)…

*【HDU - 1042 】 N! (大数乘法)

题干&#xff1a; Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in one line, process to the end of file. Output For each N, output N! in one line. Sample Input 1 2 3 Sample Output 1 2 6 解题报告&#xff1a; 大数运…

【bzoj 1754】【POJ - 2389 】Bull Math (高精度运算)

题干&#xff1a; Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls answers. Read in two…

mysql居左查询abcd_数据库--查询语句

查询语句mysql中要学习的知识&#xff1a;多表关系&#xff0c;查询语句&#xff0c;索引添加数据补充将一个查询结果插入到另一张表中create table student(name char(10), gender int);insert into student values(nalituo, 1);insert into student values(sasigi, 0);create…

druid mysql配置详解_druid 参数配置详解

spring.datasource.typecom.alibaba.druid.pool.DruidDataSource#驱动配置信息spring.datasource.driver-class-namecom.mysql.jdbc.Driver#基本连接信息spring.datasource.username rootspring.datasource.password rootspring.datasource.urljdbc:mysql://192.168.153.23:3…

【POJ - 2376】Cleaning Shifts (贪心)

题干&#xff1a; Farmer John is assigning some of his N (1 < N < 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 < T < 1,000,000), …

mysql sql 片段_MySQL代码片段

1.[代码][SQL]代码--导出为xml文件mysql -X -uroot -proot -e "use testa;select * from test_tb;" > /opt/test.xml--导出为csv文件--fields terminated 分割记录中每个字段的字符--optionally enclosed 包围每个字段的字符--lines terminated 每行结束的字符--P…

【HDU - 4990】 Reading comprehension (构造+矩阵快速幂)

题干&#xff1a; Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include <cstring> #include <cmath> #include &…

finereport文本框如何实现多值查询_如何实现参数级联查询

参数级联查询是查询控件之间的一种互动方式&#xff0c;比如在某个下拉框选定选项后&#xff0c;另一个下拉框里的选项范围会随之变化。润乾报表提供了多种编辑风格&#xff0c;每种编辑风格都有丰富的属性&#xff0c;以此为基础实现参数级联查询也很简单。下面就通过一个例子…

【HDU - 5015 】233 Matrix (矩阵快速幂)

题干&#xff1a; In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 233…

【HDU - 2899】 Strange fuction(二分或三分,求导)

题干&#xff1a; Now, here is a fuction: F(x) 6 * x^78*x^67*x^35*x^2-y*x (0 < x <100) Can you find the minimum value when x is between 0 and 100. Input The first line of the input contains an integer T(1<T<100) which means the number of…

php mysql html标签_HTML标签格式化PHP和MySQL

我有这个MySQL语句Select type.type, color.color, ShotName, Item.name, Item.Item_idFrom typeInner Join ItemOn type.type_id Item.type_idInner Join colorOn color.color_id Item.color_idWhere Item.state0 And Item.offline 0Group By color.color, Item.name, type.o…

【CF#706B】 Interesting drink (二分)

题干&#xff1a; 瓦西里喜欢在努力工作后休息&#xff0c;所以你可能经常在附近的一些酒吧见到他。他喜欢 "Beecola"&#xff0c;可以从 n 个不同的商店买到。在第 i 个商店的价格为 xi 元。 瓦西里计划购买他最喜欢的饮料 q 次。在第 i 天他能花 mi 元。他想知道每…

mysql datetime month不走索引_like百分号加前面一定不走索引吗?一不小心就翻车,关于mysql索引那些容易错的点...

like百分号加前面一定不走索引吗&#xff1f;正常来讲&#xff0c;我们都知道在mysql的like查询中&#xff0c;百分号加在关键词后面是走索引的&#xff0c;比如 select * like "张三%"&#xff0c;而百分号在前面是不走索引的&#xff0c;比如 select * like "…

ACM竞赛、数论内容常用的定理(求解(a/b)%c,乘法逆元,费马小定理)

如果b与c互素&#xff0c;则(a/b)%ca*b^((c)-1)%c其中是欧拉函数。或者(a/b)%ca*b^(c-2)%c 如果b与c不互素&#xff0c;则(a/b)%c(a%bc)/b 对于b与c互素和不互素都有(a/b)%c(a%bc)/b成立 乘法逆元用扩展欧几里得定理&#xff1a; 例题&#xff1a;ZOJ - 3609 题干&#xf…

自定义菜单url不能带_微服务架构【SpringBoot+SpringCloud+VUE】五 || 实战项目微信公众号自定义开发...

本章主要讲解微信公众号自定义菜单、微信网页开发、模板消息推送等功能的实现&#xff1b;发福利了&#xff0c;下方关注公众号&#xff0c;就能免费获取项目源码1、自定义菜单开发前需要了解以下几点&#xff1a;1、微信公众号的自定义菜单最多包括3个一级菜单&#xff0c;每个…

C语言编程中关于负数的%运算的判定。

如果 % 两边的操作数都为正数&#xff0c;则结果为正数或零&#xff1b;如果 % 两边的操作数都是负数&#xff0c;则结果为负数或零。C99 以前&#xff0c;并没有规定如果操作数中有一方为负数&#xff0c;模除的结果会是什么。C99 规定&#xff0c;如果 % 左边的操作数是正数&…