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 lucee.commons.io.cache.Cache; 022import lucee.runtime.PageContext; 023import lucee.runtime.config.ConfigImpl; 024import lucee.runtime.exp.PageException; 025import lucee.runtime.ext.function.Function; 026import lucee.runtime.op.Caster; 027import lucee.runtime.type.dt.TimeSpan; 028 029/** 030 * 031 */ 032public final class CachePut implements Function { 033 034 private static final long serialVersionUID = -8636947330333269874L; 035 036 public static String call(PageContext pc, String key,Object value) throws PageException { 037 return _call(pc,key, value, null, null,null); 038 } 039 public static String call(PageContext pc, String key,Object value,TimeSpan timeSpan) throws PageException { 040 return _call(pc,key, value, valueOf(timeSpan), null,null); 041 } 042 public static String call(PageContext pc, String key,Object value,TimeSpan timeSpan, TimeSpan idleTime) throws PageException { 043 return _call(pc,key, value, valueOf(timeSpan), valueOf(idleTime),null); 044 } 045 public static String call(PageContext pc, String key,Object value,TimeSpan timeSpan, TimeSpan idleTime,String cacheName) throws PageException { 046 return _call(pc,key, value, valueOf(timeSpan), valueOf(idleTime),cacheName); 047 } 048 049 private static String _call(PageContext pc, String key,Object value,Long timeSpan, Long idleTime,String cacheName) throws PageException { 050 //if(timeSpan!=null && timeSpan.longValue()==0L) return ""; 051 //if(idleTime!=null && idleTime.longValue()==0L) return ""; 052 try { 053 Cache cache = Util.getCache(pc,cacheName,ConfigImpl.CACHE_DEFAULT_OBJECT); 054 cache.put(Util.key(key), value, idleTime, timeSpan); 055 } catch (Exception e) { 056 throw Caster.toPageException(e); 057 } 058 059 return ""; 060 } 061 062 private static Long valueOf(TimeSpan timeSpan) { 063 if(timeSpan==null) return null; 064 return Long.valueOf(timeSpan.getMillis()); 065 } 066}