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.tag.query;
020
021import java.io.Serializable;
022import java.util.Date;
023
024import lucee.commons.digest.HashUtil;
025import lucee.runtime.PageContext;
026import lucee.runtime.cache.tag.CacheItem;
027import lucee.runtime.cache.tag.udf.UDFArgConverter;
028import lucee.runtime.dump.DumpData;
029import lucee.runtime.dump.DumpProperties;
030import lucee.runtime.dump.Dumpable;
031import lucee.runtime.type.Query;
032
033public class QueryCacheItem implements CacheItem, Dumpable, Serializable {
034
035        private static final long serialVersionUID = 7327671003736543783L;
036
037        public final Query query;
038        private final long creationDate;
039
040        public QueryCacheItem(Query query){
041                this.query=query;
042                this.creationDate=System.currentTimeMillis();
043        }
044
045        @Override
046        public String getHashFromValue() {
047                return Long.toString(HashUtil.create64BitHash(UDFArgConverter.serialize(query)));
048        }
049        
050        @Override
051        public String getName() {
052                return query.getName();
053        }
054
055        public Query getQuery() {
056                return query;
057        }
058
059        @Override
060        public long getPayload() {
061                return query.getRecordcount();
062        }
063        
064        @Override
065        public String getMeta() {
066                return query.getSql().getSQLString();
067        }
068        
069        @Override
070        public long getExecutionTime() {
071                return query.getExecutionTime();
072        }
073
074        public boolean isCachedAfter(Date cacheAfter) {
075        if(cacheAfter==null) return true;
076        if(creationDate>=cacheAfter.getTime()){
077                return true;
078        }
079        return false;
080    }
081
082        @Override
083        public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties properties) {
084                return query.toDumpData(pageContext, maxlevel, properties);
085        }
086
087}