什么时候可以使用莱特币的网站Ehcache缓存

timeToLiveSeconds和timeToIdleSecondstimeToLiveSeconds=x:缓存自创建日期起至失效时的间隔时间x;timeToIdleSeconds=y:缓存创建以后,最后一次访问缓存的日期至失效之时的时间间隔y;
如果仅有timeToLiveSeconds那么自创建时间开始 间隔x后缓存失效;如果没有timeToLiveSeconds那么自最后一次访问缓存 间隔y后 缓存失效;如果既有timeToLiveSeconds也有timeToIdleSeconds那么取最小数算作间隔时间;min(x,y);经过测试其计算原则是:若自创建缓存后一直都没有访问缓存,那么间隔x后失效,若自创建缓存后有N次访问缓存,那么计算(最后一次访问缓存时间+y ) 即:按照timeToIdleSeconds计算,但总存活时间不超过
举例:timeToIdleSeconds=120;timeToLiveSeconds=180;表示此缓存最多可以存活3分钟,如果期间超过2分钟未访问 那么此缓存失效
转自:http://blog.csdn.net/vtopqx/article/details/8522333
阅读(...) 评论()&& &文章主题:
交流经验:
总积分:261656
级别:VIP5
文章: 1163
参考:&1. EHCache 的特点,是一个纯Java ,过程中(也可以理解成插入式)缓存实现,单独安装Ehcache ,需把ehcache-X.X.jar 和相关类库方到classpath中。如项目已安装了Hibernate ,则不需要做什么。。直接可以使用Ehcache&Cache 存储方式 :内存或磁盘&2. 单独使用 EHCache&使用CacheManager 创建并管理Cache&1.创建CacheManager有4种方式:&
A:使用默认配置文件创建&
java代码:
CacheManager manager = CacheManager.create();
B:使用指定配置文件创建&
java代码:
CacheManager manager = CacheManager.create(&src/config/ehcache.xml&);
C:从classpath中找寻配置文件并创建&
java代码:
URL url = getClass().getResource(&/anothername.xml&);
CacheManager manager = CacheManager.create(url);
D:通过输入流创建
java代码:
InputStream fis = new FileInputStream(new File(&src/config/ehcache.xml&).getAbsolutePath());
manager = CacheManager.create(fis);
} finally {
fis.close();
卸载CacheManager ,关闭Cache&
java代码:
manager.shutdown();
使用Caches&取得配置文件中预先 定义的sampleCache1设置,通过CacheManager生成一个Cache&
java代码:
Cache cache = manager.getCache(&sampleCache1&);
设置一个名为test 的新cache,test属性为默认&
java代码:
CacheManager manager = CacheManager.create();
manager.addCache(&test&);
设置一个名为test 的新cache,并定义其属性&
java代码:
CacheManager manager = CacheManager.create();
Cache cache = new Cache(&test&, 1, true, false, 5, 2);
manager.addCache(cache);
往cache中加入元素&
java代码:
Element element = new Element(&key1&, &value1&);
cache.put(new Element(element);
从cache中取得元素
java代码:
Element element = cache.get(&key1&);
所以大概步骤为:&第一步:生成CacheManager对象&第二步:生成Cache对象&第三步:向Cache对象里添加由key,value组成的键值对的Element元素&具体一个Test.java程序:&
java代码:
import net.sf.ehcache.C
import net.sf.ehcache.CacheM
import net.sf.ehcache.E
* 第一步:生成CacheManager对象
* 第二步:生成Cache对象
* 第三步:向Cache对象里添加由key,value组成的键值对的Element元素
* @author mahaibo
public class Test {
public static void main(String[] args) {
//指定ehcache.xml的位置
String fileName=&E:\\1008\\workspace\\ehcachetest\\ehcache.xml&;
CacheManager manager = new CacheManager(fileName);
//取出所有的cacheName
String names[] = manager.getCacheNames();
for(int i=0;i&names.i++){
System.out.println(names[i]);
//根据cacheName生成一个Cache对象
//第一种方式:
Cache cache=manager.getCache(names[0]);
//第二种方式,ehcache里必须有defaultCache存在,&test&可以换成任何值
Cache cache = new Cache(&test&, 1, true, false, 5, 2);
manager.addCache(cache);
//向Cache对象里添加Element元素,Element元素有key,value键值对组成
cache.put(new Element(&key1&,&values1&));
Element element = cache.get(&key1&);
System.out.println(element.getValue());
Object obj = element.getObjectValue();
System.out.println((String)obj);
manager.shutdown();
3. 在 Hibernate 中运用EHCache ,
这篇文章被编辑了 2 次. 最近一次更新是在
精品视频课程推荐
深入浅出的讲解JavaBen的写法、JavaBean的用法、JavaBean的实现机制、JavaBean对应翻译的代码理解。
Hibernate 注解零配置,包括@Id、@Column、@OneToMany等
达到能综合使用Struts2+Spring3+Hibernate3+Jbpm4来进行实际项目开发的能力。
包括:ssh和jbpm的整合;数据字典;通用DAO(Spring+Hibernate+泛型+反射+SpEL+模板方法模式);自动生成UUID的加强版;分层开发、SSH联合的基本开发;翻页的示范真实值和表现值,数据参照的实现;文件上传下载;主子表操;登录验证码;登录控制的拦截器
数据校验、Javascript模拟多线程、下拉列表联动、操作XML、AJAX结合JSON的操作、Json-lib的使用
创建规范的XML文档,DTD的作用,并且可以根据要求创建私用的DTD,通过JavaScript解析XML DOM
选择一个版面
软件设计专版
Web前端技术
学习问题讨论
面试、就业
版权所有 Copyright(C) 私塾在线学习网EHcache缓存配置说明 - JAVA博客 - ITeye博客
博客分类:
配置文件例子:
ehcache&
&&& &diskStore path="java.io.tmpdir"/&
&&& &defaultCache
&&&&&&&&&&& maxElementsInMemory="10000"
&&&&&&&&&&& eternal="false"
&&&&&&&&&&& timeToIdleSeconds="120"
&&&&&&&&&&& timeToLiveSeconds="120"
&&&&&&&&&&& overflowToDisk="true"
&&&&&&&&&&& maxElementsOnDisk=""
&&&&&&&&&&& diskPersistent="false"
&&&&&&&&&&& diskExpiryThreadIntervalSeconds="120"
&&&&&&&&&&& memoryStoreEvictionPolicy="LRU"
&&&&&&&&&&& /&
&/ehcache&
1. 预览
&& Ehcache 从 Hibernate 发展而来,逐渐涵盖了Cahce界的全部功能,是目前发展势头最好的一个项目。
分布式缓存(基于RMI/JGroups/JMS)
URL,页面片段缓存(类似OSCache的相关部分)
中央缓存服务器(类似Memcached)
2. 基本功能与配置
&&&& Ehcache的基本功能,可以从配置文件中学习。详见Ehcache的文档
&&&& 总的来说,缓存与HashMap的最大不同,就是缓存设想内存是有限的,缓存的时效性也是有限的,所以可以设定内存数量的大小,可以执行失效算法 ,可以在内存满了的时候,按照最少访问等算法将缓存直接移除或切换到硬盘上。
&&&& 另外注意,ehcache的CacheManager本身有一定的默认值。而在没有指定ehcache.xml的情况下, 会使用ehcache.jar里自带的ehcache_failsafe.xml,
2.1 对象在内存中的最大数量
&&&& 因为内存是有限的,所以必须用maxElementsInMemory(必填项)设置每类对象在内存中的最大数量。ehcache_failsafe.xml 中为10000。
2.2 到达内存中最大量时的过期/移出算法
&&&&& 过期算法: 如果缓存已经失效,人道毁灭之。失效算法由3个参数组成:
eternal(必填项):如果为true,则永不过期,忽略后两个参数的设置。ehcache_failsafe.xml 为false.
timeToIdleSeconds: 空闲无访问时间,默认为0,永不过时。ehcache_failsafe.xml 设为120秒。
timeToLiveSeconds: 空闲无访问时间,默认为0,永不过时。ehcache_failsafe.xml 设为120秒。
&&&&& 移出算法:如果经过失效算法后,还是有很多有效的缓存,则执行清除算法。清除算法由两个参数组成:
memoryStoreEvictionPolicy: 默认为LRU(最近最少访问),另有先进先出(FIFO),最少访问次数(LFU)
overflowToDisk(必填项) 为true,则将清除出来的缓存持久化到磁盘,否则人道毁灭之。
2.3 储存到硬盘
maxElementsOnDisk,默认为0,无限多。ehcache_failsafe.xml为。
diskExpiryThreadIntervalSeconds:使用过期算法清除磁盘中失效对象的间隔,默认为120秒。&
diskSpoolBufferSizeMB ,默认为30M。
2.4& 重启时缓存持久化
diskPersistent 当应用重启时,可将缓存先持久化到硬盘,重启后再行载入,节省大量的重新从数据库载入。但只适合那些缓存不怎么变化,或者有特殊机制保证重启后应用能接收到重启这段时间里缓存变化信息的情况。
3. 分布式缓存
&&&& Ehcache 有传统的RMI,1.5版的JGroups,1.6版的JMS,随大流还是先用RMI的好些。
&& 3.1设置自身
&&&&& 这里设置在localhost的40001端口上侦听。如果要互相同步的CahceManager不都在一台机器上的话,hostName应该是实际IP。)
&cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=localhost, port=40001,socketTimeoutMillis=2000" /&&
&& 3.2 设置需要同步的对方服务器及缓存对象,
&&& 这里设置与40002端口上的CacheManager同步User与Role对象,如果还有第三台机器,则继续用|分割,继续往下列。同理,在40002端口上的cacheManager的ehcache.xml里,就需要配置与03的互通)
&&& 也有自动发现,广播的简单配法,但对广播天然恐惧,还是辛苦一点一个个静态列表配置文件的写了,虽然有点烦。
&cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//localhost:40002/org.springside.examples.miniweb.entity.user.User|//localhost:40002/org.springside.examples.miniweb.entity.user.Role"/&&&
3.3 缓存对象的配置
&& 往每一个需要缓存的对象加入子对象cacheEventListenerFactory
&&&& &cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
&&&&&&&& properties="replicateAsynchronously=true,
&&&&&&&& replicatePuts=true,
&&&&&&&& replicateUpdates=true,
&&&&&&&& replicateUpdatesViaCopy=true,
&&&&&&&& replicateRemovals=true
&&&&&&&& asynchronousReplicationIntervalMillis=&number of milliseconds"&
&&&&&&&& propertySeparator="," /&
replicateAsynchronously& 对象同步是否异步完成,默认为true。如果比较紧急就设为false。 在一致性时间性要求不强的时候,设为异步可大大提供性能,因为它是异步立即返回的,而且可以批量提交。
replicateUpdatesViaCopy 是否将对象变更复制到所有节点,还是只是发送一个失效信息,让对方该缓存失效,当对方需要该缓存时重新计算载入。
默认为true。鉴于对象复制的消耗挺大的,又有锁的问题,而且对方也未必需要该对象,所以此属性建议设为false。如果业务上真的需要设为true时,就可考虑使用Terracotta了。
replicatePuts、replicateUpdates、replicateRemovals& 增删改是否同步,默认都为true。但因为我们前面选择了失效算法,所以replicatePuts 要设为false。
& 所以我们一般的设置如下:
&cache name="org.springside.examples.miniweb.entity.user.User" maxElementsInMemory="500" overflowToDisk="true"
eternal="true"&
&cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=false,replicatePuts=false,replicateUpdatesViaCopy=false" /&
3.4 Shutdown
&& 在分布式环境或持久化硬盘时,需要调用CacheManager的shutdown操作,Hibernate会自动shutdown它自己的cacheManager,如果在hibernate之外使用,你需要增加:
&&&&& &listener&
&&&&&&&&& &listener-class&net.sf.ehcache.constructs.web.ShutdownListener&/listener-class&
&&&& &/listener&
4.JMS式并发同步
&&& 与其他同步方式相比,JMS同步支持了非Cache节点的程序对Cahce的修改。
&& 在分布式缓存中有一种需求:应用节点更改数据库数据后,需要通知所有缓存集群的节点,通常大家都是自行通过JMS实现的,而Ehcache的JMS Replicator 提供了一种标准的方案,提供PUT,REMOVE,REMOVE_ALL的标准操作。
浏览: 386433 次
来自: 杭州
误人子弟,不会就不要乱发
策略工厂实现Spring的ApplicationContext ...
我也一直不漏的看完了,感触颇深,还是要多花花时间陪陪父母
认真的看完了,每一位母亲都是伟大的,她为自己的子女,家庭付出的 ...
果然可以。我替换的jar ,xml文件没有对应的。Ehcache集成Spring缓存方法结果 - 凜冬将至-Winter Is Coming - ITeye博客
博客分类:
这是接上篇的进阶篇。
在实际的项目开发中,肯定会根据具体业务,数据大小,复杂度采用不同的技术实现方式,Ehcache在实际项目开发中一般被用来缓存方法结果集,且可以与Spring无缝集成,完全交由Spring——Aop拦截器来完成,我们只需处理好业务数据获取环节。
1.ehcache.xml配置:
&diskStore path="java.io.tmpdir"/&
&defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"/&
&cache name="gov.csc.ems.cache.METHOD_CACHE"
maxElementsInMemory="300"
eternal="false"
timeToIdleSeconds="600"
timeToLiveSeconds="600"
overflowToDisk="true"
&/ehcache&
2. Spring集成Ehcache配置,cacheManage,methodCache
&bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"&
&property name="configLocation"&
&value&classpath:ehcache.xml&/value&
&/property&
&bean id="methodCache"
class="org.springframework.cache.ehcache.EhCacheFactoryBean"&
&property name="cacheManager"&
&ref local="cacheManager" /&
&/property&
&property name="cacheName"&
&value&gov.csc.ems.cache.METHOD_CACHE&/value&
&/property&
3. 完成上面的基础配置,Spring是靠拦截器来缓存我们的方法,因此建立我们自己的方法拦截器MethodCacheInterceptor。MethodCacheInterceptor实现了org.aopalliance.intercept.MethodInterceptor接口。
import java.io.S
import org.aopalliance.intercept.MethodI
import org.aopalliance.intercept.MethodI
import org.springframework.beans.factory.InitializingB
import org.springframework.util.A
import net.sf.ehcache.C
import net.sf.ehcache.E
public class MethodCacheInterceptor implements MethodInterceptor,
InitializingBean {
* sets cache name to be used
public void setCache(Cache cache) {
this.cache =
* Checks if required attributes are provided.
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache,
"A cache is required. Use setCache(Cache) to provide one.");
* main method caches method result if method is configured for caching
* method results must be serializable
public Object invoke(MethodInvocation invocation) throws Throwable {
String targetName = invocation.getThis().getClass().getName();
String methodName = invocation.getMethod().getName();
Object[] arguments = invocation.getArguments();
String cacheKey = getCacheKey(targetName, methodName, arguments);
Element element = cache.get(cacheKey);
if (element == null) {
result = invocation.proceed();
element = new Element(cacheKey, (Serializable) result);
cache.put(element);
return element.getValue();
* creates cache key: targetName.methodName.argument0.argument1...
private String getCacheKey(String targetName, String methodName,
Object[] arguments) {
StringBuffer sb = new StringBuffer();
sb.append(targetName).append(".").append(methodName);
if ((arguments != null) && (arguments.length != 0)) {
for (int i = 0; i & arguments. i++) {
sb.append(".").append(arguments[i]);
return sb.toString();
invoke方法中:
String cacheKey = getCacheKey(targetName, methodName, arguments);
Element element = cache.get(cacheKey);
if (element == null) {
result = invocation.proceed();
element = new Element(cacheKey, (Serializable) result);
cache.put(element);
return element.getValue();
这段代码就是用来缓存我们的方法结果,拦截器先用key(key=className + methodName + arguments)查询缓存,缓存中存在则返回,否则调用invocation.proceed(),根据我们自己的实现查询数据,即第一次查询,以后每次就走缓存。
4.Spring中定义拦截器配置:
&bean id="methodCacheInterceptor"
class="gov.csc.ems.util.cache.interceptor.MethodCacheInterceptor"&
&property name="cache"&
&ref local="methodCache" /&
&/property&
5.Aop正则表达式的切入点配置:
&bean id="methodCachePointCut"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"&
&property name="advice"&
&ref local="methodCacheInterceptor" /&
&/property&
&property name="patterns"&
&value&.*&/value&
&/property&
patterns要拦截的规则,可以拦截指定的方法,如.getOrg*,.getUser*,这里我拦截所有的方法。
6.交给Spring代理
&bean id="codeCacheBean"
class="org.springframework.aop.framework.ProxyFactoryBean"&
&property name="target"&
&bean class="gov.csc.ems.util.cache.CodeCache" /&
&/property&
&property name="interceptorNames"&
&value&methodCachePointCut&/value&
&/property&
其中&bean class="gov.csc.ems.util.cache.CodeCache" /&它就是我们缓存的类,缓存就是它里面的所有方法,当我们要用到它里面的方法时,如果此方法配置了缓存拦截,就会先走拦截器。
7.要缓存的方法类CodeCache
public class CodeCache {
public CodeCache() {
public Map getOrgCache() {
//从数据库查询org
public Map getUserCache() {
//从数据库查询user
我这里写了个servlet,根据Spring获取到CodeCache,
CodeCache codeCache=(CodeCache) BeanUtil.getBean("codeCacheBean");
Map codeBasOrgs=codeCache.getOrgCache();
System.out.println(codeBasOrgs.get("0001"));
分别在拦截器和CodeCache中打上断点,运行发现每次先走拦截器,而且第一次会走CodeCache查询,以后就直接取缓存了。
9.缓存的更新
如新添加了Org或user,CodeCache.getOrgCache.put(key,value)即可添加到缓存。
yuwenlin2008
浏览: 46977 次
来自: 北京
我帮你加上/soft/ ...
不错,正是我想做的
难得看开一两篇讲得不错的文章!
struts2 2007 的东西了,现在 springMVCEhcache怎样在Element级上控制过期时间 - wangxuliangboy - ITeye博客
CacheManager ehCacheManager = new CacheManager();
ehCacheManager.addCache("default");
Ehcache cache = ehCacheManager.getCache("default");
Element e = new Element("aa", "aa", false, 1, 1);
cache.put(e);
System.out.println(cache.get("aa"));
Thread.sleep(1050);
System.out.println(cache.get("aa"));//如果这个时候,期待cache是否过期。但是实际的情况是。ehcache依然能获取到相关数据.
当你去调用ehcache.put动作时,会调用applyDefaultsToElementWithoutLifespanSet(element);
if (!element.isLifespanSet()) {
//Setting with Cache defaults
element.setTimeToLive((int) configuration.getTimeToLiveSeconds());
element.setTimeToIdle((int) configuration.getTimeToIdleSeconds());
element.setEternal(configuration.isEternal());
Element里面有一个isLifespan的参数,默认是为false的。。
false的时候.Ehcache会element的过期时间设置为默认配置的
当你通过new
Element(Object key, Object value,
boolean eternal, int timeToIdleSeconds, int timeToLiveSeconds)
去实例化的时候。。根本不会去设置isLifespan这个参数,而是采用默认的过期策略的。。
但是去调用element 的。setTimeToLive,setTimeToIdle,setEternal方法时,
确会去设置这个参数。。
Element.setEternal(boolean eternal) {
this.eternal =
lifespanSet =
这样的话。。就会去单独去设置Element控制过期时间,而不会用默认的配置去覆盖设置.
个人觉得这个ehcache存在的一个BUG。。
既然在调用element 的。setTimeToLive,setTimeToIdle,setEternal方法时,会去设置这个参数,那么如果在构造的时候也应该调用这些方法。
但是有可能ehcache希望用户能根据不同cache config去配置应用。而不应该应用到element级别上
wangxuliangboy
浏览: 152390 次
来自: 上海
你好,我在compass + terracotta 整合的时候 ...
[align=right][/align]
我用java'的库也验证过,用public key解密priv ...
这个说法正确吗?RSA 算法的公钥和私鈅是对称的啊私鈅加密的公 ...}

我要回帖

更多关于 涉密计算机可以使用 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信