001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.cache.eh; 020 021import java.util.List; 022 023import lucee.commons.io.cache.Cache; 024import lucee.commons.io.cache.CacheEntry; 025import lucee.commons.io.cache.CacheEvent; 026import lucee.commons.io.cache.CacheEventListener; 027import lucee.commons.lang.ExceptionUtil; 028import lucee.runtime.cache.CacheSupport; 029import lucee.runtime.type.Struct; 030import lucee.runtime.type.util.KeyConstants; 031import net.sf.ehcache.Element; 032import net.sf.ehcache.config.CacheConfiguration; 033import net.sf.ehcache.event.RegisteredEventListeners; 034 035public abstract class EHCacheSupport extends CacheSupport implements Cache,CacheEvent { 036 037 @Override 038 public void register(CacheEventListener listener) { 039 //RegisteredEventListeners listeners=cache.getCacheEventNotificationService(); 040 //listeners.registerListener(new ExpiresCacheEventListener()); 041 042 043 net.sf.ehcache.Cache cache = getCache(); 044 RegisteredEventListeners service = cache.getCacheEventNotificationService(); 045 service.registerListener(new EHCacheEventListener(listener)); 046 047 048 //.getCacheEventListeners().add(new EHCacheEventListener(listener)); 049 } 050 051 @Override 052 public boolean contains(String key) { 053 if(!getCache().isKeyInCache(key))return false; 054 return getCache().get(key)!=null; 055 } 056 057 @Override 058 public Struct getCustomInfo() { 059 Struct info=super.getCustomInfo(); 060 // custom 061 CacheConfiguration conf = getCache().getCacheConfiguration(); 062 info.setEL("disk_expiry_thread_interval", new Double(conf.getDiskExpiryThreadIntervalSeconds())); 063 info.setEL("disk_spool_buffer_size", new Double(conf.getDiskSpoolBufferSizeMB()*1024*1024)); 064 info.setEL("max_elements_in_memory", new Double(conf.getMaxElementsInMemory())); 065 info.setEL("max_elements_on_disk", new Double(conf.getMaxElementsOnDisk())); 066 info.setEL("time_to_idle", new Double(conf.getTimeToIdleSeconds())); 067 info.setEL("time_to_live", new Double(conf.getTimeToLiveSeconds())); 068 info.setEL(KeyConstants._name, conf.getName()); 069 return info; 070 } 071 072 @Override 073 public List keys() { 074 return getCache().getKeysWithExpiryCheck(); 075 } 076 077 @Override 078 public void put(String key, Object value, Long idleTime, Long liveTime) { 079 Boolean eternal = idleTime==null && liveTime==null?Boolean.TRUE:Boolean.FALSE; 080 Integer idle = idleTime==null?null : new Integer( (int)(idleTime.longValue()/1000) ); 081 Integer live = liveTime==null?null : new Integer( (int)(liveTime.longValue()/1000) ); 082 getCache().put(new Element(key, value ,eternal, idle, live)); 083 } 084 085 086 087 088 public CacheEntry getQuiet(String key, CacheEntry defaultValue){ 089 try { 090 return new EHCacheEntry(getCache().getQuiet(key)); 091 } catch (Throwable t) { 092 ExceptionUtil.rethrowIfNecessary(t); 093 return defaultValue; 094 } 095 } 096 097 public CacheEntry getQuiet(String key) { 098 return new EHCacheEntry(getCache().getQuiet(key)); 099 } 100 101 protected abstract net.sf.ehcache.Cache getCache(); 102 103 104}