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.functions.cache;
020
021import java.io.IOException;
022
023import lucee.commons.io.cache.Cache;
024import lucee.commons.lang.StringUtil;
025import lucee.runtime.PageContext;
026import lucee.runtime.config.ConfigImpl;
027import lucee.runtime.exp.FunctionException;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.ext.function.Function;
030import lucee.runtime.op.Caster;
031
032/**
033 * 
034 */
035public final class CacheGet implements Function {
036
037        private static final long serialVersionUID = -7164470356423036571L;
038
039        public static Object call(PageContext pc, String key) throws PageException {
040                try {
041                        return _call(pc, key, false, Util.getDefault(pc, ConfigImpl.CACHE_DEFAULT_OBJECT));
042                } 
043                catch (IOException e) {
044                        throw Caster.toPageException(e);
045                }
046        }
047
048        public static Object call(PageContext pc, String key, Object objThrowWhenNotExist) throws PageException {
049                // default behavior, second parameter is a boolean
050                Boolean throwWhenNotExist=Caster.toBoolean(objThrowWhenNotExist,null);
051                if(throwWhenNotExist!=null) {
052                        try {
053                                return _call(pc, key, throwWhenNotExist.booleanValue(), Util.getDefault(pc, ConfigImpl.CACHE_DEFAULT_OBJECT));
054                        } 
055                        catch (IOException e) {
056                                throw Caster.toPageException(e);
057                        }
058                }
059                
060                // compatibility behavior, second parameter is a cacheName 
061                if(objThrowWhenNotExist instanceof String) {
062                        String cacheName=(String)objThrowWhenNotExist;
063                        if(!StringUtil.isEmpty(cacheName)) {
064                                try {
065                                        Cache cache = Util.getCache(pc.getConfig(),cacheName,null);
066                                        
067                                        if(cache!=null) 
068                                                return _call(pc, key, false, cache);
069                                } 
070                                catch (IOException e) {
071                                        throw Caster.toPageException(e);
072                                }
073                        }
074                }
075                
076                // not a boolean or cacheName
077                throw new FunctionException(pc, "cacheGet", 2, "ThrowWhenNotExist", "arguments needs to be a boolean value, but also a valid cacheName is supported for compatibility reasons to other engines");
078        }
079        
080        public static Object call(PageContext pc, String key, Object objThrowWhenNotExist,String cacheName) throws PageException {
081                
082                
083                Boolean throwWhenNotExist=Caster.toBoolean(objThrowWhenNotExist,null);
084                if(throwWhenNotExist==null)throw new FunctionException(pc, "cacheGet", 2, "ThrowWhenNotExist", "arguments needs to be a boolean value");
085                
086                try {
087                        Cache cache = Util.getCache(pc,cacheName,ConfigImpl.CACHE_DEFAULT_OBJECT);
088                        return _call(pc, key, throwWhenNotExist.booleanValue(), cache);
089                } catch (IOException e) {
090                        throw Caster.toPageException(e);
091                }
092        }
093
094        private static Object _call(PageContext pc, String key, boolean throwWhenNotExist,Cache cache) throws IOException {
095                return throwWhenNotExist?cache.getValue(Util.key(key)):cache.getValue(Util.key(key),null);
096        }
097}