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.runtime.PageContext;
026import lucee.runtime.cache.util.WildCardFilter;
027import lucee.runtime.config.ConfigImpl;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.ext.function.Function;
030import lucee.runtime.op.Caster;
031import lucee.runtime.type.Array;
032import lucee.runtime.type.ArrayImpl;
033
034/**
035 * 
036 */
037public final class CacheGetAllIds implements Function {
038        
039        private static final long serialVersionUID = 4831944874663718056L;
040
041
042        public static Array call(PageContext pc) throws PageException {
043                return call(pc, null,null);
044        }
045        
046
047        public static Array call(PageContext pc, String filter) throws PageException {
048                return call(pc, filter, null);
049        }
050        
051        public static Array call(PageContext pc, String filter, String cacheName) throws PageException {
052                try {
053                        Cache cache = Util.getCache(pc,cacheName,ConfigImpl.CACHE_DEFAULT_OBJECT);
054                        
055                        List<String> keys = isFilter(filter)?cache.keys(new WildCardFilter(filter,true)):cache.keys();
056                        Iterator<String> it = keys.iterator();
057                        Array arr = new ArrayImpl();
058                        while(it.hasNext()){
059                                arr.append(it.next());
060                        }
061                        return arr;
062                } catch (Exception e) {
063                        throw Caster.toPageException(e);
064                }
065        }
066
067
068        protected static boolean isFilter(String filter) {
069                return filter!=null && filter.length()>0 && !filter.equals("*");
070        }
071}