001    package railo.runtime.cache.eh;
002    
003    import java.util.List;
004    
005    import net.sf.ehcache.Element;
006    import net.sf.ehcache.config.CacheConfiguration;
007    import net.sf.ehcache.event.RegisteredEventListeners;
008    import railo.commons.io.cache.Cache;
009    import railo.commons.io.cache.CacheEntry;
010    import railo.commons.io.cache.CacheEvent;
011    import railo.commons.io.cache.CacheEventListener;
012    import railo.runtime.cache.CacheSupport;
013    import railo.runtime.type.KeyImpl;
014    import railo.runtime.type.Struct;
015    
016    public abstract class EHCacheSupport extends CacheSupport implements Cache,CacheEvent {
017    
018            /**
019             * @see railo.commons.io.cache.CacheEvent#register(railo.commons.io.cache.CacheEventListener)
020             */
021            public void register(CacheEventListener listener) {
022                    //RegisteredEventListeners listeners=cache.getCacheEventNotificationService();
023                    //listeners.registerListener(new ExpiresCacheEventListener());
024                    
025                    
026                    net.sf.ehcache.Cache cache = getCache();
027                    RegisteredEventListeners service = cache.getCacheEventNotificationService();
028                    service.registerListener(new EHCacheEventListener(listener));
029                    
030                    
031                    //.getCacheEventListeners().add(new EHCacheEventListener(listener));
032            }
033    
034            /**
035             * @see railo.commons.io.cache.Cache#contains(String)
036             */
037            public boolean contains(String key) {
038                    if(!getCache().isKeyInCache(key))return false;
039                    return getCache().get(key)!=null;
040            }
041    
042            /**
043             * @see railo.commons.io.cache.Cache#getCustomInfo()
044             */
045            public Struct getCustomInfo() {
046                    Struct info=super.getCustomInfo();
047                    // custom
048                    CacheConfiguration conf = getCache().getCacheConfiguration();
049                    info.setEL("disk_expiry_thread_interval", new Double(conf.getDiskExpiryThreadIntervalSeconds()));
050                    info.setEL("disk_spool_buffer_size", new Double(conf.getDiskSpoolBufferSizeMB()*1024*1024));
051                    info.setEL("max_elements_in_memory", new Double(conf.getMaxElementsInMemory()));
052                    info.setEL("max_elements_on_disk", new Double(conf.getMaxElementsOnDisk()));
053                    info.setEL("time_to_idle", new Double(conf.getTimeToIdleSeconds()));
054                    info.setEL("time_to_live", new Double(conf.getTimeToLiveSeconds()));
055                    info.setEL(KeyImpl.NAME, conf.getName());
056                    return info;
057            }
058    
059            /**
060             * @see railo.commons.io.cache.Cache#keys()
061             */
062            public List keys() {
063                    return getCache().getKeysWithExpiryCheck();
064            }
065            
066            /**
067             * @see railo.commons.io.cache.Cache#put(String, java.lang.Object, java.lang.Long, java.lang.Long)
068             */
069            public void put(String key, Object value, Long idleTime, Long liveTime) {
070                    Boolean eternal = idleTime==null && liveTime==null?Boolean.TRUE:Boolean.FALSE;
071                    Integer idle = idleTime==null?null:new Integer((int)idleTime.longValue()/1000);
072                    Integer live = liveTime==null?null:new Integer((int)liveTime.longValue()/1000);
073                    getCache().put(new Element(key, value ,eternal, idle, live));
074            }
075    
076            
077            
078    
079            public CacheEntry getQuiet(String key, CacheEntry defaultValue){
080                    try {
081                            return new EHCacheEntry(getCache().getQuiet(key));
082                    } catch (Throwable t) {
083                            return defaultValue;
084                    }
085            }
086            
087            public CacheEntry getQuiet(String key) {
088                    return new EHCacheEntry(getCache().getQuiet(key));
089            }
090    
091            protected abstract net.sf.ehcache.Cache getCache();
092            
093            
094    }