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.Struct;
014    import railo.runtime.type.util.KeyConstants;
015    
016    public abstract class EHCacheSupport extends CacheSupport implements Cache,CacheEvent {
017    
018            @Override
019            public void register(CacheEventListener listener) {
020                    //RegisteredEventListeners listeners=cache.getCacheEventNotificationService();
021                    //listeners.registerListener(new ExpiresCacheEventListener());
022                    
023                    
024                    net.sf.ehcache.Cache cache = getCache();
025                    RegisteredEventListeners service = cache.getCacheEventNotificationService();
026                    service.registerListener(new EHCacheEventListener(listener));
027                    
028                    
029                    //.getCacheEventListeners().add(new EHCacheEventListener(listener));
030            }
031    
032            @Override
033            public boolean contains(String key) {
034                    if(!getCache().isKeyInCache(key))return false;
035                    return getCache().get(key)!=null;
036            }
037    
038            @Override
039            public Struct getCustomInfo() {
040                    Struct info=super.getCustomInfo();
041                    // custom
042                    CacheConfiguration conf = getCache().getCacheConfiguration();
043                    info.setEL("disk_expiry_thread_interval", new Double(conf.getDiskExpiryThreadIntervalSeconds()));
044                    info.setEL("disk_spool_buffer_size", new Double(conf.getDiskSpoolBufferSizeMB()*1024*1024));
045                    info.setEL("max_elements_in_memory", new Double(conf.getMaxElementsInMemory()));
046                    info.setEL("max_elements_on_disk", new Double(conf.getMaxElementsOnDisk()));
047                    info.setEL("time_to_idle", new Double(conf.getTimeToIdleSeconds()));
048                    info.setEL("time_to_live", new Double(conf.getTimeToLiveSeconds()));
049                    info.setEL(KeyConstants._name, conf.getName());
050                    return info;
051            }
052    
053            @Override
054            public List keys() {
055                    return getCache().getKeysWithExpiryCheck();
056            }
057            
058            @Override
059            public void put(String key, Object value, Long idleTime, Long liveTime) {
060                    Boolean eternal = idleTime==null && liveTime==null?Boolean.TRUE:Boolean.FALSE;
061                    Integer idle = idleTime==null?null:new Integer((int)idleTime.longValue()/1000);
062                    Integer live = liveTime==null?null:new Integer((int)liveTime.longValue()/1000);
063                    getCache().put(new Element(key, value ,eternal, idle, live));
064            }
065    
066            
067            
068    
069            public CacheEntry getQuiet(String key, CacheEntry defaultValue){
070                    try {
071                            return new EHCacheEntry(getCache().getQuiet(key));
072                    } catch (Throwable t) {
073                            return defaultValue;
074                    }
075            }
076            
077            public CacheEntry getQuiet(String key) {
078                    return new EHCacheEntry(getCache().getQuiet(key));
079            }
080    
081            protected abstract net.sf.ehcache.Cache getCache();
082            
083            
084    }