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;
020
021import java.io.IOException;
022
023import lucee.commons.io.cache.Cache;
024import lucee.commons.io.cache.CacheEntry;
025import lucee.commons.io.cache.CacheFilter;
026import lucee.commons.io.cache.complex.CacheComplex;
027import lucee.commons.lang.StringUtil;
028import lucee.runtime.config.Config;
029import lucee.runtime.type.Struct;
030import lucee.runtime.type.StructImpl;
031import lucee.runtime.type.dt.TimeSpan;
032import lucee.runtime.type.dt.TimeSpanImpl;
033
034public class CacheUtil {
035        public static Struct getInfo(CacheEntry ce) {
036                return getInfo(new StructImpl(), ce);
037        }
038        
039        public static Struct getInfo(Struct info,CacheEntry ce) {
040                if(info==null) info=new StructImpl();
041                info.setEL("key", ce.getKey());
042                info.setEL("created", ce.created());
043                info.setEL("last_hit", ce.lastHit());
044                info.setEL("last_modified", ce.lastModified());
045
046                info.setEL("hit_count", new Double(ce.hitCount()));
047                info.setEL("size", new Double(ce.size()));
048                
049                
050                info.setEL("idle_time_span", toTimespan(ce.idleTimeSpan()));            
051                info.setEL("live_time_span", toTimespan(ce.liveTimeSpan()));
052                
053                
054                return info;
055        }
056
057
058        public static Struct getInfo(Cache c) {
059                return getInfo(new StructImpl(), c);
060        }
061
062        public static Struct getInfo(Struct info, Cache c) {
063                if(info==null) info=new StructImpl();
064
065                long value = c.hitCount();
066                if(value>=0)info.setEL("hit_count", new Double(value));
067                value = c.missCount();
068                if(value>=0)info.setEL("miss_count", new Double(value));
069                
070                return info;
071        }
072
073        
074        public static Object toTimespan(long timespan) {
075                if(timespan==0)return "";
076                
077                TimeSpan ts = TimeSpanImpl.fromMillis(timespan);
078                if(ts==null)return "";
079                return ts;
080        }
081
082
083        public static String toString(CacheEntry ce) {
084
085                return "created:        "+ce.created()
086                +"\nlast-hit:   "+ce.lastHit()
087                +"\nlast-modified:      "+ce.lastModified()
088                
089                +"\nidle-time:  "+ce.idleTimeSpan()
090                +"\nlive-time   :"+ce.liveTimeSpan()
091                
092                +"\nhit-count:  "+ce.hitCount()
093                +"\nsize:               "+ce.size();
094        }
095
096
097        public static boolean allowAll(CacheFilter filter) {
098                if(filter==null)return true;
099                String p = StringUtil.trim(filter.toPattern(),"");
100                return p.equals("*") || p.equals("");
101        }
102
103        /**
104         * in difference to the getInstance method of the CacheConnection this method produces a wrapper Cache (if necessary) that creates Entry objects to make sure the Cache has meta data.
105         * @param cc
106         * @param config
107         * @return
108         * @throws IOException 
109         */
110        public static Cache getInstance(CacheConnection cc, Config config) throws IOException {
111                return  cc.getInstance(config);
112                /*Cache c = cc.getInstance(config);
113                if("org.lucee.extension.io.cache.memcache.MemCacheRaw".equals(c.getClass().getName())) {
114                        return new CacheComplex(cc,c);
115                }
116                return c;*/
117        }
118
119}