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.util.Iterator;
022import java.util.List;
023
024import lucee.commons.io.cache.Cache;
025import lucee.commons.io.cache.CacheEntry;
026import lucee.runtime.PageContext;
027import lucee.runtime.cache.util.WildCardFilter;
028import lucee.runtime.config.ConfigImpl;
029import lucee.runtime.exp.PageException;
030import lucee.runtime.ext.function.Function;
031import lucee.runtime.op.Caster;
032import lucee.runtime.type.KeyImpl;
033import lucee.runtime.type.Struct;
034import lucee.runtime.type.StructImpl;
035
036/**
037 * 
038 */
039public final class CacheGetAll implements Function {
040        
041        private static final long serialVersionUID = 6395709569356486777L;
042
043        public static Struct call(PageContext pc) throws PageException {
044                return call(pc, null,null);
045        }
046        public static Struct call(PageContext pc,String filter) throws PageException {
047                return call(pc, filter,null);
048        }
049        
050        public static Struct call(PageContext pc,String filter, String cacheName) throws PageException {
051                try {
052                        Cache cache = Util.getCache(pc,cacheName,ConfigImpl.CACHE_DEFAULT_OBJECT);
053                        List<CacheEntry> entries = CacheGetAllIds.isFilter(filter)?cache.entries(new WildCardFilter(filter,true)):cache.entries();
054                        Iterator<CacheEntry> it=entries.iterator();
055                        Struct sct = new StructImpl();
056                        CacheEntry entry;
057                        while(it.hasNext()){
058                                entry= it.next();
059                                sct.setEL(KeyImpl.getInstance(entry.getKey()),entry.getValue());
060                        }
061                        return sct;
062                } catch (Exception e) {
063                        throw Caster.toPageException(e);
064                }
065        }
066}