001    package railo.runtime.functions.cache;
002    
003    import java.io.IOException;
004    
005    import railo.commons.io.cache.Cache;
006    import railo.commons.io.cache.CacheEntry;
007    import railo.runtime.PageContext;
008    import railo.runtime.config.ConfigImpl;
009    import railo.runtime.exp.PageException;
010    import railo.runtime.ext.function.Function;
011    import railo.runtime.op.Caster;
012    import railo.runtime.type.Collection;
013    import railo.runtime.type.KeyImpl;
014    import railo.runtime.type.Struct;
015    import railo.runtime.type.StructImpl;
016    
017    /**
018     * 
019     */
020    public final class CacheGetMetadata implements Function {
021            
022            private static final long serialVersionUID = -470089623854482521L;
023            
024            private static final Collection.Key CACHE_HITCOUNT = KeyImpl.intern("cache_hitcount");
025            private static final Collection.Key CACHE_MISSCOUNT = KeyImpl.intern("cache_misscount");
026            private static final Collection.Key CACHE_CUSTOM = KeyImpl.intern("cache_custom");
027            private static final Collection.Key CUSTOM = KeyImpl.intern("custom");
028            private static final Collection.Key CREATED_TIME = KeyImpl.intern("createdtime");
029            private static final Collection.Key HIT_COUNT = KeyImpl.intern("hitcount");
030            private static final Collection.Key IDLE_TIME = KeyImpl.intern("idletime");
031            private static final Collection.Key LAST_HIT = KeyImpl.intern("lasthit");
032            private static final Collection.Key LAST_UPDATED = KeyImpl.intern("lastupdated");
033            private static final Collection.Key TIME_SPAN = KeyImpl.intern("timespan");
034    
035            public static Struct call(PageContext pc, String id) throws PageException {
036                    return call(pc, id,null);
037            }
038            
039            public static Struct call(PageContext pc, String id, String cacheName) throws PageException {
040                    try {
041                            Cache cache = Util.getCache(pc.getConfig(),cacheName,ConfigImpl.CACHE_DEFAULT_OBJECT);
042                            CacheEntry entry = cache.getCacheEntry(Util.key(id));
043                            
044                            Struct info=new StructImpl();
045                            info.set(CACHE_HITCOUNT, new Double(cache.hitCount()));
046                            info.set(CACHE_MISSCOUNT, new Double(cache.missCount()));
047                            info.set(CACHE_CUSTOM, cache.getCustomInfo());
048                            info.set(CUSTOM, entry.getCustomInfo());
049                            
050                            info.set(CREATED_TIME, entry.created());
051                            info.set(HIT_COUNT, new Double(entry.hitCount()));
052                            info.set(IDLE_TIME, new Double(entry.idleTimeSpan()));
053                            info.set(LAST_HIT, entry.lastHit());
054                            info.set(LAST_UPDATED, entry.lastModified());
055                            info.set(KeyImpl.SIZE, new Double(entry.size()));
056                            info.set(TIME_SPAN, new Double(entry.liveTimeSpan()));
057                            return info;
058                    } catch (IOException e) {
059                            throw Caster.toPageException(e);
060                    }
061            }
062    }