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    import railo.runtime.type.util.KeyConstants;
017    
018    /**
019     * 
020     */
021    public final class CacheGetMetadata implements Function {
022            
023            private static final long serialVersionUID = -470089623854482521L;
024            
025            private static final Collection.Key CACHE_HITCOUNT = KeyImpl.intern("cache_hitcount");
026            private static final Collection.Key CACHE_MISSCOUNT = KeyImpl.intern("cache_misscount");
027            private static final Collection.Key CACHE_CUSTOM = KeyImpl.intern("cache_custom");
028            private static final Collection.Key CREATED_TIME = KeyImpl.intern("createdtime");
029            private static final Collection.Key IDLE_TIME = KeyImpl.intern("idletime");
030            private static final Collection.Key LAST_HIT = KeyImpl.intern("lasthit");
031            private static final Collection.Key LAST_UPDATED = KeyImpl.intern("lastupdated");
032    
033            public static Struct call(PageContext pc, String id) throws PageException {
034                    return call(pc, id,null);
035            }
036            
037            public static Struct call(PageContext pc, String id, String cacheName) throws PageException {
038                    try {
039                            Cache cache = Util.getCache(pc,cacheName,ConfigImpl.CACHE_DEFAULT_OBJECT);
040                            CacheEntry entry = cache.getCacheEntry(Util.key(id));
041                            
042                            Struct info=new StructImpl();
043                            info.set(CACHE_HITCOUNT, new Double(cache.hitCount()));
044                            info.set(CACHE_MISSCOUNT, new Double(cache.missCount()));
045                            info.set(CACHE_CUSTOM, cache.getCustomInfo());
046                            info.set(KeyConstants._custom, entry.getCustomInfo());
047                            
048                            info.set(CREATED_TIME, entry.created());
049                            info.set(KeyConstants._hitcount, new Double(entry.hitCount()));
050                            info.set(IDLE_TIME, new Double(entry.idleTimeSpan()));
051                            info.set(LAST_HIT, entry.lastHit());
052                            info.set(LAST_UPDATED, entry.lastModified());
053                            info.set(KeyConstants._size, new Double(entry.size()));
054                            info.set(KeyConstants._timespan, new Double(entry.liveTimeSpan()));
055                            return info;
056                    } catch (IOException e) {
057                            throw Caster.toPageException(e);
058                    }
059            }
060    }