Ehcache怎样在Element级上控制过期时间

Ehcache怎样在Element级上控制过期时间,第1张

但是实际的情况是。ehcache依然能获取到相关数据

当你去调用ehcacheput动作时,会调用applyDefaultsToElementWithoutLifespanSetelement;方法内容:if !elementisLifespanSet Element里面有一个isLifespan的参数,默认是为false的。。

false的时候Ehcache会element的过期时间设置为默认配置的

当你通过new ElementObject key, Object value,

boolean eternal, int timeToIdleSeconds, int timeToLiveSeconds

去实例化的时候。。根本不会去设置isLifespan这个参数,而是采用默认的过期策略的。。

但是去调用element 的。setTimeToLive,setTimeToIdle,setEternal方法时,

确会去设置这个参数。。

ElementsetEternalboolean eternal

这样的话。。就会去单独去设置Element控制过期时间,而不会用默认的配置去覆盖设置

个人觉得这个ehcache存在的一个BUG。。

既然在调用element 的。setTimeToLive,setTimeToIdle,setEternal方法时,会去设置这个参数,那么如果在构造的时候也应该调用这些方法。

而不应该应用到element级别上

Ehcache缓存同步有几种方式:(1)RMI (2)Terrocotta (3)JMS (4)JGroups

先介绍下,利用RMI进行缓存同步。

测试类1:在sampleDistributedCache2缓存中查找是否存在ehcache键,如果没找到,则打印NotFound;如果找到了,则打印相应值

[java] view plaincopy

package mytestehcache1;

import javaioInputStream;

import javalangmanagementManagementFactory;

import javaxmanagementMBeanServer;

import netsfehcacheCache;

import netsfehcacheCacheManager;

import netsfehcacheElement;

import netsfehcachemanagementManagementService;

public class EHCacheTest {

public static void main(String[] args) {

InputStream is = EHCacheTestclass

getResourceAsStream("/my/test/ehcache1/ehcachexml");

//读入配置

CacheManager cacheManager = new CacheManager(is);

//打印初始缓存

String[] cacheNames = cacheManagergetCacheNames();

printNames(cacheNames);

//移除缓存

cacheManagerremoveCache("sampleDistributedCache1");

cacheNames = cacheManagergetCacheNames();

printNames(cacheNames);

//新建缓存

Cache cache = new Cache("Test1", 100, true, false, 10, 10);

cacheManageraddCache(cache);

cacheNames = cacheManagergetCacheNames();

printNames(cacheNames);

cacheput(new Element("test1", "value1"));

//得到缓存并插入值(这里监听器被调用)

cache = cacheManagergetCache("sampleCache3");

for (int i = 0; i < 20; i++) {

cacheput(new Element("key" + i, "value" + i));

}

cacheget("key10");

// distributed -- rmi同步

cache = cacheManagergetCache("sampleDistributedCache2");

for (int i = 0; i < 100; i++) {

cacheput(new Element("key" + i , "value" + i));

}

//注册被管理的Bean

// JMX -- jconsole(MBeanServer)

MBeanServer mBeanServer = ManagementFactorygetPlatformMBeanServer();

ManagementServiceregisterMBeans(cacheManager, mBeanServer, true, true,

true, true);

for (int i = 0; i < 10; i++) {

Element temp = cacheget("ehcache");

if (temp != null) {

Systemoutprintln(tempgetValue());

} else {

Systemoutprintln("NotFound");

}

try {

Threadsleep(10000);

} catch (InterruptedException e) {

eprintStackTrace();

}

}

// distributed cache using RMI

// 1Peer Discovery -- cacheManagerPeerProviderFactory

// 2CacheManager -- cacheManagerPeerListenerFactory

// 3cache replication -- cacheEventListenerFactory

// 4Bootstrap -- 启动后同步

}

private static void printNames(String[] names) {

Systemoutprintln("=======================");

for (int i = 0; i < nameslength; i++) {

Systemoutprintln(names[i]);

}

}

}

配置文件1:将官方样例文件中相应位置替换即可

(1)其他JVM提供缓存的rmiUrl地址

[java] view plaincopy

<cacheManagerPeerProviderFactory

class="netsfehcachedistributionRMICacheManagerPeerProviderFactory"

properties="peerDiscovery=manual,

rmiUrls=//localhost:40002/sampleDistributedCache2"

propertySeparator=","

/>

(2)监听来自于其他复制节点消息的本JVM的host,port

[xhtml] view plaincopy

<cacheManagerPeerListenerFactory

class="netsfehcachedistributionRMICacheManagerPeerListenerFactory"

properties="hostName=localhost, port=40001, socketTimeoutMillis=2000"

/>

(3)配置复制选项,启动时同步等

[xhtml] view plaincopy

<cache name="sampleDistributedCache2"

maxElementsInMemory="10"

eternal="false"

timeToIdleSeconds="100"

timeToLiveSeconds="100"

