001    package railo.runtime.functions.cache;
002    
003    import railo.commons.io.cache.Cache;
004    import railo.runtime.PageContext;
005    import railo.runtime.config.ConfigImpl;
006    import railo.runtime.exp.PageException;
007    import railo.runtime.ext.function.Function;
008    import railo.runtime.op.Caster;
009    import railo.runtime.type.dt.TimeSpan;
010    
011    /**
012     * 
013     */
014    public final class CachePut implements Function {
015    
016            private static final long serialVersionUID = -8636947330333269874L;
017    
018            public static String call(PageContext pc, String key,Object value) throws PageException {
019                    return _call(pc,key, value, null, null,null);
020            }
021            public static String call(PageContext pc, String key,Object value,TimeSpan timeSpan) throws PageException {
022                    return _call(pc,key, value, valueOf(timeSpan), null,null);
023            }
024            public static String call(PageContext pc, String key,Object value,TimeSpan timeSpan, TimeSpan idleTime) throws PageException {
025                    return _call(pc,key, value, valueOf(timeSpan), valueOf(idleTime),null);
026            }
027            public static String call(PageContext pc, String key,Object value,TimeSpan timeSpan, TimeSpan idleTime,String cacheName) throws PageException {
028                    return _call(pc,key, value, valueOf(timeSpan), valueOf(idleTime),cacheName);
029            }
030            
031            private static String _call(PageContext pc, String key,Object value,Long timeSpan, Long idleTime,String cacheName) throws PageException {
032                    //if(timeSpan!=null && timeSpan.longValue()==0L) return "";
033                    //if(idleTime!=null && idleTime.longValue()==0L) return "";
034                    try {
035                            Cache cache = Util.getCache(pc.getConfig(),cacheName,ConfigImpl.CACHE_DEFAULT_OBJECT);
036                            cache.put(Util.key(key), value, idleTime, timeSpan);
037                    } catch (Exception e) {
038                            throw Caster.toPageException(e);
039                    }
040                    
041                    return "";
042            }
043            
044            private static Long valueOf(TimeSpan timeSpan) {
045                    if(timeSpan==null) return null;
046                    return Long.valueOf(timeSpan.getMillis());
047            }
048    }