java 过期缓存 事件监听_Java EHCache 开源分布式缓存框架 缓存事件监听器
事件监听器可以监听到,缓存元素创建、移除、更新、过期等。EHCache版本:ehcache-2.9.0实例代码:maxEntriesLocalHeap="10000"eternal="false"timeToIdleSeconds="60"timeToLiveSeconds="60"maxEntriesLocalDisk="10000000"diskExpiryThreadIntervalSec.
事件监听器可以监听到,缓存元素创建、移除、更新、过期等。
EHCache版本: ehcache-2.9.0
实例代码:
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="60"
timeToLiveSeconds="60"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="30"
memoryStoreEvictionPolicy="LRU">
class="com.what21.ehcache.event.MyCacheEventListenerFactory"
properties="cache=mycache" />
package com.what21.ehcache.event;
import java.util.Properties;
import net.sf.ehcache.event.CacheEventListener;
import net.sf.ehcache.event.CacheEventListenerFactory;
public class MyCacheEventListenerFactory extends CacheEventListenerFactory {
@Override
public CacheEventListener createCacheEventListener(Properties props) {
return new MyCacheEventListener(props);
}
}
package com.what21.ehcache.event;
import java.util.Properties;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.event.CacheEventListener;
public class MyCacheEventListener implements CacheEventListener {
public MyCacheEventListener(Properties props){
System.out.println("props = " + props);
}
@Override
public void notifyElementEvicted(Ehcache ehcache, Element element) {
System.out.println("notifyElementEvicted()..");
System.out.println(ehcache);
System.out.println(element);
}
/**
* 元素过期触发
*/
@Override
public void notifyElementExpired(Ehcache ehcache, Element element) {
System.out.println("notifyElementExpired()..");
System.out.println(ehcache);
System.out.println(element);
}
@Override
public void notifyElementPut(Ehcache ehcache, Element element)
throws CacheException {
System.out.println("notifyElementPut()..");
System.out.println(ehcache);
System.out.println(element);
}
@Override
public void notifyElementRemoved(Ehcache ehcache, Element element)
throws CacheException {
System.out.println("notifyElementRemoved()..");
System.out.println(ehcache);
System.out.println(element);
}
@Override
public void notifyElementUpdated(Ehcache ehcache, Element element)
throws CacheException {
System.out.println("notifyElementUpdated()..");
System.out.println(ehcache);
System.out.println(element);
}
@Override
public void notifyRemoveAll(Ehcache ehcache) {
System.out.println("notifyRemoveAll()..");
System.out.println(ehcache);
}
@Override
public void dispose() {
System.out.println("dispose()..");
}
@Override
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException("non supported");
}
}
package com.what21.ehcache.event;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class EventEhcache {
/**
* @param args
*/
public static void main(String[] args) {
//
String path = EventEhcache.class.getResource("ehcache.xml").getPath().toString();
System.out.println("path = " + path);
// 创建 CacheManager
CacheManager manager = CacheManager.create(path);
// 获取默认的
Cache what21Cache = manager.getCache("what21Cache");
System.out.println("what21Cache = " + what21Cache);
// 缓存内容
Element element = new Element("aaa", "aaa");
what21Cache.put(element);
try {
Thread.sleep( 2*60 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 访问缓存
Element cacheEle = what21Cache.get("aaa");
if(cacheEle!=null){
System.out.println("Key: " + cacheEle.getObjectKey());
System.out.println("Value: " + cacheEle.getObjectValue());
}else{
System.out.println("已过期,Key: aaa");
}
}
}
更多推荐

所有评论(0)