overflowToDisk="false">

<cacheEventListenerFactory

class="netsfehcachedistributionRMICacheReplicatorFactory"

properties="replicateAsynchronously=true, replicatePuts=true,

replicatePutsViaCopy=true, replicateUpdates=true,

replicateUpdatesViaCopy=true, replicateRemovals=true,

asynchronousReplicationIntervalMillis=200"/>

<bootstrapCacheLoaderFactory

class="netsfehcachedistributionRMIBootstrapCacheLoaderFactory"/>

</cache>

测试类2:向sampleDistributedCache2缓存中添加ehcache键

[java] view plaincopy

package mytestehcache2;

import javaioInputStream;

import javalangmanagementManagementFactory;

import javaxmanagementMBeanServer;

import netsfehcacheCache;

import netsfehcacheCacheManager;

import netsfehcacheElement;

import netsfehcacheStatistics;

import netsfehcachemanagementManagementService;

import netsfehcachestatisticsLiveCacheStatistics;

public class EHCacheTest {

public static void main(String[] args) {

try {

Threadsleep(3 1000);

} catch (InterruptedException e) {

eprintStackTrace();

}

//读入配置

InputStream is = EHCacheTestclassgetResourceAsStream("/my/test/ehcache2/ehcachexml");

CacheManager cacheManager = new CacheManager(is);

//打印初始缓存

String[] cacheNames = cacheManagergetCacheNames();

printNames(cacheNames);

//注册管理Bean

MBeanServer mBeanServer = ManagementFactorygetPlatformMBeanServer();

ManagementServiceregisterMBeans(cacheManager, mBeanServer, true, true, true, true);

//distributed

Cache cache = cacheManagergetCache("sampleDistributedCache2");

printCache(cache);

//添加值后另一个虚拟机的缓存通过RMI会同步缓存,并读到这个值

cacheput(new Element("ehcache", "newaddvalue"));

}

private static void printNames(String[] names) {

Systemoutprintln("=======================");

for (int i = 0; i < nameslength; i++) {

Systemoutprintln(names[i]);

}

}

private static void printCache(Cache cache) {

int size = cachegetSize();

long memSize = cachegetMemoryStoreSize();

long diskSize = cachegetDiskStoreSize();

Statistics stat = cachegetStatistics();

LiveCacheStatistics liveStat = cachegetLiveCacheStatistics();

long hits = statgetCacheHits();

long missed = statgetCacheMisses();

long hitsOnDisk = statgetOnDiskHits();

long liveHits = liveStatgetCacheHitCount();

long liveMissed = liveStatgetCacheMissCount();

StringBuilder sb = new StringBuilder();

sbappend("size=" + size + ";memsize=" + memSize);

sbappend(";diskSize=" + diskSize + ";hits=" + hits);

sbappend(";missed=" + missed + ";liveHits=" + liveHits);

sbappend(";liveMissed=" + liveMissed + ";hitsOnDisk=" + hitsOnDisk);

Systemoutprintln(sbtoString());

}

}

配置文件:将官方样例文件中相应位置替换即可

[xhtml] view plaincopy

<cacheManagerPeerProviderFactory

class="netsfehcachedistributionRMICacheManagerPeerProviderFactory"

properties="peerDiscovery=manual,

rmiUrls=//localhost:40001/sampleDistributedCache2"

propertySeparator=","

/>

[xhtml] view plaincopy

<cacheManagerPeerListenerFactory

class="netsfehcachedistributionRMICacheManagerPeerListenerFactory"

properties="hostName=localhost, port=40002, socketTimeoutMillis=2000"

/>

[xhtml] view plaincopy

<cache name="sampleDistributedCache2"

maxElementsInMemory="10"

eternal="false"

timeToIdleSeconds="100"

timeToLiveSeconds="100"

overflowToDisk="false">

<cacheEventListenerFactory

class="netsfehcachedistributionRMICacheReplicatorFactory"

properties="replicateAsynchronously=true, replicatePuts=true,

replicatePutsViaCopy=true, replicateUpdates=true,

replicateUpdatesViaCopy=true, replicateRemovals=true,

asynchronousReplicationIntervalMillis=200"/>

<bootstrapCacheLoaderFactory

class="netsfehcachedistributionRMIBootstrapCacheLoaderFactory"/>

</cache>

name:缓存名称。

maxElementsInMemory:缓存最大个数。

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

timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。

timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0,也就是对象存活时间无穷大。

overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。

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

maxElementsOnDisk:硬盘最大缓存个数。

diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine The default value is false

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

memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。

clearOnFlush:内存数量最大时是否清除。

以上就是关于Ehcache怎样在Element级上控制过期时间全部的内容,包括:Ehcache怎样在Element级上控制过期时间、ehcache rmi 的udp方式同步缓存,他的广播地址怎么确认、java web项目里ehcache.xml是干什么用的 我完全看不懂!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9671824.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存